VB6 - Custom Context Menu example
File(s): frmCustomContextMenu.frm, html\customContextMenu.html
When you run the "Custom Context Menu" example, it will present you the following screen:

You are being asked to right click on the html page and it will show you the details depending on where you click and what you do. For example if you first select some text in the page and then right click, you'll see this:

This shows our simple custom context menu. A menu with three options, "Reload", "Back" (in disabled form) and "Help" (with an icon).
At the bottom of the view we see that it shows the text we had selected before right clicking and the target kind of the menu is currently in "Selected text" mode.
You can get different results, depending on clicking with or without selected text, by right clicking on the image and by right clicking on the link. If you then click on the link and navigate to the antview website, the "Back" option in the menu becomes enabled and you can use it to navigate back.
If you select an option from the menu, the selected option will be displayed at the bottom of the form in the info area.
The code for all this actually fairly simple and looks like this:
Option Explicit
Public WithEvents HelpMenuItem As AntViewAx2.AntViewContextMenuItem
Public WithEvents BackMenuItem As AntViewAx2.AntViewContextMenuItem
Public WithEvents ReloadMenuItem As AntViewAx2.AntViewContextMenuItem
We start by declaring our 3 menu items, so that we have access to the individual "onclick" events of each menuitem.
Private Function KindAsString(Kind As AntViewAx2.TxContextMenuTargetKind) As String
Dim sKind As String
sKind = ""
Select Case Kind
Case AntViewAx2.cmtkPage
sKind = "Page"
Case AntViewAx2.cmtkImage
sKind = "Image"
Case AntViewAx2.cmtkSelectedText
sKind = "Selected Text"
Case AntViewAx2.cmtkAudio
sKind = "Audio"
Case AntViewAx2.cmtkVideo
sKind = "Video"
End Select
KindAsString = sKind
End Function
This is just a function to return the context menu target kind type as a string.
Now follows the OnContextMenuRequested event which handles the main parts.
Private Sub EdgeWebBrowser_OnContextMenuRequested(ByVal Args As AntViewAx2.IAntViewContextMenuRequestedEventArgs)
Dim menuItems As AntViewAx2.AntViewContextMenuItemCollection
Dim target As AntViewAx2.AntViewContextMenuTarget
Dim menuItem As AntViewAx2.AntViewContextMenuItem
Dim HelpIcon As AntViewAx2.AntViewContextMenuIcon
Dim Kind As String
Dim IconFile As String
Dim Path As String
Dim bOK As Boolean
InfoText.Text = "OnContextMenuRequested event"
' use a variable for easier reading.
Set menuItems = Args.menuItems
Set target = Args.ContextMenuTarget
menuItems.RemoveAllMenuItems
Set up a few variables for easier reading of our code and remove all existing menuitems from the default context menu.
Set HelpIcon = CreateObject("AntViewAx2.AntViewContextMenuIcon", "")
Create an object for our icon.
Set ReloadMenuItem = EdgeWebBrowser.CreateContextMenuItem("Reload", Nothing, cmikCommand)
menuItems.InsertValueAtIndex menuItems.Count, ReloadMenuItem
Create a new menu item via CreateContextMenuItem, with label "Reload", no icon (Nothing) and of type "Command".
Then add that menu on position "count of existing menu items" - which is zero at this moment - via the InsertValueAtIndex method.
Note that you could also have used menuitems.appendMenuItem here.
Set BackMenuItem = EdgeWebBrowser.CreateContextMenuItem("Back", Nothing, cmikCommand)
If Left(target.PageUri, 15) = "data:text/html;" Then
BackMenuItem.Enabled = False
Else
BackMenuItem.Enabled = True
End If
menuItems.InsertValueAtIndex menuItems.Count, BackMenuItem
Next up we create the "Back" menu item, but we test the current page URI to see what page we are on.
As we loaded our page as string, the URI looks like a data URL and starts with "data:text/html;". If we're on our initial page, disable the back button, enable it otherwise.
' Get the menu icon path
Path = strGetVisualBasicDemoPath
IconFile = Path & "\html\StatusAnnotations_Information_32xLG_color.png"
bOK = HelpIcon.LoadFromFile(IconFile)
If bOK = False Then
Set HelpIcon = Nothing
End If
Set HelpMenuItem = EdgeWebBrowser.CreateContextMenuItem("Help", HelpIcon, cmikCommand)
menuItems.InsertValueAtIndex menuItems.Count, HelpMenuItem
Our icon is stored as a png file in the html folder of our demo. First get that path and load the icon via the HelpIcon.LoadFromFile method.
We then Create the "Help" menu item and if all went well, it will also include an icon.
This is then added to the menu, where the count is now 2.
So we've now got 3 menu items (0 - Reload, 1 - Back and 2 - Help) added to our context menu.
What else can we do?
If target.HasLinkUri Then
InfoText.Text = "That hyperlink goes to " & target.LinkUri
End If
We can test if we right clicked on a hyperlink (an <a href=" element) and then get the details of that link, either the link itself or the text that is shown to the user for that link.
In our case we just display the URL into the info area.
If target.HasSelection Then
InfoText.Text = "Selected text = " & target.SelectionText
End If
InfoText.Text = InfoText.Text & vbCrLf & "Kind: " & KindAsString(target.Kind)
End Sub
If the user selected text before right clicking then we can act on that, which in our case is just display the selected text.
You can of course create a special menu item for that selected text.
The last line in the sub shows target.Kind as a string.
Private Sub loadString()
Dim Html As String
Dim TextLine As String
Dim HtmlFile As String
Dim Path As String
' Load the html from file contextMenu.html
Path = strGetVisualBasicDemoPath
HtmlFile = Path & "\html\customContextMenu.html"
Open HtmlFile For Input As #1
Do Until EOF(1)
Line Input #1, TextLine
Html = Html & TextLine
Loop
Close #1
EdgeWebBrowser.NavigateToString Html
End Sub
We're loading the entire page of html into a string and then Navigate to that string.
Private Sub EdgeWebBrowser_OnCreateWebviewCompleted(ByVal HResult As Long)
loadString
InfoText.Text = "This info area will show some of the context menu data."
End Sub
We're loading the html when the control is done creating.
Private Sub Form_Load()
On Error Resume Next
Me.Show
EdgeWebBrowser.CreateWebView
Form_Resize
End Sub
Private Sub Form_Resize()
On Error Resume Next
EdgeWebBrowser.Width = Me.ScaleWidth - 200
EdgeWebBrowser.Height = Me.ScaleHeight - (InfoText.Height + 180) - 100
InfoText.Width = Me.ScaleWidth - 200
InfoText.Top = EdgeWebBrowser.Height + 180
End Sub
Here's the code that get's executed for the "OnClick" on each menu item via the OnCustomItemSelected event.
Private Sub BackMenuItem_OnCustomItemSelected()
InfoText.Text = "Back menu item was clicked."
EdgeWebBrowser.GoBack
End Sub
Private Sub HelpMenuItem_OnCustomItemSelected()
InfoText.Text = "Help menu item was clicked."
End Sub
Private Sub ReloadMenuItem_OnCustomItemSelected()
InfoText.Text = "Reload menu item was clicked."
loadString
End Sub
AntView - The MS Edge WebView2 ActiveX control Date last changed: 11/05/2025