0

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

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

所以我不能使用 Webbrowser1.ContextMenuEnabled = False

感谢您的回答

4

1 回答 1

1

一个简单的方法是在应用程序级别捕获 WM_RBUTTONDOWN 和 WM_RBUTTONDBLCLK 消息。您可以使用语句将消息过滤器添加到您的应用程序Application.AddMessageFilter(instance of IMessageFilter)。在下面的示例中,ImessageFilter 干扰由包含 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
于 2017-01-21T19:27:02.707 回答