Add custom menu item to IE context menu

You can add custom menu item to IE standard context menu to do some custom actions. For example you may want to add an item to open an executable program and do something based on selected text.

As an option to add custom menu item to IE context menu, you can follow these steps:

  1. Open registry and go to:
    HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt
    
  2. Create a new key, and set the name of key to the text you want displayed in the context menu as the name, for example: Open My App

  3. Right click on (Default) and choose Modify... and set the value to path of an html file which will contains some scripts to do a custom operation for you. For example: C:\OpenMyApp.html

  4. Add a new DWORD value named Context and set it’s value to hexadecimal 11 or decimal 17. This way, the context menu will be shown when you select a text or by default, but no when you right click on a button for example. To see more options take a look at documentation. Also in documentations it’s said to add binary but I tried DWORD instead and it worked. Also other extensions that I’ve seen use DWORD. Here is the list of available values based on the documentation:

    Context Value
    Default 0x1
    Images 0x2
    Controls 0x4
    Tables 0x8
    Text selection 0x10
    Anchor 0x20
  5. Use this content for your C:\OpenMyApp.html:
    <script type="text/javascript">
        var win = window.external.menuArguments;
        var selection = win.getSelection();
        alert(selection);
    </script>
    

Now if you select a text and right-click on the text, you will see Open My App in context menu:

Add custom menu item to IE context menu

If you want to open an application, it’s enough to add some script to open the application for you. Your application should be installed on client and if you are going to use the selected text in your application, then your application should handle command line arguments by accepting string[] args as input parameters for Main entry point or using Environment.GetCommandLineArgs().

The script to open an application should be like this:

    <script type="text/javascript">
        var win = window.external.menuArguments;
        var selection = win.getSelection();
        var oShell = new ActiveXObject("Shell.Application");
        var commandtoRun = "C:\\MyApp.exe"; 
        oShell.ShellExecute(commandtoRun,"\"" + selection + "\"","","open","1");
    </script>

You May Also Like

About the Author: Reza Aghaei

I’ve been a .NET developer since 2004. During these years, as a developer, technical lead and architect, I’ve helped organizations and development teams in design and development of different kind of applications including LOB applications, Web and Windows application frameworks and RAD tools. As a teacher and mentor, I’ve trained tens of developers in C#, ASP.NET MVC and Windows Forms. As an interviewer I’ve helped organizations to assess and hire tens of qualified developers. I really enjoy learning new things, problem solving, knowledge sharing and helping other developers. I'm usually active in .NET related tags in stackoverflow to answer community questions. I also share technical blog posts in my blog as well as sharing sample codes in GitHub.

1 Comment

  1. Awesome post, I’ve been for something like this , , , , forever, lol

    Do you happen to know if there’s a way to create submenu/cascades, like this ? can’t seem to find anything searching.
    Thanks in advance

Leave a Reply

Your email address will not be published. Required fields are marked *