如何在vb .net windows应用程序中禁用右键单击webbrowser控件 [英] how to disable right click on webbrowser control in vb .net windows application
问题描述
请给我一些技巧
提前感谢
最简单的方法是禁用WebBrowser控件中包含的文档的上下文菜单,例如WebBrowser1.IsWebBrowserContextMenuEnabled = False
你可以在WebBrowser_Navigated
事件(而不是Form_Load
事件),以便它适用于您可能导航到的每个页面。
如果您想在用户右键单击浏览器控件时执行其他操作,您实际上想要处理右键单击而不是忽略它。
您可以这样做(函数的名称是任意的)
Private Sub WebDocument_MouseDown( ByVal sender 作为系统。对象, ByVal e 正如 HtmlElementEventArgs)
如果(e.MouseButtonsPressed = Mous eButtons.Right)然后
' 做什么你想在这里做什么
结束 如果
结束 Sub
并将处理程序添加为RemoveHandler WebBrowser1.Document.MouseDown,新 HtmlElementEventHandler( AddressOf WebDocument_MouseDown)
AddHandler WebBrowser1.Document.MouseDown,新 HtmlElementEventHandler( AddressOf WebDocument_MouseDown)请注意我首先将其删除(如果不存在则不会抛出异常)以防止处理程序被多次添加。
另请注意,如果您有广告在设计时ded一个URL然后你不应该在Form_Load事件中添加处理程序,因为WebBrowser.Document对象将不存在(并且将抛出异常)。 WebBrowser_Navigated事件是此代码的适当位置。您还需要设置IsWebBrowserContextMenuEnabled
属性,以便在处理WebDocument_MouseDown
事件后停止弹出上下文菜单
please give me some techniques
thanks in advance
The easiest way to do this is to disable the context menu of the document contained in the WebBrowser control e.g.WebBrowser1.IsWebBrowserContextMenuEnabled = False
You could do this in theWebBrowser_Navigated
event (rather than theForm_Load
event) so that it applies to every page you might navigate to.
If you want to do something else when the User right-clicks in the browser control you actually want to handle the right-click rather than ignore it.
You could do something like this (the name of the function is arbitrary)
Private Sub WebDocument_MouseDown(ByVal sender As System.Object, ByVal e As HtmlElementEventArgs) If (e.MouseButtonsPressed = MouseButtons.Right) Then 'Do what you want to do here End If End Sub
And add the handler asRemoveHandler WebBrowser1.Document.MouseDown, New HtmlElementEventHandler(AddressOf WebDocument_MouseDown) AddHandler WebBrowser1.Document.MouseDown, New HtmlElementEventHandler(AddressOf WebDocument_MouseDown)Note that I remove it first (there is no exception thrown if it doesn't exist) to prevent the handler being added multiple times.
Also note that if you have added a URL at design time then you should not add the handler in the Form_Load event as the WebBrowser.Document object will not yet exist (and an exception will be thrown). The WebBrowser_Navigated event is an appropriate place for this code. You will also need to set theIsWebBrowserContextMenuEnabled
property to stop the context menu popping up after yourWebDocument_MouseDown
event has been processed
这篇关于如何在vb .net windows应用程序中禁用右键单击webbrowser控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!