Customize Internet Explorer main menu,
add IE context menu commands: VB.NET, C#

Add-in Express™
for Internet Explorer® and Microsoft® .net

Add-in Express Home > Add-in Express for Internet Explorer > Online Guide > Customizing IE main menu and context menus

Customizing IE main menu and context menus

Add-in Express allows customizing any top-level item of the Internet Explorer main menu and adding custom context menu items in IE6, IE7, IE8, IE9, IE10 and IE11.

Customizing Internet Explorer main menu

You add an ADXIEMainMenu component onto the IE module and use the property editor of the ADXIEMainMenu.Items property to add a custom item to a specified top-level menu item (of the ADXIEMainMenuItem type). The required steps are listed below.

Right-click the designer surface of the IE module and choose Add Main Menu in the context menu.

Adding a Main Menu component onto the IE module

This adds an ADXIEMainMenu component onto the IE module.

Main Menu component added to the IE module

In the Properties window, open the property editor of the ADXIEMainMenu.Items property.

Property editor of the ADXIEMainMenu.Items property

In the property editor window, add an ADXIEMainMenuItem specifying a top-level item in the BuiltinMenu property; top-level menu items are File, Edit, etc.

Customizing the Help item of the main IE menu

Select the top-level item, add an ADXIEMenuItem and set its properties. In this sample project, we set the properties as shown below:

Adding an IE main menu item component and setting its properties

Now you can handle the OnClick event of the ADXIEMenuItem. The sample code below demonstrates showing / hiding an IE bar. The same approach is applicable for an Internet Explorer toolbar.


			Imports System.Windows.Forms
			Imports AddinExpress.IE
			Dim flag As Boolean = False
			Private Sub AdxieMenuItem1_OnClick(sender As System.Object, _
					eventObject As System.Object) _
					Handles AdxieMenuItem1.OnClick
				Dim dummy As Object = Nothing
				Dim show As Object = flag
				Dim guid As Object = Type.GetType("MyIEAddon1.MyIEBar1").GUID.ToString("B")
				IEApp.ShowBrowserBar(guid, show, dummy)
				flag = Not flag
			End Sub

Note that specifying the ADXIEMenuItem.Shortcut property requires setting the HandleShortcuts property of the module to true.

When IE creates its main menu, ADXIEMainMenu receives the OnCreateMenu event. Then all top-level menu items receive the OnCreatePopupMenu event. Also, just before a menu item is created and shown, it receives the OnCreateMenuItem event.

And finally. It's not possible to create a custom top-level menu item.

Custom item in Help menu

Your custom item added to the Help menu of Internet Explorer can look similar to this:

A custom item added to the Internet Explorer Help menu

Customizing IE context menus

To create a custom context menu item, add an item to the ContextMenu collection of the IEModule and specify the item's properties: caption and contexts available for the context menu item.

Naturally, you need to handle the Click event of the context menu item. Pay attention to the Contexts and ExtendedContexts properties of top-level context menu items; you can use these properties for refining the context-sensitivity of your context menu items. Say, if you choose Anchor in the Contexts property, you will be able to choose Anchor and Link in ExtendedContexts. In the same fashion, you can limit your context menu items to any given HTML control type(s).

When Internet Explorer creates its context menu for the first time, the OnCreateContextMenu event of the IE module is raised; you can use this event for any sort of initialization. Before a given context menu item is created, it receives the OnCreateContextMenuItem event; at this moment you can change its visibility, caption, position, etc.

The sample code below shows how to get the right-clicked and selected elements:


			Imports System.Windows.Forms
			Imports AddinExpress.IE
			Private Sub AdxieMenuItem2_OnClickEx(sender As System.Object, _
				e As ADXIEMenuItemClickEventArgs) Handles AdxieMenuItem2.OnClickEx

				Dim result As String = ""
				Dim selection As mshtml.IHTMLSelectionObject = _
					CType(e.Selection, mshtml.IHTMLSelectionObject)
				If selection IsNot Nothing Then
					Dim textRange As mshtml.IHTMLTxtRange = _
						TryCast(selection.createRange(), mshtml.IHTMLTxtRange)
					If textRange IsNot Nothing Then
						If Not String.IsNullOrEmpty(textRange.text) Then
							result = "Selection: " + textRange.text + vbCrLf
							result = "Selection: " + textRange.htmlText + vbCrLf
						End If
					End If
				End If

				Dim element As mshtml.IHTMLElement = CType(e.HTMLElement, mshtml.IHTMLElement)
				If element IsNot Nothing Then
					result += "Element: " + element.innerText
				End If
				MessageBox.Show(New MyWinApiWindow(Me.ParentHandle), result)
			End Sub

The MyWinApiWindow type above is a simple class implementing the System.Windows.Forms.IWin32Window interface (see below). The window handle for MyWindow is supplied by the ParentHandle property of the module that returns the handle (hwnd) of the tab window. Note that you must show messages in this way; otherwise your message box may be shown behind the IE window.


			Public Class MyWinApiWindow
				Implements System.Windows.Forms.IWin32Window 

				Dim theHandle As IntPtr 

				Public Sub New(ByVal aHandle As System.IntPtr)
					theHandle = aHandle 
				End Sub

				Public ReadOnly Property Handle() As System.IntPtr _
					Implements System.Windows.Forms.IWin32Window.Handle
						Return theHandle
					End Get
				End Property
			End Class


			class MyWinApiWindow: System.Windows.Forms.IWin32Window
				System.IntPtr theHandle;

				public MyWinApiWindow(System.IntPtr aHandle)
					theHandle = aHandle;

				#region IWin32Window Members

				IntPtr System.Windows.Forms.IWin32Window.Handle
					get { return theHandle; }


Custom item added to IE context menu

Your custom item in the context menu of Internet Explorer can look like this:

Custom items in an IE context menu

Creating custom IE toolbars <<

>> Building Explorer bars