Add a menu to the Visual Studio menu bar
Applies to: Visual Studio Visual Studio for Mac Visual Studio Code
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.
Starting in Visual Studio 2019, top level menus contributed by extensions are placed under the Extensions menu.
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
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.
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 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
In Solution Explorer, open TopLevelMenuPackage.vsct.
At the end of the file, there is a
<Symbols>node that contains several
<GuidSymbol>nodes. In the node named
guidTopLevelMenuPackageCmdSet, add a new symbol, as follows:
<IDSymbol name="TopLevelMenu" value="0x1021"/>
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="guidTopLevelMenuPackageCmdSet" id="TopLevelMenu" priority="0x700" type="Menu"> <Parent guid="guidSHLMainMenu" id="IDG_VS_MM_TOOLSADDINS" /> <Strings> <ButtonText>Test Menu</ButtonText> </Strings> </Menu> </Menus>
idvalues of the menu specify the command set and the specific menu in the command set.
idvalues of the parent position the menu on the section of the Visual Studio menu bar that contains the Tools and Add-ins menus.
<ButtonText>element specifies that the text should appear in the menu item.
<Groups>section, find the
<Group>and change the
<Parent>element to point to the menu we just added:
<Groups> <Group guid="guidTopLevelMenuPackageCmdSet" id="MyMenuGroup" priority="0x0600"> <Parent guid="guidTopLevelMenuPackageCmdSet" id="TopLevelMenu"/> </Group> </Groups>
This makes the group part of the new menu.
<Buttons>section, find the
<Button>node. Then, in the
<Strings>node, change the
Notice that the Visual Studio Package template has generated a
Buttonelement that has its parent set to
MyMenuGroup. As a result, this command appears on your menu.
Build and test the extension
Build the project and start debugging. An instance of the experimental instance should appear.
The Extensions menu in the experimental instance should contain a Test Menu menu.
On the Test Menu menu, select Test Command.
A message box should appear and display the message "TestCommand Inside TopLevelMenu.TestCommand.MenuItemCallback()".