The sample demonstrates how to open an image in a new tab by using a customized Internet Explorer context menu. This sample includes the following features:
- Adds entries to the Internet Explorer standard context menu.
- Overrides theInternet Explorer standard context menu by using browser helper objects.
- Deploys the custom Internet Explorer context menu
Notes
- Internet Explorer 8 enables a new approach that is called accelerator or activity to add an entry to an Internet Explorer context menu. The new approach is easier to use accelerators to copy information from one webpage to another. However, adding entries to the Internet Explorer standard context menu is still a good approach to do work locally.
- If you override the Internet Explorer standard context menu in Browser Helper Object (BHO), use one add-in application at a time to override the IDocHostUIHandler interface. If you use multiple add-in applications, the add-in applications can easily conflict with one another. You can also create a user-defined web browser to set the IDocHostUIHandler interface.
- The Internet Explorer Enhanced Security Configuration (IE ESC) setting is enabled by default on Windows Server 2008 or on Windows Server 2008 R2. Therefore, Internet Explorer cannot load the extensions.
Note You must manually disable the IE ESC setting.
Difficulty level

Download information
To download this code sample, click one of the following links:
Technical overview
To use a customized Internet Explorer context menu to open an image in a new tab, follow these steps:
- Add entries to the Internet Explorer standard context menu.
To add an entry to the Internet Explorer standard context menu, you must add a key under the following registry entry: HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt\
Note The default value of the key is the html file path that handles the event.
The OpenImageMenuExt class provides two methods to add and remove entries in the registry. Also, you can use the Resource.htm or OpenImage.htm html page i to open an image in a new tab:private const string IEMenuExtRegistryKey = "Software\\Microsoft\\Internet Explorer\\MenuExt"; public static void RegisterMenuExt() { // If the key exists, CreateSubKey will open it. RegistryKey ieMenuExtKey = Registry.CurrentUser.CreateSubKey( IEMenuExtRegistryKey + "\\Open image in new tab"); // Get the path of Resource\OpenImage.htm. FileInfo fileIofo = new FileInfo(Assembly.GetExecutingAssembly().Location); string path = fileIofo.Directory.FullName + "\\Resource\\OpenImage.htm"; // Set the default value of the key to the path. ieMenuExtKey.SetValue(string.Empty, path); // Set the value of Name. ieMenuExtKey.SetValue("Name", "Open_Image"); // Set the value of Contexts to indicate which contexts your entry should // appear in the standard context menu by using a bit mask consisting of // the logical OR of the following values: // Default 0x1 // Images 0x2 // Controls 0x4 // Tables 0x8 // Text selection 0x10 // Anchor 0x20 ieMenuExtKey.SetValue("Contexts", 0x2); ieMenuExtKey.Close(); }
- Override the Internet Explorer standard context menu by using browser helper objects.
The OpenImageHandler class implements the IDocHostUIHandler interface, and the OpenImageHandler class also overrides the default context menu.The OpenImageBHO class is a browser helper object that sets the penImageHandler classes the UIHandler class of the html document. For more information about how to create and deploy BHO, see the CS/VBBrowserHelperObject sample in Microsoft All-In-One Code Framework.InternetExplorer explorer = pDisp as InternetExplorer; // Set the handler of the document in InternetExplorer. if (explorer != null) { NativeMethods.ICustomDoc customDoc = (NativeMethods.ICustomDoc)explorer.Document; customDoc.SetUIHandler(openImageDocHostUIHandler); }
- Deploy the custom context menu together with a setup project.
To deploy the custom context menu together with a setup project, follow these steps:- Add an installer class (known as CustomIEContextMenuInstaller in the code sample) to define the custom actions in the CSCustomIEContextMenu setup project. The installer class derives from the System.Configuration.Install.Installer class. The code sample uses the custom actions to add and remove the Internet Explorer context menu entries in a registry. Then, the code sample registers or unregisters the COM-visible classes in current managed assembly when user installs or uninstalls the component.
[RunInstaller(true), ComVisible(false)] public partial class CustomIEContextMenuInstaller : System.Configuration.Install.Installer { public CustomIEContextMenuInstaller() { InitializeComponent(); } public override void Install(System.Collections.IDictionary stateSaver) { base.Install(stateSaver); OpenImageMenuExt.RegisterMenuExt(); RegistrationServices regsrv = new RegistrationServices(); if (!regsrv.RegisterAssembly(this.GetType().Assembly, AssemblyRegistrationFlags.SetCodeBase)) { throw new InstallException("Failed To Register for COM"); } } public override void Uninstall(System.Collections.IDictionary savedState) { base.Uninstall(savedState); OpenImageMenuExt.UnRegisterMenuExt(); RegistrationServices regsrv = new RegistrationServices(); if (!regsrv.UnregisterAssembly(this.GetType().Assembly)) { throw new InstallException("Failed To Unregister for COM"); } } }
- Create a setup project to deploy the Internet Explorer context menu.
- Add an installer class (known as CustomIEContextMenuInstaller in the code sample) to define the custom actions in the CSCustomIEContextMenu setup project. The installer class derives from the System.Configuration.Install.Installer class. The code sample uses the custom actions to add and remove the Internet Explorer context menu entries in a registry. Then, the code sample registers or unregisters the COM-visible classes in current managed assembly when user installs or uninstalls the component.
Note For more information about how to create and deploy the sample application, see the Readme.txt file that is included in the download package.
Technology category
Windows base
Languages
This code sample is available in the following programming languages:
Language | Project Name |
---|---|
Visual C# | CSCustomIEContextMenu |
Visual Basic.NET | VBCustomIEContextMenu |
References
For more information about how to add entries to the standard context menu, visit the following MSDN website:
How to add entries to the standard context menu
For more information about context menus and extensions, visit the following MSDN website:
General information about context menus and extensions
For more information about browser helper objects, visit the following MSDN website: