CommandBars Articles and Tutorials

Office Style Toolbars and Menus

Author: Kirk Stowell
Platform: Visual C++ MFC

The following is a tutorial on how to create an MDI application using the Visual Studio 6.0 Application Wizard that will have Office style menus and toolbars. The same technique can be used for later versions of Visual Studio .NET as well.

Create a simple MDI application using the MFC AppWizard:

  1. From Visual Studio and select ‘File’ then ‘New’ and select the ‘Projects’ tab.
  2. Choose MFC Appwizard(exe) as your project type and enter ‘MDISample’ as the project name.
    Tutorial
  3. For the first step, make sure that "Multiple documents" is selected then press the ‘Finish’ button.

Add Command Bars components:

  1. Add the following line to your StdAfx.h file:
    Toolkit Pro users:
    #include <XTToolkitPro.h> // Toolkit Pro components
    
    Command Bars users:
    #include <XTCommandBarsPro.h> // Command Bars components
    
  2. In your MainFrm.h file you need to change your base class to be CXTPMDIFrameWnd (CXTPFrameWnd for SDI applications):
    class CMainFrame : public CXTPMDIFrameWnd
    {
        ...
    };
    
  3. If you plan to override either PreTranslateMessage or OnWndMsg make sure that you call the CXTPMDIFrameWnd base class, for example:
    BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
    {
        // TODO: Add your specialized code here and/or call the base class
        return CXTPMDIFrameWnd::PreTranslateMessage(pMsg);
    }
    
    BOOL CMainFrame::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
    {
        // TODO: Add your specialized code here and/or call the base class
        return CXTPMDIFrameWnd::OnWndMsg(message, wParam, lParam, pResult);
    }
    
  4. Replace int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) function to
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
            return -1;
    
        // Create Status bar. 
        // Important: All control bars including the Status Bar 
        // must be created before CommandBars....
        if (!m_wndStatusBar.Create(this) ||
            !m_wndStatusBar.SetIndicators(indicators,
            sizeof(indicators)/sizeof(UINT)))
        {
            TRACE0("Failed to create status bar\n");
            return -1;      // fail to create
        }
    
        // Initialize the Command Bar
        if (!InitCommandBars())
            return -1;
    
        // Get a pointer to the Command Bar object.
        CXTPCommandBars* pCommandBars = GetCommandBars();
        if(pCommandBars == NULL)
        {
            TRACE0("Failed to create Command Bar object.\n");
            return -1;      // fail to create
        }
    
        // Add the menu bar
        CXTPCommandBar* pMenuBar = pCommandBars->SetMenu(
            _T("Menu Bar"), IDR_MDISAMTYPE);
        if(pMenuBar == NULL)
        {
            TRACE0("Failed to create menu bar.\n");
            return -1;      // fail to create
        }
    
        // Create ToolBar
        CXTPToolBar* pToolBar = (CXTPToolBar*)
            pCommandBars->Add(_T("Standard"), xtpBarTop);
        if (!pToolBar || !pToolBar->LoadToolBar(IDR_MAINFRAME))
        {
            TRACE0("Failed to create toolbar\n");
            return -1;
        }
    
        // Set Office 2003 Theme
        CXTPPaintManager::SetTheme(xtpThemeOffice2003);
    
        return 0;
    }
    

Now we have an MDI application with an Office 2003 interface...it's that Easy!

tutorial

See Also: Office Style Toolbar and Menu Customization