Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WebBrowser control, right click menu 1

Status
Not open for further replies.

magnus2

Programmer
Mar 8, 2004
23
SE
Hi! I use a WebBrowser control. I use WebBrowser.Navigate to display a web page. The usual IE right click menu is displayed when I right click over the page. Can I disable the right click menu directly from Visual Basic? I'm very grateful for comments!
 
You need to hook the window and prevent the message WM_RBUTTONUP from being delivered to the hooked window.


"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 
Thank you DrJavaJoe for your answer! I just - maybe - have found a way to work around this problem, but I ask you anyway - how do I hook a window? Thanks again for your time. Thank you very much!
 
'place this in your form module and add a WebBrowser control

Option Explicit
Private Sub Form_Load()
WebBrowser1.Navigate " gLngMouseHook = SetWindowsHookEx(WH_MOUSE, AddressOf MouseHookProc, App.hInstance, GetCurrentThreadId)
End Sub

Private Sub Form_Unload(Cancel As Integer)
UnhookWindowsHookEx gLngMouseHook
End Sub


'and this in a code module

Option Explicit

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
Public Type MOUSEHOOKSTRUCT
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
strBuffer = Space(255)
strClassName = "Internet Explorer_Server"
'Get the classname for the Window that has been clicked
'If the function returns 0, it has failed
lngResult = GetClassName(mhs.hwnd, strBuffer, Len(strBuffer))
If lngResult > 0 Then
If Left$(strBuffer, lngResult) = strClassName Then
MouseHookProc = 1
Exit Function
End If
End If
End If
MouseHookProc = CallNextHookEx(gLngMouseHook, nCode, wParam, mhs)
End Function

****
Always stop the program using the Unload method in code or clicking the close button on the forms titlebar. DO NOT use the end button on the IDE's toolbar.


"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 
I have had a similar problem. Instead of disabling right click I pop up a menu. You can capture right click and visualize menu, form, message (aret in javascript). Thus the menu that IE displays will be overriden.

The following expression is True if you have clicked the right button:
wbDocument.Document.script.event.Button = 2

You have to capture "onmousedown", "onmouseup" or "onclick" event:
Private Sub wbDocument_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim oForwardBodyRightClick As New CForward

'Capture mouse Richt click on the document body
oForwardBodyRightClick.Set_Destination Me, "CaptureBodyRightClick"
oForwardBodyRightClick.ID = "CaptureBodyRightClick"
Set oForwardBodyRightClick.Element = wbDocument.Document.body
wbDocument.Document.body.onmouseup = oForwardBodyRightClick

End Sub

The function that captures the mouse click:
Public Sub CaptureBodyRightClick(sMsg As String, oElement As Object)
If sMsg = "CaptureBodyRightClick" And wbDocument.Document.script.event.Button = 2 Then Me.PopupMenu frmMDIForm.mnuDocumentParent
End Sub


Here is the source of CForward class:
Option Explicit
Dim m_oObject As Object
Dim m_sMethod As String
Dim m_bInstantiated As Boolean

Public ID As String
Public Element As Object

Public Sub Class_Initialize()
m_bInstantiated = False
End Sub

Public Sub Set_Destination(oInObject As Object, sInMethod As String)
Set m_oObject = oInObject
m_sMethod = sInMethod
m_bInstantiated = True
End Sub

Public Sub My_Default_Method()
Dim avArgs(1) As Variant
avArgs(0) = Me.ID
avArgs(1) = Me.Element

If m_bInstantiated Then
CallByName m_oObject, m_sMethod, VbMethod, Me.ID, Me.Element
End If
End Sub


I pass some parameters. Most likely you don't need them.

I did not disable right click. I just display something else intead of default menu.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top