C#vb.net webbrowser - 触发右键单击

时间:2016-05-22 13:39:42

标签: c# vb.net html-parsing

遵循此示例

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

由于

过了一会儿,我找到了解决方案。 但现在我遇到了另一个问题:如何按代码选择contextMenu项目?

感谢大家的帮助。

______更新______

对于所有感兴趣的人来说,这是一个很好的链接。

http://pinvoke.net/default.aspx/user32.mouse_event

但回到我的问题,我不知道在哪里可以找到刚刚创建的contextMenu。 我不知道是否在webBrowser中,与htmlelement相关联,或者在表单中或其他地方。 检查htmlElement,没有contextMenu属性,webBrowser contextMenu属性没什么。

...

2 个答案:

答案 0 :(得分:0)

如果您只想要Webbrowser控件的自定义上下文菜单,那么您的代码似乎有些过分。下面的代码对我有用 - 只是一个带有Web浏览器控件的基本表单:

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

答案 1 :(得分:0)

经过一段时间的检查,我找到了解决方案。 所有代码都运行正常 问题是我把代码放在sub 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