Walkthrough: Adding a Command to the Solution Explorer Toolbar (C#)

This walkthrough details the steps that are required to add a button to the Visual StudioSolution Explorer toolbar.

注意

Starting in 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 Visual Studio Command Table (.Vsct) Files.

Any command, whether on a toolbar or on a menu, is considered a button by Visual Studio. When the button is clicked, the code in the command handler is executed. Typically, related commands are grouped together to form a single group. Menus or toolbars act as containers for groups. Priority determines the order in which individual commands in a group appear in the menu or on the toolbar. You can prevent a button from being displayed on the toolbar or the menu by controlling its visibility. A command that is listed in the VISIBILITY section of the .ctc file appears only in the associated context. The visibility cannot be applied to groups.

For more information about menus, toolbar commands, and .ctc files, see Menus and Toolbars.

Prerequisites

This walkthrough requires installation of the Visual Studio Industry Partner (VSIP) SDK. When you perform certain steps in this walkthrough, information is written to the experimental registry hive for Visual Studio.

Creating a VSPackage That Contains a Single Menu Command

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

To create the MySolutionToolbarPkg VSPackage

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

  2. In the New Project dialog box, select Other Project Types and then select Extensibility on the Project types list.

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

  4. Type the name of your project, for example, MySolutionToolbarPkg. The rest of this walkthrough assumes that name.

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

  6. Click the Next button on the Welcome to the Visual Studio Integration Package Wizard page.

  7. On the Select a Programming Language page, select Visual C# and accept the default for the key file. Click Next.

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

  9. On the Select VSPackage Options page, select Menu Command and then click Next.

  10. On the Command Options page, set the Command Name to My Button Test Command and set the Command ID to cmdidMyTestCmd. Click Finish.

  11. After the Package wizard finishes creating the project, continue to the steps for adding a button to the Solution Explorer toolbar.

Adding a Button to the Solution Explorer Toolbar

This section of the walkthrough demonstrates how to add a single button to the Solution Explorer toolbar. Then you click the button to execute the code that is contained in your callback method.

To add a button to the Solution Explorer toolbar

  1. In Solution Explorer, expand the CtcComponents folder in the MySolutionToolbarPkg project to find the CommandIds.h file. Right-click CommandIds.h and then click Open to open it in a text editor.

  2. In the Menu Group IDs section, add a definition for MySolutionToolbarGroup:

    #define MySolutionToolbarGroup     0x1090
    
  3. Click Save and then close the CommandIds.h file.

  4. Also in the CtcComponents folder in Solution Explorer, right-click MySolutionToolbarPkg.ctc and then click Open to open it in a text editor.

  5. In the NEWGROUPS_BEGIN section, add the following line after the existing group entry. This defines the new group that contains commands for the Solutions Explorer toolbar.

    // Any command added to this group appears on the toolbar.
    guidMySolutionToolbarPkgCmdSet:MySolutionToolbarGroup, // Group ID
    guidSHLMainMenu:IDM_VS_TOOL_PROJWIN,                   // Toolbar ID
    0xF000;                                                // Priority
    
  6. In the CMDPLACEMENT_SECTION section, add the following line. This adds the command that you created in the previous section, My Button Test Command, to the toolbar group. By default, the toolbar does not appear if it has no commands associated with it.

    guidMySolutionToolbarPkgCmdSet:cmdidMyTestCmd,         // Command ID
    guidMySolutionToolbarPkgCmdSet:MySolutionToolbarGroup, // Parent ID
    0xF000;                                                // Priority
    
  7. Click Save and then close the MySolutionToolbarPkg.ctc file.

  8. On the Build menu, click Build Solution.

  9. Run the MySolutionToolbarPkg project by pressing F5.

    This opens the Visual Studio experimental build.

    注意

    Two versions of Visual Studio are open now.

  10. Inthe Visual Studio experimental build, on the View menu, click Solution Explorer.

    The toolbar should display the new command button to the right of the existing buttons. The button icon is the numeral 1 in a square.

  11. Click the new button.

    You should see a dialog box that has the message, "Inside Company.MySolutionToolbarPkg.MySolutionToolbarPkg.MenuItemCallback()".

    注意

    To add multiple buttons to the same group, add Command ID, Parent ID and Priority for each button in the CMDPLACEMENT section. If you use the same Command ID for two or more buttons, those buttons are ignored by the build process as duplicates.

Controlling the Visibility of a Button

This section of the walkthrough demonstrates how to control the visibility of a button on a toolbar. By setting a context to single project or multiple projects in the VISIBILITY_SECTION section of MySolutionToolbarPkg.ctc file, you restrict a button to appear either when a single project is open or when multiple projects are open.

To display a button when either a single project is open or multiple projects are open

  1. If you have not already done it, close the Visual Studio experimental build.

  2. In Solution Explorer, expand the CtcComponents folder in the MySolutionToolbarPkg project to find the MySolutionToolbarPkg.ctc file. Right-click MySolutionToolbarPkg.ctc and then click Open to open it in a text editor.

  3. In the BUTTONS_BEGIN section, replace the existing line that defines the command that has the following line. The DEFAULTINVISIBLE and DYNAMICVISIBILITY flags must be set so that entries in the VISIBILITY section can take effect.

    guidMySolutionToolbarPkgCmdSet:cmdidMyTestCmd,    // Command ID
    guidMySolutionToolbarPkgCmdSet:MyMenuGroup,       // Parent ID
    0x0100,                                           // Priority
    guidMySolutionToolbarPkgCmdSet:bmpPic1,           // Image
    BUTTON,                                           // Type
    DEFAULTINVISIBLE | DYNAMICVISIBILITY,             // Flags
    "My Button Test Command";                         // Command name
    
  4. In the VISIBILITY_SECTION section, add the following lines to set the context to a single and multiple projects.

    guidMySolutionToolbarPkgCmdSet:cmdidMyTestCmd,     // Command ID
    UICONTEXT_SolutionHasSingleProject;                // Single Project ID
    guidMySolutionToolbarPkgCmdSet:cmdidMyTestCmd,     // Command ID
    UICONTEXT_SolutionHasMultipleProjects;             // Multiple Projects ID
    
  5. Save the MySolutionToolbarPkg.ctc file and then close it.

  6. On the Build menu, click Build Solution.

  7. Run the MySolutionToolbarPkg project by pressing F5. This opens the Visual Studio experimental build.

    注意

    Two versions of Visual Studio are open now.

  8. Inthe Visual Studio experimental build, open Solution Explorer. The toolbar does not contain the button that has the numeral 1.

  9. Open any solution that contains a project or multiple projects.

    The button that has the numeral 1 appears on the toolbar to the right of the existing buttons.

  10. On the File menu, click Close Solution. The button disappears from the toolbar.

    注意

    The visibility of the button is controlled by Visual Studio until the VSPackage is loaded. After the VSPackage is loaded, the visibility of the button is controlled by the VSPackage.

See Also

Concepts

Menus and Toolbars

Menu and Toolbar Command Walkthroughs

Command Table Configuration (.Ctc) Files

Command Table Format Reference