我正在处理上下文菜单项,以便在单击上下文菜单项时显示消息框。当我为 Web 浏览器创建自定义鼠标右键单击事件时,上下文菜单项的单击事件将不会触发。
我尝试了几种方法让它工作,但没有一个能工作,它一直在绕圈,因为当我单击上下文菜单项时什么也不会发生。
这是我尝试过的:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim html As String = "<!DOCTYPE html><html><head><script src='https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js'></script></head><body><div id='main' class='main' style='padding-left: 13px;'><div id='editor' dir='auto' placeholder='' aria-multiline='true' contenteditable='true' tabindex='1'></div></div></body></html>"
WebBrowser1.DocumentText = html
WebBrowser1.ContextMenu = contextMenu1
End Sub
Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handler MenuItem4_Click
MessageBox.Show("Copy")
End Sub
我也尝试过这个:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim html As String = "<!DOCTYPE html><html><head><script src='https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js'></script></head><body><div id='main' class='main' style='padding-left: 13px;'><div id='editor' dir='auto' placeholder='' aria-multiline='true' contenteditable='true' tabindex='1'></div></div></body></html>"
WebBrowser1.DocumentText = html
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
AddHandler WebBrowser1.Document.MouseDown, New HtmlElementEventHandler(AddressOf WebBrowser1_MouseDown)
End Sub
Private Sub WebBrowser1_MouseDown(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
If e.MouseButtonsPressed = Windows.Forms.MouseButtons.Right Then
ContextMenu1.Show(WebBrowser1, New System.Drawing.Point(e.MousePosition.X, e.MousePosition.Y))
AddHandler MenuItem4.Click, New EventHandler(AddressOf MenuItem4_Click)
End If
End Sub
Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("Copy")
End Sub
你知道为什么当我点击上下文菜单项时,上下文菜单项不会被触发吗?
如果我做错了什么,您能否向我展示一个示例,当我在网络浏览器上单击鼠标右键后单击项目时,触发上下文菜单项的正确方法是什么?