How to use a customized Internet Explorer context menu to open an image in a new tab

Article translations Article translations
Article ID: 2618576
Expand all | Collapse all

On This Page

INTRODUCTION

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

Collapse this imageExpand this image
codefx Medium

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:
  1. 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();
            }
  2. 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);
                }
     
  3. Deploy the custom context menu together with a setup project.
    To deploy the custom context menu together with a setup project, follow these steps:
    1. 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");
                  }
              }
          }
    2. Create a setup project to deploy the Internet Explorer context menu. 
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:
Collapse this tableExpand this table
LanguageProject Name
Visual C#CSCustomIEContextMenu
Visual Basic.NETVBCustomIEContextMenu

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:
General information about browser helper objects

MORE INFORMATION

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:
http://1code.codeplex.com

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:
All-In-One Code Framework samples
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.

Properties

Article ID: 2618576 - Last Review: September 19, 2011 - Revision: 1.0
Keywords: 
kbcodefx kbinfo kbnomt kbsurveynew kbrapidpub atdownload KB2618576

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com