This documentation is archived and is not being maintained.

Walkthrough: Creating a Top Level Menu (C#)

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

This walkthrough guides you through the steps of adding a menu to the top level menu of the Visual Studio integrated development environment (IDE).

The top level menu of the IDE contains menu items such as File, Edit, View, Window, and Help. By convention, VSPackages add any top level menus just before the Window menu. This is done by setting the priority of the VSPackage's menu to a value between 0x300 and 0xFE00.

This walkthrough creates a single top level menu called My Test Menu that contains a single command. Without the command, the top level menu does not normally appear. A menu can be forced to appear even if it has no commands by setting the AlwaysCreate flag in the menu's definition in the Command Table (.vsct) File for a Data View, however, this is not recommended practice.

For more information on menus and the .ctc file, see Menus and Toolbars.

This walkthrough requires the Visual Studio SDK to be installed. The result of this walkthrough writes information to the experimental registry hive for Visual Studio.

This section of the walkthrough demonstrates how to create a VSPackage that has a single menu command using the Visual Studio Integration Package wizard.

To create the MyTopLevelMenuPackage VSPackage

  1. Open Visual Studio. On the File menu, point to New and then click Project.

  2. In the New Project dialog box, expand Other Project Types and then click Extensibility in the Project types list.

  3. In the Templates list, click Visual Studio Integration Package.

  4. Type in the name of your project; for example, MyTopLevelMenuPackage (the rest of this walkthrough assumes that name). Select a destination for the project.

  5. Click OK to open the Visual Studio Integration Package Wizard.

  6. Read the Welcome to the Visual Studio Integration Package Wizard page then click Next.

  7. In the Select a Programming Language page, click Visual C# and accept the default for the key file, and then click Next.

  8. In the Basic VSPackage Information page, you can change whatever details you want. For this example, accept the defaults and click Next.

  9. In the Select VSPackage Options page, check Menu Command and then click Next.

  10. In the Command Options page, set Command Name to My Test Command and Command ID to cmdidMyTestCmd, and then click Finish.

    After a few moments, the Visual Studio Package wizard finishes building the project. When the status bar displays that the MyTopLevelMenuPackage solution built successfully, proceed to Creating a Top Level Menu.

To create a top level menu

  1. In Solution Explorer, expand the CtcComponents folder in the MyTopLevelMenuPackage project, right-click CommandIDs.h, and then select Open to open it in a text editor.

  2. In the Menu IDs section, add the definition for MyTopLevelMenu:

    #define MyTopLevelMenu 0x1000
    
  3. In the Menu Groups IDs section, add the definition for MyTopLevelMenuGroup and VSTopLevelGroup after the existing group ID.

    #define MyTopLevelMenuGroup 0x1030
    #define VSTopLevelGroup     0x1040
    

    MyTopLevelMenuGroup contains the commands that appear in the VSPackage's top level menu. VSTopLevelGroup contains the top level menu so that it can be positioned in the Visual Studio top level menu bar.

  4. In the CtcComponents folder in the MyTopLevelMenuPackage project, right-click MyTopLevelMenuPackage.ctc and then select Open.

  5. In the MENUS_BEGIN section add the following lines:

    guidMyTopLevelMenuPackageCmdSet:MyTopLevelMenu,      // Menu ID
        guidMyTopLevelMenuPackageCmdSet:VSTopLevelGroup, // Parent Group
        0x0000,                                          // Priority
        ,                                               // A normal menu
        "&My Test Menu";                                 // Menu text
    

    This defines the top level menu to be added.

  6. In the NEWGROUPS_BEGIN section, add the following lines after the existing group.

    // Any command added to this group is placed in the top level menu.
    guidMyTopLevelMenuPackageCmdSet:MyTopLevelMenuGroup, // Group ID
        guidMyTopLevelMenuPackageCmdSet:MyTopLevelMenu   // Menu ID
        0x0000;                                          // Priority
    
    // Any menu added to this group is placed in the top level menu bar.
    guidMyTopLevelMenuPackageCmdSet:VSTopLevelGroup,     // Group ID
        guidSHLMainMenu:IDM_VS_TOOL_MAINMENU,            // Menu ID
        0xE000;                                          // Priority
    

    These define the two command groups.

  7. In the CMDPLACEMENT_SECTION, add the following lines:

    guidMyTopLevelMenuPackageCmdSet:cmdidMyTestCmd,          // Command ID
        guidMyTopLevelMenuPackageCmdSet:MyTopLevelMenuGroup, // Group ID
        0x0000;                                              // Priority
    

    This places the existing menu command in the new top level menu. This way, the command exists in both the top level menu and in the Tools menu.

  8. On the Build menu, click Build Solution.

    This rebuilds the .ctc file with the changes. Correct any errors that may occur during building (the most common error is using the wrong case for a GUID label or a command ID; GUID labels and command IDs are always case-sensitive).

  9. Open an instance of the experimental Visual Studio using any one of the following methods:

    • At the Visual Studio command prompt, type devenv /rootsuffix exp.

    • On the Start Menu, select the experimental Visual Studio shortcut, Microsoft Visual Studio 2005 Experimental. This shortcut is added when the VSIP SDK is installed.

    • Press F5 or select Start on the Debug menu (this runs the experimental Visual Studio under the debugger and allows debugging of your VSPackage).

  10. Look at the top level menu in the experimental Visual Studio to find My Test Menu positioned just before the Window top level menu.

  11. Open My Test Menu and select My Test Command.

    A message box appears with a message that includes "Inside Company.MyTopLevelMenuPackage.MyTopLevelMenuPackage.MenuItemCallback()", indicating that the new command works.

  12. On the Tools menu, click My Test Command.

    The same message box appears, demonstrating that the same command is placed in two different menus.

Show: