Home | Overview | How Do I | Sample | Tutorial
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:
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:
The toolbar object is a CToolBar 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 OnCreate 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:
Note The MFC General sample DOCKTOOL 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 MFC samples related to toolbars.
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.
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 toolbar editor. 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 CToolBar member functions work with the RT_TOOLBAR resource. LoadToolBar now takes the place of LoadBitmap to load the bitmap of the toolbar button images, and SetButtons to set the button styles and connect buttons with bitmap images.
For details about the toolbar editor, see the Toolbar Editor overview in the Visual C++ User’s Guide.
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: