HOWTO: Adding to the Standard Context Menus of the WebBrowser Control

ID: Q177241


The information in this article applies to:


SUMMARY

This article contains information that supplements the "Controlling the Context Menus" section in the "Reusing the Web Browser Control & MSHTML" overview in the Internet Client SDK. Specifically, this article describes optional registry keys to control the context in which added items are shown 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 Standard Context Menus":

Adding to the Standard Context Menus

Items can be added to the existing context menus of the WebBrowser control by placing entries in the registry and linking these to URLs that execute script. 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 text you want displayed in the menu. The default value for this key contains the URL that will be executed. The key name can include the "&" character, which will cause the character immediately following the "&" to be underlined. The URL will be loaded inside of a hidden HTML dialog box, all of the inline script will be executed, and the dialog box will be closed. The hidden HTML dialog box's external.menuArguments property contains the window 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 contained in 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 context menu item was executed. The script then obtains the current selection and, if no selection is present, inserts the text "MY INSERTED TEXT" at the point 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 optional values. One of these specifies on which context menus this item will appear. The other specifies that the script should be run as a dialog box.

The "Contexts" DWORD value specifies the context menus in which an item will appear. It is a bit mask consisting of the logical OR of the following values (defined in mshtmhst.h). These values correspond to the constant passed 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 the default menu and the text selection menu, you could create a DWORD value in the registry under the "My Menu Item" key called "Contexts" and set it to 0x11. From C/C++ code, this can be expressed as follows:

   (0x1 << CONTEXT_MENU_DEFAULT) | (0x1 << CONTEXT_MENU_TEXTSELECT) 
The other optional registry DWORD value is "Flags." There is only one bit (0x1) valid for this registry value, and it is defined as MENUEXT_SHOWDIALOG in Mshtmhst.h. When this bit is set, the script is run just as if it had been called through the ShowModalDialog method. The window that runs the script is not hidden and the dialog boxis not automatically closed after inline and onload script finishes. The external.menuArugments value still contains the window object where the user selected the menu item.

The Context Menu Event

Whenever a context menu extension is triggered, the event object off of the main window (external.menuArguments.event) contains information about where the user clicked and which context menu was shown. The mouse coordinates are valid along with event.srcElement. The event.type value contains one of the following strings, indicating which context menu was shown to the user:

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 in a new window. So, If something is deeply nested in a frameset, you can easily launch a specific frame in its own window. Here are the contents of a .reg file that can be run to insert the correct registry settings. Call this Example2.reg. Double-clicking on this file in the Explorer will insert the 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> 


REFERENCES

Internet Client SDK: Internet Tools & Technologies; Reusing the Web Browser Control and MSHTML; Overview

Additional query words:


Keywords          : kbIE400 kbIE401 kbIE500 AXSDKWebBrowser 
Version           : WINDOWS:4.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 17, 1999