Disabling the AxWebbrowser Context Menu VB.NET

232 views Asked by At

I'm using Axwebbrowser to display HTML page in my VB.NET application and i would like to know how disable its context menu ?

I use AxWebbrowser more than the original Webbrowser component because it handle the NewWindows 2 event that help me getting the popup url when a link is open in a new tab for example.

So i can't use the Webbrowser1.ContextMenuEnabled = False

Thanks for your answers

1

There are 1 answers

2
TnTinMn On

A simple method is to trap the WM_RBUTTONDOWN and WM_RBUTTONDBLCLK messages at the application level. You can add a message filter to your application using the statement Application.AddMessageFilter(instance of IMessageFilter). In the example below, the ImessageFilter interferace is implemented by the form containing the AXWebrowser control. As the filter is application wide, it is added/removed when the form activates/deactivates.

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