Toolbars in visual basics
A toolbar is a window that contains one or more rows of command buttons. These buttons provide a visual method by which the user can perform specific tasks that can also be performed through the menus in the application. Because toolbar buttons are visual representations of menu commands, toolbars mimic much of the behavior and implementation of menus.
Toolbar button graphics are based on a single bitmap that contains a row of button images. The following illustration shows a sample toolbar.
When you create an application using AppWizard, Step 4 of the creation process enables you to specify whether you want the default toolbar (IDR_MAINFRAME) added to your MFC-based application. If you choose the default toolbar, the MFC framework adds the toolbar and the default functionality.
This section includes the following topics:
® Modifying the Toolbar
® Implementing Toolbar Buttons
® Displaying Toolbar States
® Toolbars and ToolTips
You use the Toolbar editor to add a new button to a toolbar or to change the appearance or location of a button on a toolbar.
The Toolbar editor has three panes. The top pane contains a bitmap of the toolbar, and reflects the changes that occur in the other two panes. To view the Toolbar editor, click Toolbar Editor on the Image menu.
To add a button to the toolbar
1. In your project workspace view, select the ResourceView tab.
2. Click the Toolbar folder, and double-click the blank button at the far right. This button is provided by the Toolbar editor and is a placeholder rather than part of the toolbar.
3. Supply the information for the queries posed in the Toolbar Button Properties property sheet. If you want to associate the button with an existing command, select the Command ID from the drop-down list of IDs provided.
To reposition a button
— In the Toolbar editor, drag the button to a new location on the toolbar.
To copy a button
— In the Toolbar editor, hold down the CTRL key while dragging and dropping the button you want to copy.
To add or delete a separator
— In the Toolbar editor, drag the button so that it is next to the place where you want to add or remove the separator. The separator will automatically be added or deleted according to how you place the button.
To delete a button
— In the Toolbar editor, drag the button off the toolbar. You cannot delete the blank placeholder button on the right end.
To see a demonstration of how to extend the default toolbar, click this icon.
Clicking a toolbar button generates a command, just as choosing a menu item does. As a result, you must add a command handler to implement toolbar button functionality. A toolbar button with no associated command handler appears dimmed and is unavailable to the application.
Associating a Toolbar Button with an Existing Menu Item Command
If you have already implemented the command handler for a menu item, you can implement a toolbar button by giving it the same command ID as the associated menu item. You assign the command ID in the Toolbar Button Properties property sheet. If the menu item already has a prompt string that appears in the status bar, the same prompt string also appears in the button's status bar display.
Associating a Toolbar Button with a New Command
If you need to create a command for a toolbar button that has no associated menu item, you must first create a menu item to associate with the button, and then add the menu item to a dummy menu. You create a dummy menu as you would any other menu; however, the dummy menu does not appear in the user interface.
Toolbars can maintain various states, and the state of a toolbar can change while the toolbar is visible. For example, when the user clicks a toolbar button, the button appears as if it is pressed; if a toolbar button is unavailable, it appears dimmed.
Toolbar States
Like menu items, toolbar buttons are displayed differently based on whether they are available or unavailable (normal or dimmed). When you click a toolbar button, its appearance changes. An available button appears to be raised, while a clicked button looks as if it has been pressed.
Buttons on a toolbar, like menu items, are functionally and visibly disabled until a command handler is added to support button selection. To change the state of a toolbar button at run time, the application must provide a command handler to change its enabled or selection state.
Using Command Handlers to Change Toolbar States
Command handlers for toolbar buttons are called whenever the system reaches an idle state. (For more information, search for CWinThread::OnIdle in Visual C++ Help.) The handlers allow the visible states of toolbars to change dynamically.
The following table lists common CCmdUI member functions available in a command handler. In this context, the handlers affect the visual state of toolbar buttons rather than menu items.
Function Description
SetCheck Selects a toolbar button (shows as depressed).
SetRadio Same as SetCheck.
A ToolTip is a small pop-up window that appears when the mouse pointer is paused over a toolbar button or other user interface element, such as an icon. Generally, a ToolTip contains a single, short line of descriptive text about the associated user-interface element. The following illustration shows a toolbar with a ToolTip.
Adding ToolTips
Developer Studio supports ToolTips for menus and toolbar buttons.
To add a ToolTip, append a text string to the status bar prompt string on the Menu Item Properties property sheet of the menu item. ToolTip strings should be brief, consisting of one or a few words. The ToolTip string is preceded by \n and follows the prompt string; for example:
Open an existing document\nOpen
Open an existing document is the prompt string, and \nOpen is the appended ToolTip string. Note that there are no spaces around the \n separator character.
Ideally, you should supply a ToolTip string for every menu item in an application. Because toolbar buttons are usually associated with existing menu commands, ToolTips are generated automatically.
Disabling ToolTips
There are instances when you do not want or need ToolTips. In these cases, disable ToolTips for your application by commenting out these lines of code from CMainFrame::OnCreate:
//TODO: Remove this if you don't want ToolTips
m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY)
Comments
Post a Comment