Walkthrough: Creating a Cascading Submenu

注意

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.

This walkthrough builds on the Walkthrough: Creating a Top Level Menu (C#) walkthrough by guiding you through the steps of adding a cascading submenu to the My Test Menu menu.

A cascading submenu is a popup menu that appears inside another menu as a name with an arrow pointing to the right. Selecting the name causes the submenu to pop up, that gives access to the commands in that submenu.

This walkthrough creates a submenu in a top level menu and places a new command on the submenu. This walkthrough also implements the new command.

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

Prerequisites

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.

Creating a VSPackage

To create the MyTopLevelMenuPackage VSPackage

  • Follow the procedures described in Walkthrough: Creating a Top Level Menu (C#) to create the top level menu to which additional functionality is being added to here.

    注意

    If you are going to use Visual C++ as the language for your VSPackage, the procedures described in Walkthrough: Creating a Top Level Menu (C#) can be used with the exception of selecting Visual C++ as the programming language. In this walkthrough, to add a submenu, adding a command to a Visual C++ VSPackage is a very different process than adding a command to a Visual C# VSPackage. Therefore, two procedures are provided for adding a command: one for Visual C# and the other for Visual C++. However, the process of adding the submenu itself is the same for both programming languages.

    The rest of the procedures in this walkthrough assume the VSPackage name of MyTopLevelMenuPackage, which is the name used in the Walkthrough: Creating a Top Level Menu (C#) walkthrough.

Adding a Submenu to a Menu

To add a submenu to a 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 MySubMenu after the existing menu IDs:

    #define MySubMenu 0x1100
    
  3. In the Menu Group IDs section, add the definition for MySubMenuGroup after the existing group IDs:

    #define MySubMenuGroup 0x1150
    
  4. In the Command IDs section, add the cmdidMyTestSubCmd after the existing command ID. This provides a command to put on the submenu:

    #define cmdidMyTestSubCmd 0x105
    
  5. In the CtcComponents folder in the MyTopLevelMenuPackage project, right-click MyTopLevelMenuPackage.ctc and then select Open.

  6. Find the MENUS_BEGIN section and add the following lines after the existing menu entry. This defines the submenu.

    guidMyTopLevelMenuPackageCmdSet:MySubMenu,               // Menu ID
        guidMyTopLevelMenuPackageCmdSet:MyTopLevelMenuGroup, // Parent Group
        0x0100,                                              // Priority
        ,                                                    // A normal menu
        "My &Sub Menu";                                      // Menu text
    
  7. Find the NEWGROUPS_BEGIN section and add the following lines after the existing group entries. This defines the submenu's group and adds it to the top level menu after the existing command.

    // Any command added to this group is placed in the submenu.
    guidMyTopLevelMenuPackageCmdSet:MySubMenuGroup,  // Group ID
        guidMyTopLevelMenuPackageCmdSet:MySubMenu,   // Menu ID
        0x0000;                                      // Priority
    
  8. Find the BUTTONS_BEGIN section and add the following lines after the existing command entry. This defines the new command on the submenu.

    guidMyTopLevelMenuPackageCmdSet:cmdidMyTestSubCmd,  // Command ID
        guidMyTopLevelMenuPackageCmdSet:MySubMenuGroup, // Parent Group
        0x0000,                                         // Priority
        guidMyTopLevelMenuPackageCmdSet:bmpPicSmile,    // Icon ID
        BUTTON,                                         // Button Type
        ICONANDTEXT,                                    // Flags
        "&My Test Sub Command";                         // Button Text
    
  9. In Solution Explorer, right-click MyTopLevelMenuPackage and click Rebuild.

    This builds 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).

  10. To test the display of the submenu, open an instance of the experimental Visual Studio with one of the following steps:

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

    • From the Start Menu, select the experimental Visual Studio shortcut; for example, Visual Studio 2008 Experimental. This shortcut is added when the Visual Studio SDK is installed.

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

  11. Open the My Test Menu to see a new submenu called My Sub Menu. Open the My Sub Menu to see a new command, My Test Sub Command. Note that selecting the new command does nothing as yet.

    注意

    You must close the experimental Visual Studio before continuing to the next step.

    Continue to the Adding a Command procedure to add support for the "My Test Sub Command" menu command.

Adding a Command

The following procedure assumes you created the VSPackage in Visual C#. For a VSPackage created in Visual C++, see the procedure To add support for a command in unmanaged code.

To add support for a command in managed code

  1. In Solution Explorer, right-click PkgCmdID.cs, and then select Open to open it in the code editor.

  2. Add the following command ID after the existing command ID in the PkgCmdID.cs file:

    public const int cmdidMyTestSubCmd = 0x105;
    
  3. In Solution Explorer, right-click VsPkg.cs in MyTopLevelMenuPackage project and then click Open to open it in the code editor.

  4. Locate the hidden region labeled Package Members and expand it by clicking on the plus sign in the left margin.

  5. Find the Initialize method and add the following lines right after the call to the AddCommand method.

    CommandID subCommandID = new CommandID(GuidList. 
      guidMyTopLevelMenuPackageCmdSet,                                     
      (int)PkgCmdIDList.cmdidMyTestSubCmd);
    MenuCommand subItem = new MenuCommand( new 
      EventHandler(SubItemCallback),
      subCommandID);
    mcs.AddCommand(subItem);
    
  6. In VsPkg.cs, 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 selected.

    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);
    }
    
  7. Click Build Solution on the Build menu to build the solution.

  8. Open an instance of the experimental Visual Studio with one of the following steps:

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

    • From the Start Menu, select the experimental Visual Studio shortcut; for example, Visual Studio 2008 Experimental. This shortcut is added when the Visual Studio SDK is installed.

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

  9. Select My Test Sub Command from the My Sub Menu submenu in the My Test Menu. A message box should appear displaying the text "Inside Company.MyTopLevelMenuPackage.MyTopLevelMenuPackage.SubItemCallback()".

To add support for a command in unmanaged code

  1. In Solution Explorer, expand the Source Files folder in the MyTopLevelMenuPackage project to find the VsPkg.cpp file. Right-click on the VSPkg.cpp file and select Open to open it in a text editor.

  2. Find the CMyTopLevelMenuPackagePackage::QueryStatus method and add the following lines to the switch statement, just before the default statement. This enables the new command:

                case cmdidMyTestSubCmd:
                    cmdf = OLECMDF_SUPPORTED | OLECMDF_ENABLED;
                    break;
    
  3. Find the CMyTopLevelMenuPackagePackage::Exec method and add the following lines to the switch statement, just before the default statement. This connects the new command to a command handler (to be added in the next step).

                case cmdidMyTestSubCmd:
                    hr = OnMySubCommandClicked();
                    break;
    
  4. At the end of the VsPkg.cpp file, add the following method to handle the new command. This method displays a message box.

    HRESULT CMyTopLevelMenuPackagePackage::OnMySubCommandClicked()
    {
        HRESULT hr = S_OK;
        TCHAR szTitle[256];
        if (0 == ::LoadString(_AtlBaseModule.GetResourceInstance(),
                              IDS_PROJNAME,
                              szTitle,
                              countof(szTitle)))
        {
            hr = E_OUTOFMEMORY;
        }
        else
        {
            CComPtr<IVsUIShell> srpUiShell;
            hr = _AtlModule.QueryService(SID_SVsUIShell,
                                         IID_IVsUIShell,
                                         (void **)&srpUiShell);
            if (SUCCEEDED(hr))
            {
                // Show Message Box to prove we were here
                LONG lResult;
                hr = srpUiShell->ShowMessageBox(
                    0,
                    CLSID_NULL,
                    T2OLE(szTitle),
                    T2OLE(_T("Inside CMyTopLevelMenuPackagePackage::OnMySubCommandClicked()")),
                    NULL,
                    0,
                    OLEMSGBUTTON_OK,
                    OLEMSGDEFBUTTON_FIRST,
                    OLEMSGICON_INFO,
                    0,
                    &lResult);
            }
        }
        return(hr);
    }
    
  5. In Solution Explorer, open the Header Files folder in the MyTopLevelMenuPackage project to find the VsPkg.h file. Right-click on the VSPkg.h file and select Open to open it in a text editor.

  6. At the end of the CMyTopLevelMenuPackagePackage class, after the last variable in the private section, add the following method declaration:

        STDMETHOD(OnMySubCommandClicked)();
    
  7. Select Build Solution from the Build menu to build the solution.

  8. Open an instance of the experimental Visual Studio with one of the following steps:

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

    • From the Start Menu, select the experimental Visual Studio shortcut; for example, Visual Studio 2008 Experimental. This shortcut is added when the Visual Studio SDK is installed.

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

  9. Select My Test Sub Command from the My Sub Menu submenu in the My Test Menu. A message box should appear displaying the text "Inside CMyTopLevelMenuPackagePackage::OnMySubCommandClicked()".

See Also

Concepts

Menu and Toolbar Command Walkthroughs

Walkthrough: Creating a Top Level Menu (C#)

Menus and Toolbars