Walkthrough: Adding a Submenu to a Menu
This walkthrough builds on the demonstration in Walkthrough: Adding a Menu to the Visual Studio Menu Bar (C#) by showing how to add a submenu to the TestMenu menu.
A submenu is a secondary menu that appears in another menu. A submenu can be identified by the arrow that follows its name. Clicking the name causes the submenu and its commands to be displayed.
This walkthrough creates a submenu in a menu on the Visual Studio menu bar and puts a new command on the submenu. The walkthrough also implements the new command.
Prerequisites
To complete this walkthrough, you must install the Visual Studio 2010 SDK.
注意
For more information about the Visual Studio SDK, see Visual Studio Integration SDK. To find out how to download the Visual Studio SDK, see Visual Studio Extensibility Developer Center on the MSDN Web site.
Locations for the Visual Studio Package Project Template
The Visual Studio Package project template is available in three locations in the New Project dialog box:
Under Visual Basic Extensibility. The default language of the project is Visual Basic.
Under C# Extensibility. The default language of the project is C#.
Under Other Project Types Extensibility. The default language of the project is C++.
Creating a VSPackage
To create the TopLevelMenu VSPackage
Follow the steps in Walkthrough: Adding a Menu to the Visual Studio Menu Bar (C#) to create the menu to which functionality is added in this walkthrough.
The steps in this walkthrough assume that the name of the VSPackage is TopLevelMenu, which is the name that is used in Walkthrough: Adding a Menu to the Visual Studio Menu Bar (C#).
Adding a Submenu to a Menu
To add a submenu to a menu
In Solution Explorer, open TopLevelMenu.vsct.
In the <Symbols> section, add an <IDSymbol> element for the submenu, one for the submenu group, and one for the command, all in the <GuidSymbol> node named "guidTopLevelMenuCmdSet." This is the same node that contains the <IDSymbol> element for the top-level menu.
<IDSymbol name="SubMenu" value="0x1100"/> <IDSymbol name="SubMenuGroup" value="0x1150"/> <IDSymbol name="cmdidTestSubCommand" value="0x0105"/>
Add the newly created submenu to the <Menus> section.
<Menu guid="guidTopLevelMenuCmdSet" id="SubMenu" priority="0x0100" type="Menu"> <Parent guid="guidTopLevelMenuCmdSet" id="MyMenuGroup"/> <Strings> <ButtonText>Sub Menu</ButtonText> <CommandName>Sub Menu</CommandName> </Strings> </Menu>
The GUID/ID pair of the parent specifies the menu group that was generated in Walkthrough: Adding a Menu to the Visual Studio Menu Bar (C#), and is a child of the top-level menu.
Add the menu group defined in step 2 to the <Groups> section and make it a child of the submenu.
<Group guid="guidTopLevelMenuCmdSet" id="SubMenuGroup" priority="0x0000"> <Parent guid="guidTopLevelMenuCmdSet" id="SubMenu"/> </Group>
Add a new <Button> element to the <Buttons> section to define the command created in step 2 as an item on the submenu.
<Button guid="guidTopLevelMenuCmdSet" id="cmdidTestSubCommand" priority="0x0000" type="Button"> <Parent guid="guidTopLevelMenuCmdSet" id="SubMenuGroup" /> <Icon guid="guidImages" id="bmpPic2" /> <Strings> <CommandName>cmdidTestSubCommand</CommandName> <ButtonText>Test Sub Command</ButtonText> </Strings> </Button>
注意
In .vsct files, the <Button> element is a generic construct that is used to represent buttons, menu items, and any other user interface (UI) elements that a user can click to execute a command.
In Solution Explorer, right-click TopLevelMenu.vsct and then click Rebuild.
Doing this builds the .vsct 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.)
Press F5 to open an instance of the Visual Studio experimental environment in debug mode.
Click TestMenu to see a new submenu named Sub Menu. Click Sub Menu to open the submenu and see a new command, Test Sub Command. Notice that clicking Test Sub Command does nothing.
注意
You must close the experimental Visual Studio before you continue to the next section.
Adding a Command
To add support for a command in managed code
Open PkgCmdID.cs or PkgCmdID.vb in the code editor.
Add the following command ID after the existing command ID in the PkgCmdIDList class definition.
public const int cmdidTestSubCmd = 0x105;
Open TopLevelMenuPackage.cs or TopLevelMenuPackage.vb in the code editor.
Find the hidden region labeled Package Members and expand it by clicking the plus sign in the left margin.
Find the Initialize method and add the following lines just after the call to the AddCommand method. The symbol SubItemCallback will be undefined until after step 6.
CommandID subCommandID = new CommandID( GuidList.guidTopLevelMenuCmdSet, (int)PkgCmdIDList.cmdidTestSubCmd); MenuCommand subItem = new MenuCommand( new EventHandler(SubItemCallback), subCommandID); mcs.AddCommand(subItem);
At the end of the class, immediately after the MenuItemCallback method, add the following method. This is the method that is called when the new command in the submenu is clicked.
private void SubItemCallback(object sender, EventArgs e) { IVsUIShell uiShell = (IVsUIShell)GetService( typeof(SVsUIShell)); Guid clsid = Guid.Empty; int result; uiShell.ShowMessageBox( 0, ref clsid, "My Top Level Menu Package", string.Format(CultureInfo.CurrentCulture, "Inside {0}.SubItemCallback()", this.ToString()), string.Empty, 0, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST, OLEMSGICON.OLEMSGICON_INFO, 0, out result); }
On the Build menu, click Build Solution to build the solution.
Press F5 to open an instance of the experimental Visual Studio.
On the TestMenu menu, click Sub Menu and then click Test Sub Command. A message box should appear and display the text, "Inside Company.TopLevelMenu.TopLevelMenuPackage.SubItemCallback()".
See Also
Tasks
Walkthrough: Adding a Menu to the Visual Studio Menu Bar (C#)