Condividi tramite


Add a menu to the Visual Studio menu bar

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

This walkthrough shows how to add a menu to the menu bar of the Visual Studio integrated development environment (IDE). The IDE menu bar contains menu categories such as File, Edit, View, Window, and Help.

Before adding a new menu to the Visual Studio menu bar, consider whether your commands should be placed within an existing menu. For more information about command placement, see Menus and commands for Visual Studio.

Menus are declared in the .vsct file of the project. For more information about menus and .vsct files, see Commands, menus, and toolbars.

By completing this walkthrough, you can create a menu named Test Menu that contains one command.

Prerequisites

Starting in Visual Studio 2015, you do not install the Visual Studio SDK from the download center. It is included as an optional feature in Visual Studio setup. You can also install the VS SDK later on. For more information, see Install the Visual Studio SDK.

Create a VSIX project that has a custom command item template

  1. Create a VSIX project named TopLevelMenu. You can find the VSIX project template in the New Project dialog by searching for "vsix". For more information, see Create an extension with a menu command.
  1. When the project opens, add a custom command item template named TestCommand. In the Solution Explorer, right-click the project node and select Add > New Item. In the Add New Item dialog, go to Visual C# / Extensibility and select Custom Command. In the Name field at the bottom of the window, change the command file name to TestCommand.cs.

Create a menu on the IDE menu bar

  1. In Solution Explorer, open TestCommandPackage.vsct.

    At the end of the file, there is a <Symbols> node that contains several <GuidSymbol> nodes. In the node named guidTestCommandPackageCmdSet, add a new symbol, as follows:

    <IDSymbol name="TopLevelMenu" value="0x1021"/>
    
  2. Create an empty <Menus> node in the <Commands> node, just before <Groups>. In the <Menus> node, add a <Menu> node, as follows:

    <Menus>
          <Menu guid="guidTestCommandPackageCmdSet" id="TopLevelMenu" priority="0x700" type="Menu">
            <Parent guid="guidSHLMainMenu"
                    id="IDG_VS_MM_TOOLSADDINS" />
            <Strings>
              <ButtonText>Test Menu</ButtonText>
            </Strings>
        </Menu>
    </Menus>
    

    The guid and id values of the menu specify the command set and the specific menu in the command set.

    The guid and id values of the parent position the menu on the section of the Visual Studio menu bar that contains the Tools and Add-ins menus.

    The <ButtonText> element specifies that the text should appear in the menu item.

  3. In the <Groups> section, find the <Group> and change the <Parent> element to point to the menu we just added:

    <Groups>
        <Group guid="guidTestCommandPackageCmdSet" id="MyMenuGroup" priority="0x0600">
            <Parent guid="guidTestCommandPackageCmdSet" id="TopLevelMenu"/>
        </Group>
    </Groups>
    

    This makes the group part of the new menu.

  1. In the <Buttons> section, find the <Button> node. Then, in the <Strings> node, change the <ButtonText> element to Test Command.

    Notice that the Visual Studio Package template has generated a Button element that has its parent set to MyMenuGroup. As a result, this command appears on your menu.

Build and test the extension

  1. Build the project and start debugging. An instance of the experimental instance should appear.
  1. The menu bar in the experimental instance should contain a Test Menu menu.
  1. On the Test Menu menu, select Test Command.

    A message box should appear and display the message "TestCommand Inside TopLevelMenu.TestCommand.MenuItemCallback()".

See also