How to: Create Basic Menus

注意

Beginning with Visual Studio 2008 SDK, use XML Command Table (.vsct) files instead of command table configuration (.ctc) files to define how menus and commands appear in your VSPackages. For more information, see XML-Based Command Table Configuration (.vsct) Files.

VSPackages cannot directly add menus to the Visual Studio integrated development environment (IDE), but must make use of the Visual Studio SDK architecture for command group menus. Command group menus support reusing and sharing commands between components and the environment For an overview on command group menus, see How VSPackages Add User Interface Elements.

For VSPackages, menus are defined in the MENUS_BEGIN – MENUS_END section of a .ctc file (as described in Command Table Configuration (.Ctc) Files). There are three basic components of a .ctc file: menus, groups, and commands. Commands are what you select to perform a function. Groups are containers for commands and menus. Menus are containers for groups. Therefore, for a basic menu, it is necessary create a menu, a command group, and at least one command.

There are three basic ways that a menu can appear in Visual Studio:

  1. As a top level menu on the main menu bar.

  2. As a submenu of a top level menu.

  3. As a context or shortcut menu.

The procedure given here creates a menu that can be used as a top level menu or as a submenu of another menu. For context menus, see How to: Create Context Menus.

The following walkthroughs show how to create basic menus in both managed (Visual C#) and unmanaged (Visual C++) code:

To Create a Simple Menu

  1. If your project does not already have a Command Table Configuration (.Ctc) Files, add one to your project.

  2. Select a GUID:ID pair to represent the new menu command. The GUID represents a command set defined for your VSPackage. Your VSPackage may define multiple command sets.

    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 number. The GUID:ID pair is used to identify the menu and it must be unique.

  3. Define the new menu in the MENUS_BEGIN – MENUS_END section of the .ctc file:

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

    2. Set the Group ID field to the menu's parent group or to a place holder if the menu is to be positioned by command placement (see the CMDPLACEMENT_SECTION – CMDPLACEMENT_END section).

      A placeholder value for a parent group is, by convention, the same GUID:ID as the Menu ID field or GUID:0, depending on the menu (this is discussed in detail in the MENUS_BEGIN – MENUS_END section).

      A typical approach is to create a new group in your .ctc file (see the NEWGROUPS_BEGIN – NEWGROUPS_END section) and use that as the parent group for your menu. Then the parent group for your new group can be the IDE top level menu bar or one of the IDE menus. All you have to change is the parent group of your new group to move your menu to a different location.

      For example, to add your menu to the top level menu bar of the IDE, set the parent of your new group to guidSHLMainMenu:IDM_VS_TOOL_MAINMENU.

      To add your menu to the Tools menu of the IDE, set the parent group of your new group to guidSHLMainMenu:IDM_VS_TOOLS.

      For your menu to be visible, its parent group must be directly or indirectly connected to a menu or the top level menu bar in the IDE.

    3. Set the Priority field.

      The priority field is used by the .ctc to determine the menu's location among the other objects in the parent group.

      Commands with low priority values are displayed before commands with high priority values. Duplicate priority values are permitted, but the relative position of commands with equal priority is determined by the order in which VSPackages are processed for menus and that order is cannot be predetermined.

    4. The Type field should be left blank for normal menus.

      There is no explicit "menu" type, although certain flags can be set in the Type field. For basic menus, no flags are needed so the Type field is left blank. For a list of valid Type flags see the Type field discussion in the MENUS_BEGIN – MENUS_END section.

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

      The value of the Menu Name field appears as the name of the menu in the Customize dialog box. The Menu Name field is also used as part of the command name when accessing the commands on the menu through the Command Window. For details, see the discussion of the Menu Name field in the MENUS_BEGIN – MENUS_END section.

    6. Set the Menu Text field to the display name of the menu.

      The Menu Text field is what the user selects to open the menu. The text can include an '&' character to create a mnemonic character that can allow the menu to be selected by holding the Alt key down and pressing the mnemonic character.

  4. Create a new group to contain the commands that are to appear on your 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 menu you created in step 3. This places the group of commands on the menu.

  5. Add commands to the 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 4.

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

  6. 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 By Using the Binary Command Table Compiler.

See Also

Concepts

Command Table Format Reference

NEWGROUPS_BEGIN – NEWGROUPS_END

BUTTONS_BEGIN – BUTTONS_END

How to: Create and Handle Commands in VSPackages (C#)