CMenu 类的应用 添加子菜单项AppendMenu,插入子菜单项InsertMenu,删除菜单DeleteMenu
//
CMenu::AppendMenu
BOOL AppendMenu( UINT nFlags, UINT nIDNewItem = 0, LPCTSTR lpszNewItem = NULL );
BOOL AppendMenu( UINT nFlags, UINT nIDNewItem, const CBitmap* pBmp );
资源类对象都有一个成员变量来保存关联上的资源句柄
查看CMenu类,它有个成员变量m_hMenu:Specifies the handle to the Windows menu attached to the CMenu object.
//------ 追加菜单,在已菜单后面追加一个子菜单 用函数CMenu::AppendMenu
CMenu menu;
menu.CreatePopupMenu(); //初始化对象menu --- 创建一个空的弹出菜单
GetMenu()->AppendMenu(MF_POPUP,(UINT)menu.m_hMenu,"AppendMenu");
menu.Detach(); //把菜单资源与局部对象menu脱离,以免menu析构时,清除菜单.
//--
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::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 } // 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); //------ 追加菜单,在已菜单后面追加一个子菜单 用函数CMenu::AppendMenu CMenu menu; menu.CreatePopupMenu(); //初始化对象menu --- 创建一个空的弹出菜单 GetMenu()->AppendMenu(MF_POPUP,(UINT)menu.m_hMenu,"AppendMenu"); menu.Detach(); //把菜单资源与局部对象menu脱离,以免menu析构时,清除菜单. return 0; }
//------
//------ 插入菜单,在已有Popup菜单之间插入一个子菜单 用函数CMenu::InsertMenu
//------ 插入菜单,在已有Popup菜单之间插入一个一个子菜单 用函数CMenu::InsertMenu CMenu menu; menu.CreatePopupMenu(); //初始化对象menu --- 创建一个空的弹出菜单 GetMenu()->InsertMenu(2,MF_BYPOSITION|MF_POPUP,(UINT)menu.m_hMenu,"InsertMenu"); menu.Detach(); //把菜单资源与局部对象menu脱离,以免menu析构时,清除菜单.
//---
//------ 插入菜单项,用函数CMenu::InsertMenu,再用对象menu的成员函数AppendMenu插入菜单项
//------ 插入菜单项,用函数CMenu::InsertMenu,再用对象menu的成员函数AppendMenu插入菜单项 CMenu menu; menu.CreatePopupMenu(); //初始化对象menu --- 创建一个空的弹出菜单 GetMenu()->InsertMenu(2,MF_BYPOSITION|MF_POPUP,(UINT)menu.m_hMenu,"InsertMenu"); menu.AppendMenu(MF_STRING,110,"汇编"); //menu对象已经赋予了一个菜单资源,可用其成员函数操作菜单 menu.AppendMenu(MF_STRING,111,"C++"); menu.AppendMenu(MF_STRING,111,"JAVA"); // 在 "文件"子菜单的末尾追加一个菜单项 GetMenu()->GetSubMenu(0)->AppendMenu(MF_STRING,112,"---文件END---"); // 在 "文件"子菜单的中间插入一个菜单项:在OPEN的上面插入一个"---PrevOpen---" GetMenu()->GetSubMenu(0)->InsertMenu(ID_FILE_OPEN,MF_BYCOMMAND|MF_STRING,113,"---PrevOpen---"); menu.Detach(); //把菜单资源与局部对象menu脱离,以免menu析构时,清除菜单.
//---
//---
//------ 删除子菜单,子菜单项
对照上面的图......
GetMenu()->DeleteMenu(1,MF_BYPOSITION);//删除 "编辑" GetMenu()->GetSubMenu(0)->DeleteMenu(1,MF_BYPOSITION);//删除 "文件"菜单中的"---PrevOpen---"子菜单项
全部代码:
// MainFrm.cpp : implementation of the CMainFrame class // #include "stdafx.h" #include "Menu2.h" #include "MainFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMainFrame IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{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 END_MESSAGE_MAP() static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, }; ///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destruction CMainFrame::CMainFrame() { // TODO: add member initialization code here } CMainFrame::~CMainFrame() { } int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::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 } // 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); //------ 追加菜单,在已菜单后面追加一个子菜单 用函数CMenu::AppendMenu /* CMenu menu; menu.CreatePopupMenu(); //初始化对象menu --- 创建一个空的弹出菜单 GetMenu()->AppendMenu(MF_POPUP,(UINT)menu.m_hMenu,"AppendMenu"); menu.Detach(); //把菜单资源与局部对象menu脱离,以免menu析构时,清除菜单. */ //------ 插入菜单,在已有Popup菜单之间插入一个子菜单 用函数CMenu::InsertMenu /* CMenu menu; menu.CreatePopupMenu(); //初始化对象menu --- 创建一个空的弹出菜单 GetMenu()->InsertMenu(2,MF_BYPOSITION|MF_POPUP,(UINT)menu.m_hMenu,"InsertMenu");//2是子菜单出现的位置(index=2) menu.Detach(); //把菜单资源与局部对象menu脱离,以免menu析构时,清除菜单. */ //------ 插入菜单项,用函数CMenu::InsertMenu,再用对象menu的成员函数AppendMenu插入菜单项 CMenu menu; menu.CreatePopupMenu(); //初始化对象menu --- 创建一个空的弹出菜单 GetMenu()->InsertMenu(2,MF_BYPOSITION|MF_POPUP,(UINT)menu.m_hMenu,"InsertMenu"); menu.AppendMenu(MF_STRING,110,"汇编"); //menu对象已经赋予了一个菜单资源,可用其成员函数操作菜单 menu.AppendMenu(MF_STRING,111,"C++"); menu.AppendMenu(MF_STRING,111,"JAVA"); // 在 "文件"子菜单的末尾追加一个菜单项 GetMenu()->GetSubMenu(0)->AppendMenu(MF_STRING,112,"---文件END---"); // 在 "文件"子菜单的中间插入一个菜单项:在OPEN的上面插入一个"---PrevOpen---" GetMenu()->GetSubMenu(0)->InsertMenu(ID_FILE_OPEN,MF_BYCOMMAND|MF_STRING,113,"---PrevOpen---"); menu.Detach(); //把菜单资源与局部对象menu脱离,以免menu析构时,清除菜单. //------ 删除子菜单,子菜单项 GetMenu()->DeleteMenu(1,MF_BYPOSITION);//删除 "编辑" GetMenu()->GetSubMenu(0)->DeleteMenu(1,MF_BYPOSITION);//删除 "文件"菜单中的"---PrevOpen---"子菜单项 return 0; } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CMainFrame diagnostics #ifdef _DEBUG void CMainFrame::AssertValid() const { CFrameWnd::AssertValid(); } void CMainFrame::Dump(CDumpContext& dc) const { CFrameWnd::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers