How To Add a Toolbar in an MDI Child Window

Last reviewed: July 10, 1997
Article ID: Q155141
4.00 4.10 4.20 WINDOWS NT kbprg

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), included with: Microsoft Visual C++, 32-bit Edition, versions 4.0, 4.1, 4.2

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.


Additional reference words: 4.00 4.10 4.20 kbdse
KBCategory: kbui
KBSubcategory: MfcUI
Keywords : MfcUI kbui
Technology : kbMfc
Version : 4.00 4.10 4.20
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.