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 the
Internet Explorer standard context menu by using browser helper objects.
- Deploys the custom Internet Explorer context menu
- 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.
Collapse this imageExpand this image
To download this code sample, click one of the following links:
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:
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 =
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.
// Set the value of Name.
// 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
- 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;
- 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.
public partial class CustomIEContextMenuInstaller : System.Configuration.Install.Installer
public override void Install(System.Collections.IDictionary stateSaver)
RegistrationServices regsrv = new RegistrationServices();
throw new InstallException("Failed To Register for COM");
public override void Uninstall(System.Collections.IDictionary savedState)
RegistrationServices regsrv = new RegistrationServices();
throw new InstallException("Failed To Unregister for COM");
- Create a setup project to deploy the Internet Explorer context menu.
For more information about how to create and deploy the sample application, see the Readme.txt file that is included in the download package.
This code sample is available in the following programming languages:
Collapse this tableExpand this table
For more information about how to add entries to the standard context menu, visit the following MSDN website:
For more information about context menus and extensions, visit the following MSDN website:
For more information about browser helper objects, visit the following MSDN website:
What is All-In-One Code Framework?
All-In-One Code Framework shows most Microsoft development techniques by using code samples in different programming languages. Each example is carefully selected, composed, and documented to show one common code scenario. For more information about All-In-One Code Framework, visit the following Microsoft website:
How to find more All-In-One Code Framework samples
To find more All-In-One Code Framework samples, search for "kbcodefx" together with related keywords on the Microsoft support Web site. Or, visit the following Microsoft website:
Rapid publishing disclaimer
Microsoft corporation and/or its respective suppliers make no representations about the suitability, reliability, or accuracy of the information and related graphics contained herein. All such information and related graphics are provided "as is" without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information and related graphics, including all implied warranties and conditions of merchantability, fitness for a particular purpose, workmanlike effort, title and non-infringement. You specifically agree that in no event shall Microsoft and/or its suppliers be liable for any direct, indirect, punitive, incidental, special, consequential damages or any damages whatsoever including, without limitation, damages for loss of use, data or profits, arising out of or in any way connected with the use of or inability to use the information and related graphics contained herein, whether based on contract, tort, negligence, strict liability or otherwise, even if Microsoft or any of its suppliers has been advised of the possibility of damages.
Article ID: 2618576 - Last Review: September 19, 2011 - Revision: 1.0
|kbcodefx kbinfo kbnomt kbsurveynew kbrapidpub atdownload KB2618576|