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
Open Visual Studio. On the File menu, point to New and then click Project.
In the New Project dialog box, select Other Project Types and then select Extensibility on the Project types list.
In the Templates list, select Visual Studio Integration Package.
Type the name of your project, for example, MySolutionToolbarPkg. The rest of this walkthrough assumes that name.
Click OK to open the Visual Studio Integration Package Wizard.
Click the Next button on the Welcome to the Visual Studio Integration Package Wizard page.
On the Select a Programming Language page, select Visual C# and accept the default for the key file. Click Next.
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.
On the Select VSPackage Options page, select Menu Command and then click Next.
On the Command Options page, set the Command Name to My Button Test Command and set the Command ID to cmdidMyTestCmd. Click Finish.
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
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.
In the Menu Group IDs section, add a definition for MySolutionToolbarGroup:
#define MySolutionToolbarGroup 0x1090
Click Save and then close the CommandIds.h file.
Also in the CtcComponents folder in Solution Explorer, right-click MySolutionToolbarPkg.ctc and then click Open to open it in a text editor.
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
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
Click Save and then close the MySolutionToolbarPkg.ctc file.
On the Build menu, click Build Solution.
Run the MySolutionToolbarPkg project by pressing F5.
This opens the Visual Studio experimental build.
注意
Two versions of Visual Studio are open now.
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.
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
If you have not already done it, close the Visual Studio experimental build.
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.
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
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
Save the MySolutionToolbarPkg.ctc file and then close it.
On the Build menu, click Build Solution.
Run the MySolutionToolbarPkg project by pressing F5. This opens the Visual Studio experimental build.
注意
Two versions of Visual Studio are open now.
Inthe Visual Studio experimental build, open Solution Explorer. The toolbar does not contain the button that has the numeral 1.
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.
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
Menu and Toolbar Command Walkthroughs