CommandBars Articles and Tutorials

Office Style Toolbar and Menu Customization

Author: Kirk Stowell
Platform: Visual C++ MFC

The following is a tutorial on how to create add customization to your applications toolbars and menus to your application. This tutorial assumes that you have already created an application that uses office style toolbars and menus. For a tutorial on how to create an application that uses Office style toolbars and menus, click here.

Add customization for toolbars and menus.

  1. Add a ON_COMMAND for XTP_ID_CUSTOMIZE to the message map for CMainFrame. This will handle setup and display for the toolbar and menu customization dialog.
    MainFrm.cpp:
    BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
    //{{AFX_MSG_MAP(CMainFrame)
    ON_WM_CREATE()
    //}}AFX_MSG_MAP
    ON_COMMAND(XTP_ID_CUSTOMIZE, OnCustomize)
    END_MESSAGE_MAP()
    
    MainFrm.h:
    //{{AFX_MSG(CMainFrame)
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    //}}AFX_MSG
    afx_msg void OnCustomize();
    DECLARE_MESSAGE_MAP()
    
  2. Add the body for the OnCustomize function:
    void CMainFrame::OnCustomize()
    {
        // Get a pointer to the Command Bar object.
        CXTPCommandBars* pCommandBars = GetCommandBars();
        if(pCommandBars != NULL)
        {
            // Instantiate the customize dialog object.
            CXTPCustomizeSheet dlg(pCommandBars);
    
            // Add the options page to the customize dialog.
            CXTPCustomizeOptionsPage pageOptions(&dlg);
            dlg.AddPage(&pageOptions);
    
            // Add the commands page to the customize dialog.
            CXTPCustomizeCommandsPage* pCommands =
            dlg.GetCommandsPage();
            pCommands->AddCategories(IDR_MDISAMTYPE);
    
            // Use the command bar manager to initialize the 
            // customize dialog.
            pCommands->InsertAllCommandsCategory();
            pCommands->InsertBuiltInMenus(IDR_MDISAMTYPE);
            pCommands->InsertNewMenuCategory();
    
            // Display the dialog.
            dlg.DoModal();
        }
    }
    
  3. Add LoadCommandBars(_T("CommandBars")); to the OnCreate function for CMainFrame. This will restore the previous state of your toolbar and menus plus any customization made.
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        ...
    
            // Load the previous state for toolbars and menus.
            LoadCommandBars(_T("CommandBars"));
    
        return 0;
    }
    
  4. Add the OnClose message handler to CMainFrame and add SaveCommandBars(_T("CommandBars")); before the call to the base class. This will save the current state of your toolbar and menus in plus any customization made.
     void CMainFrame::OnClose()
     {
         // Save the current state for toolbars and menus.
         SaveCommandBars(_T("CommandBars"));
         CMDIFrameWnd::OnClose();
     }
    

    Tutorial

    Tutorial

See Also: Office Style Toolbars and Menus