当前位置:首页 > 使用BCG菜单和工具栏
一、在对话框中添加菜单资源
1. 先按9.6中将工程变成BCG工程。
2. 添加一个CCmdFrame类。其继承自CBCGPFrameWnd。修改其构造函数为
CCmdFrame(CBCGPDialog* pDlg),为其添加成员变量CBCGPDialog* m_pDlg,添加成员函数void SetMenuBar (CBCGPMenuBar* pMenuBar),添加消息处理函数virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)。 其头文件主要如下:
class CCmdFrame : public CBCGPFrameWnd {
public: CCmdFrame(CBCGPDialog* pDlg);
// Attributes protected: CBCGPDialog* m_pDlg; // Operations public: void SetMenuBar (CBCGPMenuBar* pMenuBar);
// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CCmdFrame) public: virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo); //}}AFX_VIRTUAL
// Implementation protected: virtual ~CCmdFrame(); // Generated message map functions //{{AFX_MSG(CCmdFrame) // NOTE - the ClassWizard will add and remove member functions here. //}}AFX_MSG DECLARE_MESSAGE_MAP() };
3. 在CCmdFrame类中添加相应的函数。
CCmdFrame::CCmdFrame(CBCGPDialog* pDlg) : m_pDlg (pDlg) {
}
BOOL CCmdFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) { ASSERT_VALID (m_pDlg); return m_pDlg->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo); }
class CMyFrameImpl : public CBCGPFrameImpl { CMyFrameImpl() : CBCGPFrameImpl (NULL) {} friend class CCmdFrame; };
void CCmdFrame::SetMenuBar (CBCGPMenuBar* pMenuBar) { ((CMyFrameImpl&)m_Impl).m_pMenuBar = pMenuBar; }
4. 在***Dlg.h中添加一个CMyMenuBar类。
class CMyMenuBar : public CBCGPMenuBar { virtual BOOL LoadState (LPCTSTR /*lpszProfileName*/ = NULL, int /*nIndex*/ = -1, UINT /*uiID*/ = (UINT) -1) { return TRUE; } virtual BOOL SaveState (LPCTSTR /*lpszProfileName*/ = NULL, int /*nIndex*/ = -1, UINT /*uiID*/ = (UINT) -1) { return TRUE; } };
5. 在.rc中添加菜单资源:
// Menu //
IDR_MENU1 MENU PRELOAD DISCARDABLE BEGIN
POPUP \ BEGIN
MENUITEM \ ID_FILE_NEW MENUITEM \ ID_FILE_OPEN MENUITEM \ ID_FILE_SAVE MENUITEM \ ID_FILE_SAVE_AS MENUITEM SEPARATOR
MENUITEM \ ID_FILE_PRINT MENUITEM \Pre&view\ ID_FILE_PRINT_PREVIEW
MENUITEM \ ID_FILE_PRINT_SETUP
MENUITEM SEPARATOR
MENUITEM \File\ ID_FILE_MRU_FILE1, GRAYED
MENUITEM SEPARATOR
MENUITEM \ ID_APP_EXIT END
POPUP \ BEGIN
MENUITEM \ ID_EDIT_UNDO MENUITEM SEPARATOR
MENUITEM \ ID_EDIT_CUT MENUITEM \ ID_EDIT_COPY MENUITEM \ ID_EDIT_PASTE END
POPUP \ BEGIN
MENUITEM \ ID_APP_ABOUT END END
在对话框资源中加入控件:
CONTROL \| NOT WS_VISIBLE,0,0,319,14
也可以通过插入>>资源来创建一个菜单资源,然后加入到工程中,这样就会自动生成相应的.rc文件代码,也不用在Resource.h中添加代码。 6. 在Resource.h中添加相应的资源:
#define IDR_MENU1 1002 #define IDC_MENU_LOCATION 1004 7. 在C***Dlg类中添加相应的成员变量。
CStatic m_wndMenuBarLocation;(在AFX_VIRTUAL中) CMyMenuBar m_wndMenuBar; CCmdFrame *m_pMenuFrame;
8. 在***Dlg.cpp中的DoDataExchange函数中添加:
DDX_Control(pDX, IDC_MENU_LOCATION, m_wndMenuBarLocation)。 在OnInitDialog中加入菜单创建的一些代码: // Create menu bar: m_wndMenuBar.Create (this); m_wndMenuBar.SetControlVisualMode (this);
CMenu menu;
menu.LoadMenu (IDR_MENU1); m_wndMenuBar.CreateFromMenu (menu.GetSafeHmenu (), TRUE, TRUE); m_wndMenuBar.SetBarStyle ( m_wndMenuBar.GetBarStyle () &
~(CBRS_GRIPPER | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT)); // Set menu bar position and size: CRect rectMenuBar; m_wndMenuBarLocation.GetWindowRect (&rectMenuBar); ScreenToClient (&rectMenuBar); m_wndMenuBar.SetWindowPos (&wndTop, rectMenuBar.left, rectMenuBar.top, rectMenuBar.Width (), rectMenuBar.Height (), SWP_NOACTIVATE); m_pMenuFrame = new CCmdFrame (this); m_pMenuFrame->Create (NULL, _T(\ m_pMenuFrame->ShowWindow (SW_HIDE); m_pMenuFrame->SetMenuBar (&m_wndMenuBar); m_wndMenuBar.SetOwner (m_pMenuFrame); BCGCBProSetTopLevelFrame (m_pMenuFrame); m_wndToolBar.Create (this); m_wndToolBar.SetControlVisualMode (this); m_wndToolBar.LoadToolBar (IDR_TOOLBAR, 0, 0, TRUE /* Locked bar */); m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY); m_wndToolBar.SetBarStyle ( m_wndToolBar.GetBarStyle () & ~(CBRS_GRIPPER | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT)); 9. 完成后编译运行。
二、在对话框中添加工具栏资源。
1. 先按9.6中将工程变成BCG工程。
2. 添加一个CCmdFrame类。其继承自CBCGPFrameWnd。修改其构造函数为
CCmdFrame(CBCGPDialog* pDlg),为其添加成员变量CBCGPDialog* m_pDlg,添加成员函数void SetMenuBar (CBCGPMenuBar* pMenuBar),添加消息处理函数virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)。 其头文件主要如下:
class CCmdFrame : public CBCGPFrameWnd {
public: CCmdFrame(CBCGPDialog* pDlg);
共分享92篇相关文档