Toolbars
A toolbar consists of a number of horizontally (or vertically) arranged graphical buttons that might be clustered in groups. The programming interface determines the grouping. The graphical images for the buttons are stored in a single bitmap that is attached to the application's resource file. When a button is clicked, it sends a command message, as menus and keyboard accelerators do. An update command user interface message handler is used to update the button's state, which in turn is used by the application framework to modify the button's graphical image.
The Toolbar Bitmap
Each button on a toolbar appears to have its own bitmap, but actually a single bitmap serves the entire toolbar. The toolbar bitmap has a tile, 15 pixels high and 16 pixels wide, for each button. The application framework supplies the button borders, and it modifies these borders, together with the button's bitmap tile color, to reflect the current button state. Figure 13-1 shows the relationship between the toolbar bitmap and the corresponding toolbar.

Figure 13-1: A toolbar bitmap and an actual toolbar.
The toolbar bitmap is stored in the file

Toolbar Button States
Each toolbar button can assume the states listed in Table 13-1. (There are additional states for later toolbar versions.)
State | Description |
---|---|
0 | Normal, unpressed state. |
TBSTATE_CHECKED | Checked (down) state. |
TBSTATE_ENABLED | Available for use. Button is grayed-out and unavailable if this state is not set. |
TBSTATE_HIDDEN | Not visible. |
TBSTATE_INDETERMINATE | Grayed-out. |
TBSTATE_PRESSED | Currently selected (pressed) using the mouse. |
TBSTATE_WRAP | Line break follows the button. |
A toolbar button can be a pushbutton, which is down only when currently clicked by the mouse, or it can be a check box button, which can be toggled up and down with mouse clicks. All toolbar buttons in the standard application framework toolbar are pushbuttons.
The Toolbar and Command Messages
When the user clicks a toolbar button with the mouse, a command message is generated. This message is routed like the menu command messages you saw in Chapter 12. Most of the time, a toolbar button matches a menu command. In the standard application framework toolbar, for example, the Disk button is equivalent to the File Save menu command—both generate the ID_FILE_SAVE command. The object receiving the command message doesn't need to know whether the message was produced by a button click or by the menu command.
A toolbar button doesn't have to mirror a menu command. If you don't provide an equivalent menu command, however, you should define a keyboard accelerator for the button so the user can activate the command with the keyboard or with a keyboard macro product for Windows. You can use Class View and the Properties window to define commands and update command user interface message handlers for toolbar buttons, whether or not they have corresponding menu commands.A toolbar has an associated bitmap resource and, in the RC file, a companion toolbar resource that defines the menu commands associated with the buttons. Both the bitmap and the toolbar resource have the same ID, typically IDR_MAINFRAME. The text of the toolbar resource generated by the MFC Application Wizard is shown here:
IDR_MAINFRAME TOOLBAR 16, 15
BEGIN
BUTTON ID_FILE_NEW
BUTTON ID_FILE_OPEN
BUTTON ID_FILE_SAVE
SEPARATOR
BUTTON ID_EDIT_CUT
BUTTON ID_EDIT_COPY
BUTTON ID_EDIT_PASTE
SEPARATOR
BUTTON ID_FILE_PRINT
BUTTON ID_APP_ABOUT
END
The SEPARATOR constants serve to group the buttons by inserting corresponding spaces on the toolbar. If the number of toolbar bitmap panes exceeds the number of resource elements (excluding separators), the extra buttons are not displayed.When you edit the toolbar using the resource editor, you're editing both the bitmap resource and the toolbar resource. You select a button image, and then you edit the properties, including the button's ID, in the Properties window.
Toolbar Update Command User Interface Message Handlers
You'll recall from Chapter 12 that update command user interface message handlers are used to disable or add check marks to menu commands. These same message handlers apply to toolbar buttons. If your update command user interface message handler calls the CCmdUI::Enable member function with a FALSE parameter, the corresponding button will be set to the disabled (grayed-out) state and no longer respond to mouse clicks.
Next to a menu command, the CCmdUI::SetCheck member function displays a check mark. For the toolbar, the SetCheck function implements check box buttons. If the update command user interface message handler calls SetCheck with a parameter value of 1, the button will be toggled to the down (checked) state; if the parameter is 0, the button will be toggled up (unchecked).
Note | If the SetCheck parameter value is 2, the button will be set to the indeterminate state. This state looks like the disabled state, but the button is still active and its color is a bit brighter. |
The update command user interface message handlers for a shortcut menu are called only when the menu is painted. The toolbar is displayed all the time, so when are its update command user interface message handlers called? They're called during the application's idle processing so the buttons can be updated continuously. If the same handler covers a menu command and a toolbar button, it is called both during idle processing and when the shortcut menu is displayed.