This documentation is archived and is not being maintained.

How to: Create Context Menus

Visual Studio 2005
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Context menus are text menus that are displayed when the user right-clicks in an active region of the client area and disappears when the right mouse button is released.

The first procedure describes how to create a context menu.

The second procedure describes how to invoke the context menu from managed code.

The Walkthrough: Implementing a Context Menu in a Tool Window walkthrough shows how to create a context menu in a tool window in managed (Visual C#) code.

To create a context menu

  1. Select a GUID:ID pair to represent the new menu. The ctc.exe compiler expects a GUID data type in this format:

    #define guidCmd  { 0xBC8DA515, 0x5743, 0x4FEB, { 0xA9, 0x29, 0x29, 0x38, 0x24, 0x9C, 0xBA, 0x26 } }

    The pair definition continues with a colon and finally a unique number. The GUID:ID pair must be unique, and is used to identify the menu.

  2. Create a new line in the MENUS_BEGIN – MENUS_END section of the Command Table Configuration (.Ctc) Files.

    1. Set the Menu ID field to the GUID:ID of the new menu.

    2. Set the Group ID field to GUID:0, where the GUID is the same GUID used in the Menu ID field.

      Context menus typically do not have parent menus so the Group ID is, set to GUID:0, by convention. However, context menus can be used as submenus and submenus can be used as context menus (if the submenu is defined in the .ctc file has having the menu type of Context; when used as a submenu, a context menu is treated as a normal menu). In these cases, the Group ID field can be set to the GUID:ID of a parent menu; when the submenu is used as a context menu, the Group ID field is ignored. For more information on how to create submenus, see How to: Create Cascading Submenus.

    3. Set the Priority field to zero.

      A context menu does not need a priority value as it is always displayed by itself.

    4. Set the Type field to Context.

      As appropriate, add the following flags to the Type field, using the | (logical OR) operator:

      AlwaysCreate – if you want the context menu to be created even if it contains no commands.

      NoCustomize – if you do not want the context menu to be modified through the Customize dialog box (available from the Tools menu). All context menus can be viewed and optionally modified by the user by showing the Shortcut Menus toolbar from the Customize dialog box.

    5. Set the Menu Name field to the name of the context menu, enclosed in quotes.

    6. Set the Menu Text field to the display name of the context menu, enclosed in quotes.

      This name is what appears in the Shortcut Menus toolbar when the context menu is being customized.

      If the Menu Text field is not specified, the Menu Name field is used.

  3. Create a new group to contain the commands that are to appear on your context menu.

    See the NEWGROUPS_BEGIN – NEWGROUPS_END section for details on creating new groups.

    Set the parent for this new group to be the GUID:ID of the context menu you created in step 2. This places the group of commands on the context menu.

  4. Add commands to the context menu by creating command entries in the BUTTONS_BEGIN – BUTTONS_END section of the .ctc file and setting the Group ID field for each command to the GUID:ID of the group created in step 3.

    Use the Priority field of each command entry to specify the order in which the commands appear in the group. This affects the order of the commands in the context menu.

  5. Using the command table compiler (ctc.exe), convert your .ctc file to a binary resource that can be included in your VSPackage satellite dll file. For information on the Command Table Compiler, see How to: Create Menu Commands for a VSPackage Using the Binary Command Table Compiler.

To invoke the context menu from managed code

  • In your program, display the context menu in response to a right mouse button click.

    A context menu can be displayed using the ShowContextMenu method in the OleMenuCommandService interface, which is the preferred approach. An older approach is to call the ShowContextMenu method in the IVsUIShell interface (which can be obtained from the SVsUIShell service).

    The following example shows how to display a context menu at a specified point from a managed code program using the managed package framework (MPF). The point is obtained from the mouse click handler and must be in screen coordinates (if the right-click occurs in a control derived the Microsoft .NET Framework Control class, use the PointToScreen method to convert the click position to a screen position).

    using System.ComponentModel.Design; // for IMenuCommandService
    using Microsoft.VisualStudio.Shell; // for OleMenuCommandService, ServiceProvider
    private void DisplayContextMenuAt(System.Drawing.Point point,
                                      ServiceProvider serviceProvider)
        // guidMyPackageCommandSet:MyContextMenu is the GUID:ID pair
        // for the context menu.
        CommandID contextMenuID = new CommandID(GuidList.guidMyPackageCommandSet,
        OleMenuCommandService menuService;
        menuService = serviceProvider.GetService(typeof(IMenuCommandService))
                      as OleMenuCommandService;
        if (null != menuService)
                 // Note: point must be in screen coordinates!
                 menuService.ShowContextMenu(contextMenuID, point.X, point.Y);
            catch(Exception e)
                Debug.Assert(false, "Failed to show context menu: " + e.Message);

See Also