第四章 Xtreme Toolkit Pro v13.2 使用指南
怎样制作Office风格工具栏和菜单的应用程序
接下来的指南 是怎样使用Visual Studio 6.0 应用程序向导制作拥有Office菜单和工具栏风格的MDI 应用程序 . 这种技巧同样适用于更新版本的 Visual Studio .NET .
使用MFC AppWizard制作简单的 MDI 应用程序 :
- 从Visual Studio 中选择 File | New ,选择Projects 标签.
- 选择MFC Appwizard(exe) 作为项目类别 ,输入 ‘MDISample’ 作为项目名.
Visual Studio 新建对话框...
- 第一步, 确保选择了Multiple documents ,然后 按‘Finish’按钮.
添加 Xtreme 命令工具栏组件:
- 添加下面一行代码到
StdAfx.h
文件:Xtreme Toolkit Pro:
#include <XTToolkitPro.h> // Xtreme Toolkit Pro component library
- 在
MainFrm.h
文件,对于MDI应用程序改变基类为CXTPMDIFrameWnd
,对于SDI应用程序改基类为CXTPFrameWnd
:(译者注:就是在CMDIFrameWnd前加XTP(XtrmemToolkitPro))class CMainFrame : public CXTPMDIFrameWnd { ... };
- 如果打算覆盖( override)
PreTranslateMessage
或OnWndMsg
,确定你调用CXTPFrameWnd
或CXTPMDIFrameWnd
基类, 比如:
//虚函数BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CXTPMDIFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return TRUE; }
BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) { // TODO: Add your specialized code here and/or call the base class return CXTPMDIFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo); }
- 把下面的代码添加到
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
函数:int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } 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 } //删除下面3行代码 // TODO: Delete these three lines if you don't want the toolbar to be dockable停靠 // m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); // EnableDocking(CBRS_ALIGN_ANY); // DockControlBar(&m_wndToolBar); //添加自己的代码 // 初始命令工具栏 if (!InitCommandBars()) return -1; // 得到命令工具栏对象指针. CXTPCommandBars* pCommandBars = GetCommandBars(); if(pCommandBars == NULL) { TRACE0("Failed to create command bars object.\n"); return -1; // fail to create } // 添加菜单栏 CXTPCommandBar* pMenuBar = pCommandBars->SetMenu( _T("Menu Bar"), IDR_MDISAMTYPE); if(pMenuBar == NULL) { TRACE0("Failed to create menu bar.\n"); return -1; // fail to create } // 制作工具栏 CXTPToolBar* pToolBar = (CXTPToolBar*) pCommandBars->Add(_T("Standard"), xtpBarTop); if (!pToolBar || !pToolBar->LoadToolBar(IDR_MAINFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; } // 设置Office 2003 主题 CXTPPaintManager::SetTheme(xtpThemeOffice2003); return 0; }
MDI 示例程序...
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
原文
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:
- From Visual Studio and select File thenNew and select theProjects tab.
- Choose MFC Appwizard(exe) as your project type and enter ‘MDISample’ as the project name.
Visual Studio New Dialog...
- For the first step, make sure that Multiple documents is selected then press the ‘Finish’ button.
Add Xtreme Command Bars components:
- Add the following line to your
StdAfx.h
file:Xtreme Toolkit Pro:
#include <XTToolkitPro.h> // Xtreme Toolkit Pro component library
- In your
MainFrm.h
file you need to change your base class to beCXTPMDIFrameWnd
for MDI applications orCXTPFrameWnd
for SDI applications:class CMainFrame : public CXTPMDIFrameWnd { ... };
- If you plan to override either
PreTranslateMessage
orOnWndMsg
make sure that you call theCXTPFrameWnd
orCXTPMDIFrameWnd
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::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) { // TODO: Add your specialized code here and/or call the base class return CXTPMDIFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo); }
- Add the following code to the
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
function: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 bars if (!InitCommandBars()) return -1; // Get a pointer to the command bars object. CXTPCommandBars* pCommandBars = GetCommandBars(); if(pCommandBars == NULL) { TRACE0("Failed to create command bars 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; }
MDI Sample Application...