Toolbar Fundamentals
This article describes the fundamental MFC implementation that lets you add a default toolbar to your application by selecting an option in the Application Wizard. Topics covered include:
The Application Wizard toolbar option
The toolbar in code
Editing the toolbar resource
Multiple toolbars
The Application Wizard Toolbar Option
To get a single toolbar with default buttons, select the Standard Docking toolbar option on the page labeled User Interface Features. 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 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 int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()
Toolbar creation occurs in CMainFrame::OnCreate. MFC calls OnCreate after creating the window for the frame but before it becomes visible. The default OnCreate that the Application Wizard generates does the following toolbar tasks:
Calls the CToolBar object's Create member function to create the underlying CToolBarCtrl object.
Calls LoadToolBar to load the toolbar resource information.
Calls functions to enable docking, floating, and tool tips. For details about these calls, see the article Docking and Floating Toolbars.
Примечание. |
---|
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. |
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 the Application Wizard 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 create a handler function for the command (see Mapping Messages to Functions for more information).
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 using the toolbar editor, see Toolbar Editor.
Multiple Toolbars
The Application Wizard provides you with one default toolbar. If you need more than one toolbar in your application, you can model your code for additional toolbars based on the wizard-generated code for the default toolbar.
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 LoadToolbar member function.
Embed a new CToolBar object in your main frame window class.
Make the appropriate function calls in OnCreate to dock or float the toolbar, set its styles, and so on.