禁用AxWebbrowser上下文菜单VB.NET

时间:2017-01-21 16:23:06

标签: vb.net webbrowser-control contextmenu axwebbrowser

我使用Axwebbrowser在我的VB.NET应用程序中显示HTML页面,我想知道如何禁用其上下文菜单?

我使用AxWebbrowser比原始Webbrowser组件更多,因为它处理NewWindows 2事件,帮助我在新选项卡中打开链接时获取弹出URL。

所以我无法使用Webbrowser1.ContextMenuEnabled = False

感谢您的回答

1 个答案:

答案 0 :(得分:1)

一种简单的方法是在应用程序级别捕获WM_RBUTTONDOWN和WM_RBUTTONDBLCLK消息。您可以使用语句Application.AddMessageFilter(instance of IMessageFilter)向应用程序添加消息过滤器。在下面的示例中,ImessageFilter interferace由包含AXWebrowser控件的表单实现。由于过滤器是应用程序范围的,因此在表单激活/停用时会添加/删除过滤器。

Public Class Form1
    Implements IMessageFilter

    Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
        Const WM_RBUTTONDOWN  As Int32 = &H204
        Const WM_RBUTTONDBLCLK As Int32 = &H206
        ' check that the message is WM_RBUTTONDOWN Or WM_RBUTTONDBLCLK
        ' verify AxWebBrowser1 is not null
        ' verify the target is AxWebBrowser1
        Return (m.Msg = WM_RBUTTONDOWN OrElse m.Msg = WM_RBUTTONDBLCLK) AndAlso (AxWebBrowser1 IsNot Nothing) AndAlso (AxWebBrowser1 Is Control.FromChildHandle(m.HWnd))
    End Function

    Protected Overrides Sub OnActivated(e As EventArgs)
        MyBase.OnActivated(e)
        ' Filter is application wide, activate filter with form
        Application.AddMessageFilter(Me)
    End Sub

    Protected Overrides Sub OnDeactivate(e As EventArgs)
        MyBase.OnDeactivate(e)
        ' Filter is application wide, remove when form not active
        Application.RemoveMessageFilter(Me)
    End Sub

End Class