Toolbars: Fundamentals
| Overview | How Do I | Sample |
This article describes the fundamental MFC implementation that lets you add a default toolbar to your application by selecting an option in AppWizard. Topics covered include:
AppWizard toolbar option
The toolbar in code
Editing the toolbar resource
Multiple toolbars
The AppWizard Toolbar Option
To get a single toolbar with default buttons, select the Dockable Toolbar option on the page labeled AppWizard Step 4 of 6. This adds code to your application that:
Creates the toolbar object.
Manages the toolbar, including its ability to dock or to float.
The Toolbar in Code
The toolbar object is a object declared as a data member of your application’s CMainFrame class. In other words, the toolbar object is embedded in the main frame window object. This means that MFC creates the toolbar when it creates the frame window and destroys the toolbar when it destroys the frame window. The following partial class declaration, for a multiple document interface (MDI) application, shows data members for an embedded toolbar and an embedded status bar. It also shows the override of the OnCreate member function.
class CMainFrame : public CMDIFrameWnd
{
// ...
// Implementation
// ...
protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
// Generated message map functions
protected:
//{{AFX_MSG(CMainFrame)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of
// generated code!
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
Toolbar creation occurs in CMainFrame::OnCreate. MFC calls after creating the Windows window for the frame but before the frame window becomes visible. The default OnCreate that AppWizard generates does the following toolbar tasks:
Calls the CToolBar object’s member function to create the underlying object.
Calls functions to enable docking, floating, and tool tips. For details about these calls, see the article Toolbars: Docking and Floating.
Note The MFC General sample includes illustrations of both old and new MFC toolbars. The toolbars that use COldToolbar require calls in step 2 to LoadBitmap (rather than LoadToolBar) and to SetButtons. The new toolbars require calls to LoadToolBar. Click for a list of .
The docking, floating, and tool tips calls are optional. You can remove those lines from OnCreate if you prefer. The result is a toolbar that remains fixed, unable to float or redock and unable to display tool tips.
Editing the Toolbar Resource
The default toolbar you get with AppWizard is based on an RT_TOOLBAR custom resource, introduced in MFC version 4.0. You can edit this resource with the . The editor lets you easily add, delete, and rearrange buttons. It contains a graphical editor for the buttons that is very similar to the general graphics editor in Visual C++. If you edited toolbars in previous versions of Visual C++, you’ll find the task much easier now.
To connect a toolbar button to a command, you give the button a command ID, such as ID_MYCOMMAND
. Specify the command ID in the button’s property page in the toolbar editor. Then use ClassWizard to create a handler function for the command.
New member functions work with the RT_TOOLBAR resource. now takes the place of to load the bitmap of the toolbar button images, and to set the button styles and connect buttons with bitmap images.
For details about the toolbar editor, see the in the Visual C++ User’s Guide.
Multiple Toolbars
AppWizard gives you one toolbar. If you want more, model your code for the additional toolbars on the code for the first one.
If you want to display a toolbar as the result of a command, you’ll need to:
Create a new toolbar resource with the toolbar editor and load it in OnCreate with the member function.
Make the appropriate function calls in OnCreate to dock or float the toolbar, set its styles, and so on.