第四章 怎样添加用户自定义菜单和工具栏

Tutorials for Using Xtreme Toolkit Pro v13.2 源程序链接

下面是一个案例指导,怎样为应用程序创建“添加自定义工具栏和菜单”。本案例假定你已经创建了一个具有office风格的工具栏和菜单。(见上一节内容)
添加自定义菜单和工具栏.
  1. 在CMainFrame为XTP_ID_CUSTOMIZE 添加ON_COMMAND  消息映射. 控制工具栏和菜单自定义对话框的设置和显示 .
    在 MainFrm.cpp 添加下面代码:
    BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
    	//{{AFX_MSG_MAP(CMainFrame)
    		// NOTE - the ClassWizard will add and remove mapping macros here.
    		//    DO NOT EDIT what you see in these blocks of generated code !
    	ON_WM_CREATE()
    	//}}AFX_MSG_MAP
    	ON_COMMAND(XTP_ID_CUSTOMIZE,OnCustomize)
    END_MESSAGE_MAP()
    在 MainFrm.h 添加代码:
// 生成消息映射函数
protected:
	//{{AFX_MSG(CMainFrame)
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
		// NOTE - the ClassWizard will add and remove member functions here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	afx_msg void OnCustomize();
	DECLARE_MESSAGE_MAP()
};
  • 添加OnCustomize函数体:
    void CMainFrame::OnCustomize()
    {
        // 得到命令工具栏对象指针.
        CXTPCommandBars* pCommandBars = GetCommandBars();
        if(pCommandBars != NULL)
        {
            // 创建(Instanciate)自定义对话框对象.
            CXTPCustomizeSheet dlg(pCommandBars);
      
            //自定义对话框中添加选项页.
            CXTPCustomizeOptionsPage pageOptions(&dlg);
            dlg.AddPage(&pageOptions);
      
            // 自定义对话框中添加命令页.
            CXTPCustomizeCommandsPage* pCommands = dlg.GetCommandsPage();
            pCommands->AddCategories(IDR_MDITYPE);
      
            // 使用命令栏管理器初始化自定义对话框.
            pCommands->InsertAllCommandsCategory();
            pCommands->InsertBuiltInMenus(IDR_MDITYPE);
            pCommands->InsertNewMenuCategory();
      
            //显示对话框.
            dlg.DoModal();
        }
    }
    
  • CMainFrame的OnCreate函数添加 LoadCommandBars(_T("CommandBars")); . 恢复工具栏和菜单先前状态和自定义选项.
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        ...
    
        

    //加载先前工具栏和菜单状态.     LoadCommandBars(_T("CommandBars"));  

      return 0; }

  • 在CMainFrame 添加OnClose消息句柄并在基类的调用前添加SaveCommandBars(_T("CommandBars"));. 保存最近一次自定义的工具栏和菜单状态.
    void CMainFrame::OnClose()
    {
        // 保存最近一次自定义的工具栏和菜单状态.
        SaveCommandBars(_T("CommandBars"));
        CMDIFrameWnd::OnClose();
    }
    
    MDI 示例程序添加移除菜单...
    第四章 怎样添加用户自定义菜单和工具栏_第1张图片
    MDI 示例程序自定义设置对话框...
    第四章 怎样添加用户自定义菜单和工具栏_第2张图片


    -----------------------------------------------------------------------------------------------------------------------------------------------------

    原文

    Chapter 4: Tutorials for Using Xtreme Toolkit Pro v13.2
    Up | Previous | Next
    How to Add Customization to Toolbars and Menus 

    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.
    Add customization for toolbars and menus.
    1. Add an ON_COMMAND for XTP_ID_CUSTOMIZE to the message map forCMainFrame. This will handle setup and display for the toolbar and menu customization dialog.
      In MainFrm.cpp add the following code:
      BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
          //{{AFX_MSG_MAP(CMainFrame)
          ON_WM_CREATE()
          //}}AFX_MSG_MAP
          ON_COMMAND(XTP_ID_CUSTOMIZE, OnCustomize)
      END_MESSAGE_MAP()
      
      in MainFrm.h add the following code:
          //{{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 bars object.
          CXTPCommandBars* pCommandBars = GetCommandBars();
          if(pCommandBars != NULL)
          {
              // Instanciate 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();
      
              // Dispaly the dialog.
              dlg.DoModal();
          }
      }
      
    3. Add LoadCommandBars(_T("CommandBars")); to theOnCreate function forCMainFrame. 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();
      }
      
      MDI Sample Application Add or Remove Buttons Menu...
      第四章 怎样添加用户自定义菜单和工具栏_第3张图片
      MDI Sample Application Customize Dialog...

  • 你可能感兴趣的:(manager,command,application,dialog,工具,tutorials)