You are currently offline, waiting for your internet to reconnect

How to add to the standard context menus of the WebBrowser control

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
This article contains information that supplements the "Controlling theContext Menus" section in the "Reusing the Web Browser Control & MSHTML"overview in the Internet Client SDK. Specifically, this article describesoptional registry keys to control the context in which added items areshown as well as a description of the context menu event object.
More information
Here is the full complete text of the section "Adding to the StandardContext Menus":

Adding to the Standard Context Menus

Items can be added to the existing context menus of the WebBrowser controlby placing entries in the registry and linking these to URLs that executescript. To add additional items to the standard WebBrowser context menus,create or open the following key:
   HKEY_CURRENT_USER       Software           Microsoft               Internet Explorer                   MenuExt				
Under this key, you create a key (not a value) whose name contains the textyou want displayed in the menu. The default value for this key contains theURL that will be executed. The key name can include the "&" character,which will cause the character immediately following the "&" to beunderlined. The URL will be loaded inside of a hidden HTML dialog box, allof the inline script will be executed, and the dialog box will be closed.The hidden HTML dialog box's external.menuArguments property contains thewindow object of the window on which the context menu item was executed.

The following registry entry will add an item with the title "My Menu Item"to the WebBrowser context menu and will execute the inline script containedin the file "c:\myscript.htm."
   HKEY_CURRENT_USER       Software           Microsoft               Internet Explorer                   MenuExt                       My Menu Item = "file://c:\myscript.htm"				
The following are the contents of "c:\myscript.htm."
   <SCRIPT LANGUAGE="JavaScript" defer>   var parentwin = external.menuArguments;   var doc = parentwin.document;   var sel = doc.selection;   var rng = sel.createRange();   var str = new String(rng.text);   if(str.length == 0)      rng.text = "MY INSERTED TEXT";   else      rng.text =  str.toUpperCase();   </SCRIPT>				
This script obtains the parent window object from external.menuArguments.The parent window object is the WebBrowser control in which the contextmenu item was executed. The script then obtains the current selection and,if no selection is present, inserts the text "MY INSERTED TEXT" at thepoint where the context menu was executed. If there is a selection present,the selected text is changed to uppercase.

Optional Keys

Under the item registry key created earlier, there are a couple of optionalvalues. One of these specifies on which context menus this item willappear. The other specifies that the script should be run as a dialog box.

The "Contexts" DWORD value specifies the context menus in which an itemwill appear. It is a bit mask consisting of the logical OR of the followingvalues (defined in mshtmhst.h). These values correspond to the constantpassed in a IDocHostUIHandler::ShowContextMenu call.
(0x1 << CONTEXT_MENU_DEFAULT) (evaluates to 0x1)(0x1 << CONTEXT_MENU_IMAGE) (evaluates to 0x2)(0x1 << CONTEXT_MENU_CONTROL) (evaluates to 0x4)(0x1 << CONTEXT_MENU_TABLE) (evaluates to 0x8)(0x1 << CONTEXT_MENU_TEXTSELECT) (evaluates to 0x10)(0x1 << CONTEXT_MENU_ANCHOR) (evaluates to 0x20)(0x1 << CONTEXT_MENU_UNKNOWN) (evaluates to 0x40)				
So for example, if you wanted your simple extension to appear only in thedefault menu and the text selection menu, you could create a DWORD value inthe registry under the "My Menu Item" key called "Contexts" and set it to0x11. From C/C++ code, this can be expressed as follows:
The other optional registry DWORD value is "Flags." There is only one bit(0x1) valid for this registry value, and it is defined asMENUEXT_SHOWDIALOG in Mshtmhst.h. When this bit is set, the script is runjust as if it had been called through the ShowModalDialog method. Thewindow that runs the script is not hidden and the dialog boxis notautomatically closed after inline and onload script finishes. Theexternal.menuArugments value still contains the window object where theuser selected the menu item.

The Context Menu Event

Whenever a context menu extension is triggered, the event object off of themain window (external.menuArguments.event) contains information about wherethe user clicked and which context menu was shown. The mouse coordinatesare valid along with event.srcElement. The event.type value contains one ofthe following strings, indicating which context menu was shown to the user:
  • MenuExtDefault
  • MenuExtImage
  • MenuExtControl
  • MenuExtTable
  • MenuExtTextSelect
  • MenuExtAnchor
  • MenuExtUnknown

Another Example

This example will create a new context menu item on just the default menu.This item, called "Show in New Window" will start whatever is clicked on ina new window. So, If something is deeply nested in a frameset, you caneasily launch a specific frame in its own window. Here are the contents ofa .reg file that can be run to insert the correct registry settings. Callthis Example2.reg. Double-clicking on this file in the Explorer will insertthe settings in your registry. REGEDIT4
   [HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt\Show in   &New Window]   @="file://c:\\example2.htm"   "Contexts"=dword:00000001				
Here are the contents of "C:\Example2.htm."
   <SCRIPT LANGUAGE="JavaScript" defer>      open(external.menuArguments.location.href);   </script>				
Internet Client SDK: Internet Tools & Technologies; Reusing the Web BrowserControl and MSHTML; Overview

Article ID: 177241 - Last Review: 06/22/2014 18:38:00 - Revision: 4.0

  • kbhowto KB177241