How to: Create Toolbars for Tool Windows
You can add a toolbar to a tool window by defining a menu of type ToolWindowToolbar in a VSPackage .vsct file, and then programming the package to display it.
Toolbars in tool windows differ from toolbars in the general integrated development environment (IDE) in the following ways:
An IDE toolbar can be created just by defining a menu of type Toolbar in the .vsct file. A tool window toolbar must be programmatically created in the target tool window.
A user can configure an IDE toolbar by using the Customize dialog box. A tool window toolbar cannot be configured in this manner.
A tool window toolbar is first defined as a Menu Element in the .vsct file.
The following procedures assume that you have a VSPackage project in which a tool window is defined in the .vsct file. If you are creating a VSPackage, in the package wizard, select Tool Window to add a .vsct file to the package project and define a tool window.
To create a tool window toolbar
Open the .vsct file in the editor.
Locate the section that is formed by the Symbols Element.
Find the GuidSymbol Element that contains your menus, groups, and commands. By default, it is named guid<ProjectName>CmdSet.
Add an IDSymbol Element for your toolbar, and one for a group to hold your commands, as shown in the following example.
The name attributes of the GuidSymbol and IDSymbol elements in a defined toolbar, command, or group provide a GUID:ID pair that represents that toolbar, command, or group. The GUID represents a command set that is defined for your VSPackage. Multiple command sets may be defined in the package. The GUID:ID pair that is used to identify each item must be unique.
Define the toolbar as a Menu Element in the Menus section, as follows:
Set the guid and id attributes to the GUID:ID of the new toolbar.
Set the type attribute to ToolWindowToolbar.
In the Menu element, use the Strings Element to create a section that contains a ButtonText Element and a CommandName Element. The ButtonText element is used to set the name of the toolbar as it appears in the IDE. The CommandName element is used to set the command name that can be typed in the Command window to access the toolbar.
The following example defines a tool window toolbar.
In the Groups section, create a Group Element to contain the commands that you want to appear on your toolbar.
Set the priority of the group to determine where it will appear on your toolbar.
A group that has a low priority setting will appear on the left side of the toolbar (or the top, depending on how the toolbar is oriented).
Set the parent of the group to the GUID:ID of the toolbar.
The following group appears on the toolbar that is defined in the previous example.
Use the Managed Package Framework (MPF) to program the package to display the toolbar in the tool window.
The ToolBar property of the ToolWindowPane class takes a CommandID instance as its value. The CommandID instance takes a (string) Guid and and an (int) ID. By setting these to the GUID:ID pair of the toolbar, the toolbar is bound to the tool window. Typically, this is done in the constructor of the class that derives from the ToolWindowPane class.
A managed code tool window that is built by using the MPF can have only one toolbar.
To display the toolbar in the tool window
When you create a VSPackage by using the package wizard, the command set GUIDs are automatically added to the GuidList class, which is defined in the Guids.cs file. Generated menu and command IDs are added to the PkgCmdIDList class, which is defined in PkgCmdID.cs. Then, GUID and ID values can be accessed, in a readable form, through those two classes.
Add an entry for the tool window toolbar, and for any commands that you want to appear on the toolbar. The value of each entry must match the value attribute of the associated IDSymbol element.
The following example creates an entry for the toolbar defined earlier.
In the constructor, set the ToolBar property of the MyToolWindow class to a new [T:System.ComponentModel.Design.]CommandID instance with the Guid and ID of the toolbar as parameters, as shown in the following example.
If the GuidList and PkgCmdIDList classes are not defined, you can use the String value of the GUID and the Int value of the ID instead.