Webbrowser popup menu

Posted on 2001-06-11
Last Modified: 2010-05-02
God morning all!

My problem is this:

I have a program, and I'd like to implement an internal webbrowser for it. It should work as internet explorer so I am using the webbrowser component. But I want to change som features in the popup menu (adding new menuitems to it) this is my first problem, how can I override the default popup menu? Secondly how can I know whats under the mousepointer? a link (and if so what that link points to, ie image, or html doc ....) I cant find a mousedown event for the borwser component.
Is it at all possible to do this with the webbrowser component, if not what are my options? is there any freeware component out there that incorporates functionality such as plugins?

For a good ansewer I will raise the points

Thank you for your time

Question by:clylv
  • 4
  • 3
  • 2

Author Comment

ID: 6176722
Ive sorted out the replacing bit :

Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy 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 Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Public Const GWL_WNDPROC = (-4)

Public Const GW_HWNDNEXT = 2
Public Const GW_CHILD = 5
Public Const WM_MOUSEACTIVATE = &H21
Public Const WM_CONTEXTMENU = &H7B
Public Const WM_RBUTTONDOWN = &H204

Public origWndProc As Long

Public Function AppWndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Select Case Msg
            Dim C As Integer
            Call CopyMemory(C, ByVal VarPtr(lParam) + 2, 2)
            If C = WM_RBUTTONDOWN Then
                Form1.PopupMenu Form1.mnuBrowser
                SendKeys "{ESC}"
            End If
            Form1.PopupMenu Form1.mnuBrowser
            SendKeys "{ESC}"
    End Select
    AppWndProc = CallWindowProc(origWndProc, hwnd, Msg, wParam, lParam)
End Function

Private Sub Form_Load()
    Dim h As Long, aClass As String, k As Long
    h = GetWindow(hwnd, GW_CHILD)
    aClass = Space$(128)
    Do While h
        k = GetClassName(h, aClass, 128)
        If Left$(aClass, k) = "Shell Embedding" Then hw = h: Exit Do
        h = GetWindow(h, GW_HWNDNEXT)

    WebBrowser1.Navigate ""
    origWndProc = SetWindowLong(hw, GWL_WNDPROC, AddressOf AppWndProc)
End Sub

STILL need help with whats under the mouse pointer when popup appears...
LVL 28

Expert Comment

ID: 6177313
you can use the webbrowser's StatusText property.  you are notified of its change when the OnStatusTextChange event fires.  of course, webpage scripting allows web developers to alter this property...essentially you see what IE sees in its statusbar when you view webpages.

if you want a lot of control and notifications over a page, use the DOM.  go to project -> references and select Microsoft HTML Object Library.  here is how you might cancel the default context menu using this object model as opposed to subclassing techniques used in your above snippet:

Private WithEvents htmlDoc As HTMLDocument

Private Sub WebBrowser_DocumentComplete(...)
   If (pDisp Is WebBrowser.Object) Then
      Set htmlDoc = WebBrowser.Document
   End If
End Sub

Private Sub htmlDoc_oncontextmenu() As Boolean
   'cancel the default context menu
   htmlDoc_oncontextmenu = False

   'replace with your own popup menu here
   Popupmenu mnuMain
End Sub

if you are familiar with DHTML and scripting as a web developer, you will catch on to how to use this object model fairly quickly.  otherwise, youll need to do some digging into the Object Browsre to become familiar with all of classes and functionality exposed via the HTML Object Library.

Author Comment

ID: 6177582
Tnx AzraSound,

Your solution is a better one, do you have any sugestions on how to know what the user is clicking on? the OnStatusTextChange event do not work properly on images and such...

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 28

Accepted Solution

AzraSound earned 150 total points
ID: 6177965
if you create an HTMLWindow object, you can trap global events occurring on the page if they "bubble".  to use this, you would use something similar to my above snippet by adding:

Private WithEvents htmlWin As HTMLWindow

'-- add to documentcomplete event
Set htmlWin = webBrowser.Document.parentWindow

now, events that "bubble" are those that occur, on say, the clicking of a button.  first, the button receives the event, and performs any actions that should occur then (as defined in the webpage itself for example).  then this event begins to "bubble", or move up, the object hierarchy of the page.  so, perhaps this event can then be handled by the form in which the image resides, and then the document containing that form, and then finally the window object (the top element in the DOM hierarchy).

if the event does bubble, you can access the element that triggered it and then use that to get the information you need.  this can be found through the HTMLWindow object's "event" attribute/property.   i am not on a development machine at this time to offer any concrete code, nor do i really have the time to build a sample.  however, perhaps later this evening (US time) or later this week i can put something together if you are still struggling with the implementation.
LVL 16

Expert Comment

ID: 6178235

Author Comment

ID: 6180112

Works like a charm greate tnx AzraSound! U r extreamly good at explaining. Giving u an extra 50p for your time an additional sampel is not nessessary for me.

once again thankyou

Author Comment

ID: 6180130

Thanyou for your example that will work as well very nice!

LVL 16

Expert Comment

ID: 6181113
You are welcome.
LVL 28

Expert Comment

ID: 6181345
glad i could help  :-)

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
passing a value with stream reader AFTER a ";" 3 75
vb6 connector to mongodb 2 122
Added a column screws up code 5 63
RUNRMTCMD from AS/400 12 71
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

820 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