Article ID: 155141 - Last Review: November 21, 2006 - Revision: 2.1

HOWTO: How To Add a Toolbar in an MDI Child Window

This article was previously published under Q155141

On This Page

Expand all | Collapse all

SUMMARY

By default, an AppWizard-generated MDI application has a toolbar in its main application window (CMDIFrameWnd-derived class). This toolbar is shared by all the MDI child windows (CMDIChildWnd-derived class) in the application. In some cases, it would be useful to have a toolbar appear in each MDI child window. This is demonstrated in the following sample code.

Because CMDIChildWnd is a CFrameWnd-derived class, the toolbar in an MDI child window may be dockable. This can be easily achieved by calling such functions as EnableDocking() and DockControlBar().

Sample Code

Steps to create a toolbar in an MDI child window:

  1. Create a CMDIChildWnd-derived class using ClassWizard if one is missing. Remember to use this derived class in the CMultiDocTemplate statement in the CWinApp::InitInstance() function.
  2. Add a member variable of type CToolBar to the CMDIChildWnd-derived class.
       class CChildFrame : public CMDIChildWnd
       {
       ...
           CToolBar    m_wndToolBar;
       }
    						
  3. Override the OnCreate() function in your CMDIChildWnd-derived class using ClassWizard.
  4. Add the following code to the OnCreate() function of your CMDIChildWnd- derived class:
       int CChildFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
       {
           if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
               return -1;
    
           // Create a toolbar window.  IDR_CHILDFRAME is the resource name
           // of the toolbar to be loaded.
           if (!m_wndToolBar.Create(this) ||
               !m_wndToolBar.LoadToolBar(IDR_CHILDFRAME))
           {
               TRACE0("Failed to create toolbar\n");
               return -1;      // fail to create
           }
    
           // TODO: Remove this if you don't want tool tips or a
           // resizeable toolbar
           m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
               CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
    
           // TODO: Delete these three lines if you don't want the toolbar
           // to be dockable
           m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
           EnableDocking(CBRS_ALIGN_ANY);
           DockControlBar(&m_wndToolBar);
    
           return 0;
       }

REFERENCES

For information on using ClassWizard, please refer to VC++ Books Online.

APPLIES TO
  • Microsoft Foundation Class Library 4.2, when used with:
    • Microsoft Visual C++ 4.0 Standard Edition
    • Microsoft Visual C++ 4.1 Subscription
    • Microsoft Visual C++ 4.2 Enterprise Edition
    • Microsoft Visual C++ 4.2 Professional Edition
Keywords: 
kbdocview kbhowto kbmdi kbmfcctrlbar kbtoolbar kbuidesign KB155141
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
 

Article Translations