Saturday, August 23, 2008

How to Add Context Menu Item to Internet Explorer Browser.

This article describes about how to add custom context menu item to Internet Explorer Browser.This essentialy involves registry modifications.This can be done either manually or programatically.Manually means opening system registry & editing manually.Programatically in the sense writing code to edit registry.For programatic editing i'l present C# coding.
Context Menu:
Context menu is menu which will be opened on mouse right buttom click on a window.
Note:To understand this article better a basic understanding of system registry is essential.
Manul Way:
Internet Explorer has its own default context menu.If one wants to add custom menu item to this default menu,microsoft is providing a way to do this.This typically requires 'System registry' modifications.To add a context item open system registry.For this follow below navigation.

Start --> Run --->regedit
Then registry will be opened.In registry navigate to following path 'HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt'
Under 'MenuExt' key add a new key.The name of this key will be the name of menu item in Internet Explorer.For instance say 'MyMenuItem'.Once key is added a default string value is added to this key.The Value of the this default string should be the path of the Script file which will be executed when 'MyMenuItem' is clicked.For instance say 'C:\MyMenuItem.htm'This may be .js file or .htm file.Once this is done the menu item will appear in Internet Explorer Context Menu.
Now 'MyMenuItem' item will be appear in IE context menu.One can perform what ever the action needed by adding appropriate script in sript file.The window can be accessed from the script as follows.
//gets the window
var window=external.menuArguments ;
//to get the document object
var doc=window.document ;
//perform action .....

This is how a context menu item can be added to IE manually.If context menu item to work for specific items like Selected text,image or links, this can be done by specifying 'Context' value for 'MyMenuItem' key in registry.
To Add content specific context menu item to IE, Add 'DWORD' type value to 'MyMenuItem' key & name it 'Context'.The value of the 'Context' decides to which content the menu item is available.
The valid values of 'Context' & their corresponding contents are given below.
  1. 0x1 (Hex) -- this default
  2. 0x2 (hex)-- for images (means this context menu item available for only images )
  3. 0x4 " ---for controls
  4. 0x8 " --- for tables
  5. 0x10 ---for selected text
  6. 0x20 ---for Anchors(links)

Note:before editing Registry ensure that Registry back up is taken.

For More Info about Customising IE Check http://msdn.microsoft.com/en-us/library/bb735853(VS.85).aspx

Programatic Way:

.net framework provides built in API to work with system registry.

Note:Make sure that namespace 'Microsoft.Win32' is accessible to your code

RegistryKey registryKey;
registryKey = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Internet Explorer\MenuExt");

//Adding registry key 'MyMenuItem'
RegistryKey addSourceKey = registryKey.CreateSubKey("MyMenuItem");

//Set Default string value to Path of the script file
addSourceKey.SetValue("", "C:\\MyMenuItem.htm");
addSourceKey.Close();
registryKey.Close();

No comments: