您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 06第六章 MFC程序设计-菜单
MFC程序设计菜单作者:李季主要内容菜单分类菜单创建菜单加载与显示响应菜单命令命令范围更新菜单项更新范围自制菜单创建上下文菜单菜单分类顶层菜单弹出式菜单–下拉式菜单–上下文菜单–层叠菜单自制菜单菜单创建编程方法,调用CreateMenu、InsertMenu等其他CMenu函数初始化定义菜单内容的数据结构,调用CMenu::LoadMenuIndirect创建菜单资源,在程序运行时加载生成的菜单菜单加载和显示CFrameWnd::Create–Create(NULL,_T(“MyApplication”),WS_OVERLOPPEDWINDOW,rectDefault,NULL,MAKEINTRESOURCE(IDR_MAINFRAME));CFrameWnd::LoadFrame–LoadFrame(IDR_MAINFRAME,WS_OVERLOPPEDWINDOW,NULL,NULL);CMenu::LoadMenu、CMenu::SetMenu–CMenumenu;Menu.LoadMenu(IDR_MAINFRAME);SetMenu(&menu);Menu.Detach();响应菜单命令WM_INITMENU:显示最高级菜单项之前WM_INITMENUPOPUP:显示子菜单项之前WM_MENUSELECT:报告加亮条在菜单中的最新位置WM_COMMAND–ON_COMMAND(ID_FILE_NEW,OnFileNew)–ON_COMMAND(ID_FILE_OPEN,OnFileOpen)–ON_COMMAND(ID_FILE_EXIT,OnFileExit)命令范围ON_COMMAND(ID_SHAPE_CIRCLE,OnShapeCircle)ON_COMMAND(ID_SHAPE_TRIANGLE,OnShapeTriangle)ON_COMMAND(ID_SHAPE_SQUARE,OnShapeSquare)…voidCMainWindow::OnShapeCircle(){m_nCurrentShape=0;}voidCMianWindow::OnShapeTriangle(){m_nCurrentShape=1;}voidCMainWindow::OnShapeSquare(){m_nCurrentShape=2;}命令范围ON_COMMAND(ID_SHAPE_CIRCLE,OnShape)ON_COMMAND(ID_SHAPE_TRIANGLE,OnShape)ON_COMMAND(ID_SHAPE_SQUARE,OnShape)…voidCMainWindow::OnShape(){UINTnShapeID=(UINT)LOWORD(GetCurrentMessage()-wParam);m_nCurrentShape=nShapeID–ID_SHAPE_CIRCLE;}命令范围ON_COMMAD_RANGE(ON_SHAPE_CIRCLE,ON_SHAPE_SQUARE,OnShape)…voidCMainWindow::OnShape(UINTnID){m_nCurrentShape=nID–ID_SHAPE_CIRCLE;}更新菜单项voidCMainWindow::OnShape(UINTnID){CMenu*pMenu=GetMenu();pMenu-CheckMenuItem(m_nCurrentShape+ID_SHAPE_CIRCLE,MF_UNCHECKED);pMenu-CheckMenuItem(nID,MF_CHECKED);m_nCurrentShape=nID-ID_SHAPE_CIRCLE);}更新菜单项ON_WM_INITMENUPOPUP()…voidCMainWindow::OnInitMenuPopup(CMenu*pPopupMenu,UINTnIndex,BOOLbSysMenu){if(!bSysMenu&&(nIndex==SHAPE_MENU_INDEX)){pPopupMenu-CheckMenuItem(ID_SHAPE_CIRCLE,MF_UNCHECKED);pPopupMenu-CheckMenuItem(ID_SHAPE_TRIANGLE,MF_UNCHECKED);pPopupMenu-CheckMenuItem(ID_SHAPE_SQUARE,MF_UNCHECKED);pPopupMenu-CheckMenuItem(m_nCurrentShape+ID_SHAPE_CIRCLE,MF_CHECKED);}}更新菜单项ON_COMMAD_RANGE(ID_SHAPE_CIRCLE,ID_SHAPE_SQUARE,OnShape)ON_UPDATE_COMMAND_UI(ID_SHAPE_CIRCLE,OnUpdateShapeCircle)ON_UPDATE_COMMAND_UI(ID_SHAPE_TRIANGLE,OnUpdateShapeTriangle)ON_UPDATE_COMMAND_UI(ID_SHAPE_SQUARE,OnUpdateShapeSquare)…voidCMainWindow::OnShape(UINTnID){m_nCurrentShape=nID–ID_SHAPE_CIRCLE;}voidCMainWindow::OnUpdateShapeCircle(CCmdUI*pCmdUI){pCmdUI-SetCheck(m_nCurrentShape==0);}voidCMainWindow::OnUpdateShapeTriangle(CCmdUI*pCmdUI){pCmdUI-SetCheck(m_nCurrentShape==1);}voidCMainWindow::OnUpdateShapeSquare(CCmdUI*pCmdUI){pCmdUI-SetCheck(m_nCurrentShape==2);}更新菜单项函数说明CCmdUI::Enable使菜单项有效或无效CCmdUI::SetCheck选中或取消选中的菜单项CCmdUI::SetRadio给菜单项添加或删除单选标记CCmdUI::SetText改变菜单项的正文更新范围ON_COMMAND_RANGE(ID_SHAPE_CIRCLE,ID_SHAPE_SQUARE,OnShape)ON_UPDATE_COMMAND_UI_RANGE(ID_SHAPE_CIRCLE,ID_SHAPE_SQUARE,OnUpdateShapeUI)…voidCMainWindow::OnShape(UINTnID){m_nCurrentShape=nID–ID_SHAPE_CIRCLE;}voidCMainWindow::OnUpdateShapeUI(CCmdUI*pCmdUI){pCmdUI-SetRadio(pCmdUI-m_nID–ID_SHAPE_CIRCLE==m_nCurrentShape);}自制菜单创建第一步:调用CMenu::ModifyMenu实现传统菜单项到自制菜单项的转化–CMenu*pMenu=newCMenu;pMenu-ModifyMenu(menuID,MF_OWNERDRAW,menuID);自制菜单创建第二步:添加WM_MEASUREITEM消息映射和相应的处理函数以及相关的消息-位图映射项–OnMeasureItem(intnIDCtl,LPMEASUREITEMSTRUCTlpMeasureItemStruct){设置菜单项高度和宽度}自制菜单创建第三步:添加WM_DRAWITEM消息映射和相应的处理函数–在这里实施绘制自制菜单的工作上下文菜单创建和加载上下文菜单–CMenumenu;menu.LoadMenu(IDR_CONTEXTMENU);CMenu*pContextMenu=menu.GetSubMenu(0);上下文菜单添加WM_CONTEXTMENU消息映射和相应处理函数在OnContextMenu()中首先确定弹出范围然后调用TrackPopupMenu()在指定位置显示上下文菜单
本文标题:06第六章 MFC程序设计-菜单
链接地址:https://www.777doc.com/doc-3144751 .html