C# vb.net webbrowser - 触发右键单击
C# vb.net webbrowser - trigger right click
按照这个例子
C# webbrowser - trigger right click
如果我从代码创建一个元素(按钮),它可以工作,但是如果我尝试在普通网页中打开上下文菜单则不起作用
有人可以帮助我吗?
Imports System.Runtime.InteropServices
Imports mshtml
Imports System.IO
Public Class Form1
Dim Wb As New System.Windows.Forms.WebBrowser
Dim str As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Size = New Size(1350, 700)
Me.Location = New Point(10, 40)
Wb.Size = New Size(1350, 700)
Wb.Location = New Point(10, 10)
Wb.IsWebBrowserContextMenuEnabled = True
Wb.AllowWebBrowserDrop = True
Me.Controls.Add(Wb)
'Wb.DocumentText = "<button class=""mybtn"" type=""submit""> Right click"
'AddHandler Wb.DocumentCompleted, AddressOf webBrowser1_DocumentCompleted
str = "http://google.com"
Wb.Navigate(str)
Do Until Wb.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
Loop
Dim MyHtmlElements As System.Windows.Forms.HtmlElementCollection
MyHtmlElements = Wb.Document.GetElementsByTagName("input")
Dim myHtmlElement As System.Windows.Forms.HtmlElementCollection
myHtmlElement = MyHtmlElements.GetElementsByName("btnK")
Dim MyPoint As New Point(myHtmlElement(0).OffsetRectangle.Left + myHtmlElement(0).OffsetRectangle.Width / 2, myHtmlElement(0).OffsetRectangle.Top + myHtmlElement(0).OffsetRectangle.Height / 2)
Dim parentElement As System.Windows.Forms.HtmlElement
parentElement = myHtmlElement(0).OffsetParent
While parentElement IsNot Nothing
MyPoint.X += parentElement.OffsetRectangle.Left
MyPoint.Y += parentElement.OffsetRectangle.Top
parentElement = parentElement.OffsetParent
End While
Dim controlLoc As Point = Me.PointToScreen(Wb.Location)
controlLoc.X = controlLoc.X + MyPoint.X
controlLoc.Y = controlLoc.Y + MyPoint.Y
Cursor.Position = controlLoc
MouseSimulator.ClickRightMouseButton()
End Sub
Private Sub webBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
For Each element As HtmlElement In Wb.Document.GetElementsByTagName("button")
If element.GetAttribute("ClassName") = "mybtn" Then
Dim controlLoc As Point = Me.PointToScreen(Wb.Location)
controlLoc.X = controlLoc.X + element.OffsetRectangle.Left
controlLoc.Y = controlLoc.Y + element.OffsetRectangle.Top
Cursor.Position = controlLoc
MouseSimulator.ClickRightMouseButton()
End If
Next
End Sub
End Class
Public Class MouseSimulator
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function SendInput(nInputs As UInteger, ByRef pInputs As INPUT, cbSize As Integer) As UInteger
End Function
<StructLayout(LayoutKind.Sequential)>
Private Structure INPUT
Public type As SendInputEventType
Public mkhi As MouseKeybdhardwareInputUnion
End Structure
<StructLayout(LayoutKind.Explicit)>
Private Structure MouseKeybdhardwareInputUnion
<FieldOffset(0)>
Public mi As MouseInputData
<FieldOffset(0)>
Public ki As KEYBDINPUT
<FieldOffset(0)>
Public hi As HARDWAREINPUT
End Structure
<StructLayout(LayoutKind.Sequential)>
Private Structure KEYBDINPUT
Public wVk As UShort
Public wScan As UShort
Public dwFlags As UInteger
Public time As UInteger
Public dwExtraInfo As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)>
Private Structure HARDWAREINPUT
Public uMsg As Integer
Public wParamL As Short
Public wParamH As Short
End Structure
Private Structure MouseInputData
Public dx As Integer
Public dy As Integer
Public mouseData As UInteger
Public dwFlags As MouseEventFlags
Public time As UInteger
Public dwExtraInfo As IntPtr
End Structure
<Flags>
Private Enum MouseEventFlags As UInteger
MOUSEEVENTF_MOVE = &H1
MOUSEEVENTF_LEFTDOWN = &H2
MOUSEEVENTF_LEFTUP = &H4
MOUSEEVENTF_RIGHTDOWN = &H8
MOUSEEVENTF_RIGHTUP = &H10
MOUSEEVENTF_MIDDLEDOWN = &H20
MOUSEEVENTF_MIDDLEUP = &H40
MOUSEEVENTF_XDOWN = &H80
MOUSEEVENTF_XUP = &H100
MOUSEEVENTF_WHEEL = &H800
MOUSEEVENTF_VIRTUALDESK = &H4000
MOUSEEVENTF_ABSOLUTE = &H8000
End Enum
Private Enum SendInputEventType As Integer
InputMouse
InputKeyboard
InputHardware
End Enum
Public Shared Sub ClickRightMouseButton()
Dim mouseDownInput As New INPUT()
mouseDownInput.type = SendInputEventType.InputMouse
mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTDOWN
SendInput(1, mouseDownInput, Marshal.SizeOf(New INPUT()))
Dim mouseUpInput As New INPUT()
mouseUpInput.type = SendInputEventType.InputMouse
mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTUP
SendInput(1, mouseUpInput, Marshal.SizeOf(New INPUT()))
End Sub
End Class
谢谢
过了一会儿我找到了解决办法。
但现在我遇到了另一个问题:如何通过代码 select 上下文菜单项?
感谢大家的帮助。
______更新______
对于所有感兴趣的人,这是一个很好的 link 论点。
http://pinvoke.net/default.aspx/user32.mouse_event
但回到我的问题,我不知道在哪里可以找到刚刚创建的上下文菜单。
我不知道是在 webBrowser 中,与 htmlelement 相关联,还是在表单中或其他地方。
检查 htmlElement,没有 contextMenu 属性,而 webBrowser contextMenu 属性 什么都没有。
...
如果您只想要 Webbrowser 控件的自定义上下文菜单,那么您的代码似乎有点矫枉过正。下面的代码对我有用——只是一个带有 Webbrowser 控件的基本形式:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim Strip As New ContextMenuStrip
Dim Item1, Item2 As ToolStripMenuItem
Item1 = New ToolStripMenuItem("Test1", Nothing, AddressOf DoTestAction)
Item2 = New ToolStripMenuItem("Google", Nothing, AddressOf DoGoogle)
Strip.Items.Add(Item1)
Strip.Items.Add(Item2)
Me.WebBrowser1.ContextMenuStrip = Strip
End Sub
Public Sub DoTestAction(sender As Object, e As EventArgs)
MessageBox.Show("Testing")
End Sub
Public Sub DoGoogle(sender As Object, e As EventArgs)
Me.WebBrowser1.Navigate("http://www.google.com")
End Sub
End Class
检查了一段时间后,我找到了解决方案。
所有代码都可以正常工作
问题是我把代码放在子 form1_load 中:
刚加载完 form1_load,程序重绘表单,上下文菜单失去焦点并消失。
但是添加一个按钮,更改 form1_load 并添加一个子项都可以正常工作
Imports System.Runtime.InteropServices
Imports mshtml
Imports System.IO
Public Class Form1
Dim Wb As New System.Windows.Forms.WebBrowser
Dim bStart As New Button
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Size = New Size(1350, 600)
Me.Location = New Point(10, 40)
bStart.Text = "go"
bStart.Size = New Size(40, 20)
bStart.Location = New Point(10, 10)
AddHandler bStart.Click, AddressOf bStart_click
Me.Controls.Add(bStart)
Wb.Size = New Size(1350, 550)
Wb.Location = New Point(10, 50)
Wb.IsWebBrowserContextMenuEnabled = True
Wb.AllowWebBrowserDrop = True
Me.Controls.Add(Wb)
End Sub
Private Sub bStart_click()
str = "http://google.com"
Wb.Navigate(str)
Do Until Wb.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
Loop
Dim HtmlActiveElements As System.Windows.Forms.HtmlElementCollection
HtmlActiveElements = Wb.Document.GetElementsByTagName("input")
Dim myHtmlElement As System.Windows.Forms.HtmlElementCollection
myHtmlElement = HtmlActiveElements.GetElementsByName("btnK")
Dim MyPoint As New Point(myHtmlElement(0).OffsetRectangle.Left + myHtmlElement(0).OffsetRectangle.Width / 2, myHtmlElement(0).OffsetRectangle.Top + myHtmlElement(0).OffsetRectangle.Height / 2)
Dim parentElement As System.Windows.Forms.HtmlElement
parentElement = myHtmlElement(0).OffsetParent
While parentElement IsNot Nothing
MyPoint.X += parentElement.OffsetRectangle.Left
MyPoint.Y += parentElement.OffsetRectangle.Top
parentElement = parentElement.OffsetParent
End While
Dim controlLoc As Point = Me.PointToScreen(Wb.Location)
controlLoc.X = controlLoc.X + MyPoint.X
controlLoc.Y = controlLoc.Y + MyPoint.Y
Cursor.Position = controlLoc
MouseSimulator.ClickRightMouseButton()
End Sub
End Class
按照这个例子
C# webbrowser - trigger right click
如果我从代码创建一个元素(按钮),它可以工作,但是如果我尝试在普通网页中打开上下文菜单则不起作用 有人可以帮助我吗?
Imports System.Runtime.InteropServices
Imports mshtml
Imports System.IO
Public Class Form1
Dim Wb As New System.Windows.Forms.WebBrowser
Dim str As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Size = New Size(1350, 700)
Me.Location = New Point(10, 40)
Wb.Size = New Size(1350, 700)
Wb.Location = New Point(10, 10)
Wb.IsWebBrowserContextMenuEnabled = True
Wb.AllowWebBrowserDrop = True
Me.Controls.Add(Wb)
'Wb.DocumentText = "<button class=""mybtn"" type=""submit""> Right click"
'AddHandler Wb.DocumentCompleted, AddressOf webBrowser1_DocumentCompleted
str = "http://google.com"
Wb.Navigate(str)
Do Until Wb.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
Loop
Dim MyHtmlElements As System.Windows.Forms.HtmlElementCollection
MyHtmlElements = Wb.Document.GetElementsByTagName("input")
Dim myHtmlElement As System.Windows.Forms.HtmlElementCollection
myHtmlElement = MyHtmlElements.GetElementsByName("btnK")
Dim MyPoint As New Point(myHtmlElement(0).OffsetRectangle.Left + myHtmlElement(0).OffsetRectangle.Width / 2, myHtmlElement(0).OffsetRectangle.Top + myHtmlElement(0).OffsetRectangle.Height / 2)
Dim parentElement As System.Windows.Forms.HtmlElement
parentElement = myHtmlElement(0).OffsetParent
While parentElement IsNot Nothing
MyPoint.X += parentElement.OffsetRectangle.Left
MyPoint.Y += parentElement.OffsetRectangle.Top
parentElement = parentElement.OffsetParent
End While
Dim controlLoc As Point = Me.PointToScreen(Wb.Location)
controlLoc.X = controlLoc.X + MyPoint.X
controlLoc.Y = controlLoc.Y + MyPoint.Y
Cursor.Position = controlLoc
MouseSimulator.ClickRightMouseButton()
End Sub
Private Sub webBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
For Each element As HtmlElement In Wb.Document.GetElementsByTagName("button")
If element.GetAttribute("ClassName") = "mybtn" Then
Dim controlLoc As Point = Me.PointToScreen(Wb.Location)
controlLoc.X = controlLoc.X + element.OffsetRectangle.Left
controlLoc.Y = controlLoc.Y + element.OffsetRectangle.Top
Cursor.Position = controlLoc
MouseSimulator.ClickRightMouseButton()
End If
Next
End Sub
End Class
Public Class MouseSimulator
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function SendInput(nInputs As UInteger, ByRef pInputs As INPUT, cbSize As Integer) As UInteger
End Function
<StructLayout(LayoutKind.Sequential)>
Private Structure INPUT
Public type As SendInputEventType
Public mkhi As MouseKeybdhardwareInputUnion
End Structure
<StructLayout(LayoutKind.Explicit)>
Private Structure MouseKeybdhardwareInputUnion
<FieldOffset(0)>
Public mi As MouseInputData
<FieldOffset(0)>
Public ki As KEYBDINPUT
<FieldOffset(0)>
Public hi As HARDWAREINPUT
End Structure
<StructLayout(LayoutKind.Sequential)>
Private Structure KEYBDINPUT
Public wVk As UShort
Public wScan As UShort
Public dwFlags As UInteger
Public time As UInteger
Public dwExtraInfo As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)>
Private Structure HARDWAREINPUT
Public uMsg As Integer
Public wParamL As Short
Public wParamH As Short
End Structure
Private Structure MouseInputData
Public dx As Integer
Public dy As Integer
Public mouseData As UInteger
Public dwFlags As MouseEventFlags
Public time As UInteger
Public dwExtraInfo As IntPtr
End Structure
<Flags>
Private Enum MouseEventFlags As UInteger
MOUSEEVENTF_MOVE = &H1
MOUSEEVENTF_LEFTDOWN = &H2
MOUSEEVENTF_LEFTUP = &H4
MOUSEEVENTF_RIGHTDOWN = &H8
MOUSEEVENTF_RIGHTUP = &H10
MOUSEEVENTF_MIDDLEDOWN = &H20
MOUSEEVENTF_MIDDLEUP = &H40
MOUSEEVENTF_XDOWN = &H80
MOUSEEVENTF_XUP = &H100
MOUSEEVENTF_WHEEL = &H800
MOUSEEVENTF_VIRTUALDESK = &H4000
MOUSEEVENTF_ABSOLUTE = &H8000
End Enum
Private Enum SendInputEventType As Integer
InputMouse
InputKeyboard
InputHardware
End Enum
Public Shared Sub ClickRightMouseButton()
Dim mouseDownInput As New INPUT()
mouseDownInput.type = SendInputEventType.InputMouse
mouseDownInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTDOWN
SendInput(1, mouseDownInput, Marshal.SizeOf(New INPUT()))
Dim mouseUpInput As New INPUT()
mouseUpInput.type = SendInputEventType.InputMouse
mouseUpInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTUP
SendInput(1, mouseUpInput, Marshal.SizeOf(New INPUT()))
End Sub
End Class
谢谢
过了一会儿我找到了解决办法。 但现在我遇到了另一个问题:如何通过代码 select 上下文菜单项?
感谢大家的帮助。
______更新______
对于所有感兴趣的人,这是一个很好的 link 论点。
http://pinvoke.net/default.aspx/user32.mouse_event
但回到我的问题,我不知道在哪里可以找到刚刚创建的上下文菜单。 我不知道是在 webBrowser 中,与 htmlelement 相关联,还是在表单中或其他地方。 检查 htmlElement,没有 contextMenu 属性,而 webBrowser contextMenu 属性 什么都没有。
...
如果您只想要 Webbrowser 控件的自定义上下文菜单,那么您的代码似乎有点矫枉过正。下面的代码对我有用——只是一个带有 Webbrowser 控件的基本形式:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim Strip As New ContextMenuStrip
Dim Item1, Item2 As ToolStripMenuItem
Item1 = New ToolStripMenuItem("Test1", Nothing, AddressOf DoTestAction)
Item2 = New ToolStripMenuItem("Google", Nothing, AddressOf DoGoogle)
Strip.Items.Add(Item1)
Strip.Items.Add(Item2)
Me.WebBrowser1.ContextMenuStrip = Strip
End Sub
Public Sub DoTestAction(sender As Object, e As EventArgs)
MessageBox.Show("Testing")
End Sub
Public Sub DoGoogle(sender As Object, e As EventArgs)
Me.WebBrowser1.Navigate("http://www.google.com")
End Sub
End Class
检查了一段时间后,我找到了解决方案。 所有代码都可以正常工作 问题是我把代码放在子 form1_load 中: 刚加载完 form1_load,程序重绘表单,上下文菜单失去焦点并消失。
但是添加一个按钮,更改 form1_load 并添加一个子项都可以正常工作
Imports System.Runtime.InteropServices
Imports mshtml
Imports System.IO
Public Class Form1
Dim Wb As New System.Windows.Forms.WebBrowser
Dim bStart As New Button
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Size = New Size(1350, 600)
Me.Location = New Point(10, 40)
bStart.Text = "go"
bStart.Size = New Size(40, 20)
bStart.Location = New Point(10, 10)
AddHandler bStart.Click, AddressOf bStart_click
Me.Controls.Add(bStart)
Wb.Size = New Size(1350, 550)
Wb.Location = New Point(10, 50)
Wb.IsWebBrowserContextMenuEnabled = True
Wb.AllowWebBrowserDrop = True
Me.Controls.Add(Wb)
End Sub
Private Sub bStart_click()
str = "http://google.com"
Wb.Navigate(str)
Do Until Wb.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
Loop
Dim HtmlActiveElements As System.Windows.Forms.HtmlElementCollection
HtmlActiveElements = Wb.Document.GetElementsByTagName("input")
Dim myHtmlElement As System.Windows.Forms.HtmlElementCollection
myHtmlElement = HtmlActiveElements.GetElementsByName("btnK")
Dim MyPoint As New Point(myHtmlElement(0).OffsetRectangle.Left + myHtmlElement(0).OffsetRectangle.Width / 2, myHtmlElement(0).OffsetRectangle.Top + myHtmlElement(0).OffsetRectangle.Height / 2)
Dim parentElement As System.Windows.Forms.HtmlElement
parentElement = myHtmlElement(0).OffsetParent
While parentElement IsNot Nothing
MyPoint.X += parentElement.OffsetRectangle.Left
MyPoint.Y += parentElement.OffsetRectangle.Top
parentElement = parentElement.OffsetParent
End While
Dim controlLoc As Point = Me.PointToScreen(Wb.Location)
controlLoc.X = controlLoc.X + MyPoint.X
controlLoc.Y = controlLoc.Y + MyPoint.Y
Cursor.Position = controlLoc
MouseSimulator.ClickRightMouseButton()
End Sub
End Class