How to: Add Icons to Commands on Toolbars

注意

Beginning with 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 XML-Based Command Table Configuration (.vsct) Files.

Commands can appear on both menus and toolbars. On toolbars, it is common for a command to be displayed with just an icon (to save space) while on menus a command typically appears with both an icon and text.

Icons are 16 pixels wide by 16 pixels high and can be either 8-bit color depth (256 colors) or 32-bit color depth (true color). 32-bit color icons are preferred. Icons are typically arranged in a single horizontal row in a single bitmap, although multiple bitmaps are allowed. This bitmap is declared in the Command Table Configuration (.Ctc) Files along with the individual icons available in the bitmap. See the reference for the BITMAPS_BEGIN – BITMAPS_END section for more details.

Adding an Icon to a Command

To add an icon to a command

  1. Create a bitmap with a color depth of 32-bits. An icon is always 16 x 16 so this bitmap must be 16 pixels high and a multiple of 16 pixels wide.

    Each icon is placed on the bitmap next to each other in a single row. Use the alpha channel to indicate places of transparency in each icon.

    If you use an 8-bit color depth, use magenta, RGB(255,0,255), as the transparency. However, 32-bit color icons are preferred.

  2. Create an entry in the BITMAPS_BEGIN – BITMAPS_END section of the .ctc file that represents the bitmap containing the icons.

    • Set the Bitmap ID field to a GUID:ID pair that represents the bitmap.

      This GUID is typically the same GUID as the command set. The ID is the resource ID of the bitmap.

    • Follow the Bitmap ID field with one or more indices indicating which icons in the bitmap are available for use.

      The index values start at 1, which represents the first icon in the bitmap. If an index for an icon is not specified here, the icon cannot be used. See the BITMAPS_BEGIN – BITMAPS_END section for more details.

  3. Create a command entry in the BUTTONS_BEGIN – BUTTONS_END section. The two fields are important for adding an icon to the command are the Icon ID and Flags fields.

    • Set the Icon ID field to the GUID:ID pair of the icon to use. The GUID is the same GUID used in the Bitmap ID field of the bitmap entry in the BITMAPS_BEGIN – BITMAPS_END section. The ID is the index value of the icon to use.

    • Add to the Flags field one of the following flags:

      IconAndText: Both the icon and the button text are displayed in the menu and toolbar.

      Pict: Only the icon of the command is shown on a toolbar while only the text of the command is shown on a menu. If an icon is not specified, a blank space is shown on a toolbar.

      TextOnly: only the text of the command is shown on a toolbar or a menu. The Icon ID field is ignored.

Example

This is an example .ctc file that defines three commands, each with an icon, and places those icons on a top level menu and a toolbar. The commands appear with both an icon and text in both the menu and toolbar.

Typically the #define statements at the beginning of the example are placed in one or more header files that are included into the .ctc file with the #include statement. The #define statements are shown here for clarity.

#include "vsshlids.h"

// GUIDs
// (These GUIDs are for example only. You must use your own GUIDs in your .ctc file.)
#define guidMyPackage { 0xCC1BD4C8, 0xE76D, 0x4E31, { 0x89, 0x78, 0x94, 0x43, 0xC6, 0x3E, 0x74, 0x6A } }
#define guidMyCmdSet  { 0x0B15CEDD, 0x5AE1, 0x4C58, { 0xA8, 0xBB, 0x1F, 0x16, 0xDA, 0xB4, 0xC3, 0xFF } }

// Resource IDs
// (This typically appears in a resource.h file.)
#define IDB_MENU_IMAGES 0x400

// Bitmap IDs
// (These typically appear in the PkgCmdID.h file.)
#define bmpOpen  1
#define bmpClose 2
#define bmpNew   3

// Menu IDs
// (These typically appear in the PkgCmdID.h file.)
#define MyFileMenu        0x1000
#define MyFileToolbar     0x1100

// Menu Group IDs
// (These typically appear in the PkgCmdID.h file.)
#define MyFileGroup       0x1200
#define VSTopMenuBarGroup 0x1300

// Command IDs
// (These typically appear in the PkgCmdID.h file.)
#define cmdidOpen  0x100
#define cmdidClose 0x101
#define cmdidNew   0x102


CMDS_SECTION guidMyPackage
    MENUS_BEGIN
        guidMyCmdSet:MyFileMenu,            // Menu ID
            guidMyCmdSet:VSTopMenuBarGroup, // Parent Group
            0x0000,                         // Priority
            ,                               // Type and Flags (normal menu)
            "My &File";                     // Menu name

        guidMyCmdSet:MyFileToolbar,         // Menu ID
            guidMyCmdSet:MyFileToolbar,     // Parent Group
            0x0000,                         // Priority
            TOOLBAR | DEFAULTDOCKED,        // Type and flags
            "My File Toolbar";              // Toolbar name
    MENUS_END

    NEWGROUPS_BEGIN
        // Any command added to this group appears on our file menu
        // and file toolbar.
        guidMyCmdSet:MyFileGroup,                 // Group ID
            guidMyCmdSet:MyFileMenu,              // Menu ID
            0x0000;                               // Priority

        // Any menu added to this group appears on the top menu bar
        // in Visual Studio.
        guidMyCmdSet:VSTopMenuBarGroup,           // Group ID
            guidSHLMainMenu:IDM_VS_TOOL_MAINMENU, // Menu ID
            0xE000;                               // Priority
    NEWGROUPS_END

    BUTTONS_BEGIN
        guidMyCmdSet:cmdidOpen,        // Command ID
            guidMyCmdSet:MyFileGroup,  // Parent Group
            0x0000,                    // Priority
            guidMyCmdSet:bmpOpen,      // Icon ID
            BUTTON,                    // Type
            ICONANDTEXT,               // Flags
            "&Open";                   // Button Text

        guidMyCmdSet:cmdidClose,       // Command ID
            guidMyCmdSet:MyFileGroup,  // Parent Group
            0x0100,                    // Priority
            guidMyCmdSet:bmpClose,     // Icon ID
            BUTTON,                    // Type
            ICONANDTEXT,               // Flags
            "&Close";                  // Button Text

        guidMyCmdSet:cmdidNew,         // Command ID
            guidMyCmdSet:MyFileGroup,  // Parent Group
            0x0200,                    // Priority
            guidMyCmdSet:bmpNew,       // Icon ID
            BUTTON,                    // Type
            ICONANDTEXT,               // Flags
            "&New";                    // Button Text
    BUTTONS_END

    BITMAPS_BEGIN
        guidMyCmdSet:IDB_MENU_IMAGES, bmpOpen, bmpClose, bmpNew;
    BITMAPS_END
CMDS_END

CMDPLACEMENT_SECTION
    // Place the file group onto the toolbar.
    guidMyCmdSet:MyFileGroup,       // Item ID
        guidMyCmdSet:MyFileToolbar, // Parent Group
        0x0000;                     // Priority
CMDPLACEMENT_END

See Also

Concepts

Common Menu Tasks

Command Table Configuration (.Ctc) Files

BITMAPS_BEGIN – BITMAPS_END

BUTTONS_BEGIN – BUTTONS_END