Walkthrough: Adding a Toolbar to the IDE
This walkthrough guides you through the steps of adding a toolbar to the Visual Studio integrated development environment (IDE).
A toolbar is a small, horizontal or vertical window that contains buttons bound to commands. Depending on its implementation, a toolbar in the IDE can be repositioned, docked on any side of the IDE, or made to stay in front of all other windows.
In addition, commands can be added to or removed from a toolbar through the Customize dialog box. The toolbar supplied by a VSPackage typically is a default toolbar that the user can customize. The IDE handles all customization, while the VSPackage only needs to respond to commands. The VSPackage does not need to know where a command is physically located.
For more information on menus and their related .ctc files, see Menus and Toolbars.
This section demonstrates how to use the Visual Studio Integration Package wizard to create a VSPackage that supports a single menu command. The information is written to the experimental registry hive for Visual Studio.
To create the MyToolbarPackage VSPackage
-
Open Visual Studio. On the File menu, point to New, and the click Project.
-
In the Project types pane of the New Project dialog box, expand the Other Project Types node, and then click Extensibility.
-
In the Templates pane, select Visual Studio Integration Package.
-
In the Name box, type MyToolbarPackage. Select a destination folder for the project.
-
Click OK to open the Visual Studio Integration Package Wizard.
-
Read 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.
You also can use the Visual C++ programming language for your VSPackage. The steps in this walkthrough work for both languages.
-
On the Basic VSPackage Information page, accept the defaults.
-
On the Select VSPackage Options page, select the Menu Command check box.
-
On the Command Options page, set the Command Name to My Toolbar Test Command and set the Command ID to cmdidMyTestCmd.
-
Click Finish. After the wizard finishes building the project, the status bar displays that the MyToolbarPackage solution has built successfully.
To create a toolbar for the IDE
-
In Solution Explorer, expand the CtcComponents folder and right-click the CommandIds.h file, and then click Open to open it in a text editor.
-
In the Menu IDs section, add a definition for MyToolbar:
#define MyToolbar 0x1000
-
In the Menu Group IDs section, add a definition for MyToolbarGroup:
#define MyToolbarGroup 0x1050
-
In Solution Explorer, right-click the MyToolbarPackage.ctc file, and then click Open to open it in a text editor.
-
In the MENUS_BEGIN section, add the following lines to define the toolbar:
guidMyToolbarPackageCmdSet:MyToolbar, // Menu ID guidMyToolbarPackageCmdSet:MyToolbar, // Parent Group 0x0000, // Priority TOOLBAR | DEFAULTDOCKED, // Type and Flags "My IDE Toolbar"; // Toolbar NameToolbars cannot be nested like submenus, so the parent group of a toolbar is, by convention, set to the same GUID:ID pair as the toolbar itself. Typically, initial placement of a toolbar is defined programmatically, but thereafter, changes by the user are persisted.
-
In the NEWGROUPS_BEGIN section, after the existing group entry, add the following lines to define the group that contains the commands for the toolbar:
// Any command added to this group appears on the toolbar. guidMyToolbarPackageCmdSet:MyToolbarGroup, // Group ID guidMyToolbarPackageCmdSet:MyToolbar, // Menu ID 0x0000; // Priority -
In the CMDPLACEMENT_SECTION section, type the following lines to add the existing command to the toolbar group so the toolbar will be displayed (by default, if a toolbar has no commands, it does not appear):
guidMyToolbarPackageCmdSet:cmdidMyTestCmd, // Command ID guidMyToolbarPackageCmdSet:MyToolbarGroup, // Parent ID 0x0000; // Priority -
Save the project, then on the Build menu, click Build Solution. Correct any errors that might arise.
-
Open an instance of the experimental Visual Studio by pressing F5 or selecting Start on the Debug menu. This runs the experimental Visual Studio under the debugger and allows debugging of your VSPackage.
-
Right-click the IDE menu bar, and then select My IDE Toolbar from the list of toolbars.
-
Click the icon on the new toolbar to see a dialog box with the message "Inside Company.MyToolbarPackage.MyToolbarPackage.MenuItemCallback()".