Toll Free: (877) 723-1442 U.S. and Canada
Support
ActiveX / COM
Visual C++ / MFC
Customer Spotlight
Corporate Partners
Contact Sales

Call center available M-F 9:00 - 6:00 US Eastern Time.

U.S. and Canada
(877) 723-1442

International
(517) 625-5729

Email

Office Style Toolbar and Menu Customization

Author: Kirk Stowell
Posted: November 16, 2004
Environment: 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.
  2. 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()



  3. Add the body for the OnCustomize function:


  4.  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();

         }

     }



  5. 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.


  6.  int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

     {

         ...

     

             // Load the previous state for toolbars and menus.

             LoadCommandBars(_T("CommandBars"));

     

         return 0;

     }



  7. 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.


  8.  void CMainFrame::OnClose()

     {

         // Save the current state for toolbars and menus.

         SaveCommandBars(_T("CommandBars"));

         CMDIFrameWnd::OnClose();

     }

See Also

Microsoft, Visual Studio, and the Visual Studio logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Other products and/or company names may be trademarks or registered trademarks of their respective owners.