Override WebBrowser Control right click

Posted on 1999-12-13
Medium Priority
Last Modified: 2013-12-26
How do you override the WebBrowser controls default right click menu and use one of your own, I know how to create right click menus and how to override menu's for other controls but do not know how to for a control like WEbBrowser which has no mouse events (except drag and drop)
Question by:2Grey

Accepted Solution

jjmartin earned 140 total points
ID: 2277996
You must subclass windows to intercept the mouse click.  Use the following code:

'Insert this into the form that contains your webbrowser:

Private Sub Form_Load()
    'Start Trapping Right-Mouse clicks in WebBrowser Control:
    gLngMouseHook = SetWindowsHookEx(WH_MOUSE, AddressOf MouseHookProc, App.hInstance, GetCurrentThreadId)

End Sub

Private Sub Form_Unload(Cancel As Integer)
'Cancel the trapping of the code
UnhookWindowsHookEx gLngMouseHook

End Sub

'Add this to a BAS module:

Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lparam As Any) As Long
Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Const WM_RBUTTONUP = &H205
Public Const WH_MOUSE = 7

Public Type POINTAPI
    x As Long
    y As Long
End Type

    pt As POINTAPI
    hwnd As Long
    wHitTestCode As Long
    dwExtraInfo As Long
End Type
Public gLngMouseHook As Long
Public Function MouseHookProc(ByVal nCode As Long, ByVal wParam As Long, mhs As MOUSEHOOKSTRUCT) As Long
Dim strBuffer As String
Dim lngBufferLen As Long
Dim strClassName As String
Dim lngResult As Long

If (nCode >= 0 And wParam = WM_RBUTTONUP) Then

        'Preinitialize string
        strBuffer = Space(255)
       ' lngBufferLen = Len(strBuffer)
        'This is the string that holds the class name that we are looking for
        strClassName = "Internet Explorer_Server"
        Debug.Print strClassName
        'Get the classname for the Window that has been clicked, making sure something is returned
        'If the function returns 0, it has failed
        lngResult = GetClassName(mhs.hwnd, strBuffer, Len(strBuffer))
        Debug.Print Left$(strBuffer, lngResult)
        If lngResult > 0 Then

            'Check to see if the class of the window we clicked on is the same as above
            If Left$(strBuffer, lngResult) = strClassName Then
                'Value is the same. Squash the command
                MouseHookProc = 1
                Exit Function
            End If
        End If

    End If

MouseHookProc = CallNextHookEx(gLngMouseHook, nCode, wParam, mhs)
End Function

LVL 12

Expert Comment

ID: 2280941

Have been searching for a similar answer for a while now. Please look at Q.10231342 and add your $.02...


Author Comment

ID: 2360759
Sorry for the long time in responding, have been away on vacation, It worked well for brining up my own menu. I was wondering if you or anyone else could tell me how to view Q.10231342, I am new to this forum and am not fully aware on how to use it.

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question