Walkthrough: Adding a Toolbar to the IDE

This walkthrough guides you through the steps for adding a toolbar to the Visual Studio integrated development environment (IDE).

A toolbar is a horizontal or vertical strip that contains buttons bound to commands. Depending on its implementation, a toolbar in the IDE can be repositioned, docked on any side of the main IDE window, or made to stay in front of all other windows.

In addition, commands can be added to a toolbar or removed from it by using the Customize dialog box. The toolbar typically supplied by a VSPackage is a toolbar that the user can customize. The IDE handles all customization, and the VSPackage responds to commands. The VSPackage does not have to know where a command is physically located.

For more information about menus, see Menus and Toolbars.

Prerequisites

Because this walkthrough writes information to the experimental registry hive, Visual Studio 2008 SDK must be installed.

Creating a VSPackage

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

To create the MyToolbarPackage VSPackage

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

  2. In the Project types pane of the New Project dialog box, expand Other Project Types, and then click Extensibility.

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

  4. In the Name box, type MyToolbarPackage. Select a destination folder for the project.

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

  6. Read 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.

    You can also use the Visual C++ programming language for your VSPackage. The steps in this walkthrough work for both languages.

  8. On the Basic VSPackage Information page, accept the defaults.

  9. On the Select VSPackage Options page, select Menu Command.

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

  11. Click Finish. After the wizard finishes building the project, the status bar displays that the MyToolbarPackage solution has been built successfully.

Creating a Toolbar for the IDE

To create a toolbar for the IDE

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

  2. In the Menu IDs section, add a definition for MyToolbar, as follows.

    #define MyToolbar 0x1000
    
  3. In the Menu Group IDs section, add a definition for MyToolbarGroup.

    #define MyToolbarGroup 0x1050
    
  4. In Solution Explorer, right-click the MyToolbarPackage file, and then click Open to open it in a text editor.

  5. 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 Name
    

    Toolbars cannot be nested like submenus. Therefore, 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 subsequent changes by the user are persisted.

  6. 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
    
  7. In the CMDPLACEMENT_SECTION section, type the following lines to add the existing command to the toolbar group so that the toolbar will be displayed.

    guidMyToolbarPackageCmdSet:cmdidMyTestCmd,     // Command ID
        guidMyToolbarPackageCmdSet:MyToolbarGroup, // Parent ID
        0x0000;                                    // Priority
    

    By default, if a toolbar has no commands, it does not appear.

  8. On the Build menu, click Build Solution. Correct any errors that might occur.

  9. Press F5 to open the experimental Visual Studio in debug mode.

  10. Right-click the IDE menu bar, and then click My IDE Toolbar on the list of toolbars.

  11. Click the icon on the new toolbar to see a dialog box that shows the message "Inside Company.MyToolbarPackage.MyToolbarPackage.MenuItemCallback()".

See Also

Concepts

Menu and Toolbar Command Walkthroughs

Menus and Toolbars