Tutorials for Using Xtreme Toolkit Pro v13.2源程序链接
下面是一个案例指导,怎样为应用程序创建“添加自定义工具栏和菜单”。本案例假定你已经创建了一个具有office风格的工具栏和菜单。(见上一节内容)
添加自定义菜单和工具栏.
-
在CMainFrame为XTP_ID_CUSTOMIZE添加ON_COMMAND消息映射.控制工具栏和菜单自定义对话框的设置和显示 .在MainFrm.cpp 添加下面代码:在MainFrm.h 添加代码: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()
// 生成消息映射函数
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();
}
}
LoadCommandBars(_T("CommandBars"));. 恢复工具栏和菜单先前状态和自定义选项.
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
...
//加载先前工具栏和菜单状态. LoadCommandBars(_T("CommandBars"));
return 0; }
OnClose消息句柄并在基类的调用前添加
SaveCommandBars(_T("CommandBars"));. 保存最近一次自定义的工具栏和菜单状态.
void CMainFrame::OnClose()
{
// 保存最近一次自定义的工具栏和菜单状态.
SaveCommandBars(_T("CommandBars"));
CMDIFrameWnd::OnClose();
}
MDI 示例程序添加移除菜单...
MDI 示例程序自定义设置对话框...
-----------------------------------------------------------------------------------------------------------------------------------------------------
原文
|
Chapter 4: Tutorials for Using Xtreme Toolkit Pro v13.2
|
How to Add Customization to Toolbars and Menus
Add customization for toolbars and menus.
- Add an
ON_COMMANDforXTP_ID_CUSTOMIZEto 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() - Add the body for the
OnCustomizefunction: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(); } } - Add
LoadCommandBars(_T("CommandBars"));to theOnCreatefunction 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; } - Add the
OnClosemessage handler to CMainFrame and addSaveCommandBars(_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...
MDI Sample Application Customize Dialog...
本文档详细介绍了如何为应用程序添加自定义工具栏和菜单功能。通过为CMainFrame类添加ON_COMMAND消息映射来实现自定义对话框的设置与显示,并提供了具体的代码实现步骤。

被折叠的 条评论
为什么被折叠?



