VC6++教程实例(PPT)第4章

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

第4章菜单、快捷键和控制条内容摘要:1.菜单2.快捷菜单3.动态生成菜单4.工具条5.状态条返回目录4.1菜单学习目标1.响应菜单消息2.控制菜单状态3.使用快捷键4.1返回第4章程序的实现步骤1.利用AppWizard生成程序框架(MenuTest)2.编辑资源3.加入菜单消息处理函数4.菜单命令的更新控制4.1步骤1生成程序框架(MenuTest)1.项目名称:MenuTest2.选择单文档界面应用程序(Singledocument)4.1步骤2编辑资源1.编辑菜单资源画图Pop-up选中CaptionIDPrompt矩形(&R)\tCtrl+RID_DRAW_RECT在视中画矩形椭圆(&C)\tAlt+CID_DRAW_CIRCLE在视中画椭圆2.编辑快捷键资源IDModifiersKeyID_DRAW_RECTCtrlRID_DRAW_CIRCLAltC4.1步骤3加入菜单消息处理函数ClassNameObjectsIDsMessages1.CMenuTestViewID_DRAW_RECTCOMMAND2.CMenuTestViewID_DRAW_CIRCLECOMMAND4.1voidCMenuTestView::OnDrawRect(){CClientDCdc(this);dc.Rectangle(50,50,200,150);}voidCMenuTestView::OnDrawCircle(){CClientDCdc(this);dc.Ellipse(250,50,400,150);}步骤4菜单命令的更新控制1.在画图菜单中再添加两个菜单项CaptionIDPrompt允许画矩形ID_DRAW_ENABLE--------禁止画矩形ID_DRAW_DISABLE--------2.在视类中添加一个记录当前“矩形”菜单项状态的变量BOOLm_Enable;3.在构造函数中初始化数据成员m_Enablem_Enable=TRUE;4.1步骤4菜单命令的更新控制4.添加菜单响应函数ClassNameObjectsIDsMessages1.CMenuTestViewID_DRAW_ENABLECOMMAND2.CMenuTestViewID_DRAW_DISABLECOMMAND3.CMenuTestViewID_DRAW_RECTUPDATE_COMMAND_UI4.CMenuTestViewID_DRAW_ENABLEUPDATE_COMMAND_UI5.CMenuTestViewID_DRAW_DISABLEUPDATE_COMMAND_UI4.1voidCMenuTestView::OnUpdateDrawRect(CCmdUI*pCmdUI){pCmdUI-Enable(m_Enable);}步骤4菜单命令的更新控制4.1voidCMenuTestView::OnDrawDisable(){m_Enable=FALSE;}voidCMenuTestView::OnDrawEnable(){m_Enable=TRUE;}voidCMenuTestView::OnUpdateDrawEnable(CCmdUI*pCmdUI){pCmdUI-SetCheck(m_Enable?1:0);}voidCMenuTestView::OnUpdateDrawDisable(CCmdUI*pCmdUI){pCmdUI-SetCheck(m_Enable?0:1);}基本知识1.pCmdUI-Enable(true|false);2.pCmdUI-SetCheck(1|0);3.使用COMMAND消息响应菜单项的选择事件4.UPDATE_COMMAND_UI消息控制菜使用单项状态4.14.2快捷菜单学习目标1.实现快捷菜单4.2返回第4章程序的实现步骤1.编辑快捷菜单资源2.显示跟踪菜单3.添加菜单响应函数4.实现对话框功能4.2步骤1编辑快捷菜单资源添加菜单(IDR_POPUP_MENU)Popup1选中Pop-upCaptionID编辑矩形坐标ID_EDIT_RECT编辑椭圆坐标ID_EDIT_CIRCLE4.2步骤2显示快捷菜单添加响应函数ClassNameObjectsIDsMessagesCMainFrameCMainFrameWM_CONTEXTMENU4.2voidCMainFrame::OnContextMenu(CWnd*pWnd,CPointpoint){//TODO:AddyourmessagehandlercodehereCMenumenu;if(menu.LoadMenu(IDR_POPUP_MENU)){CMenu*pPopup=menu.GetSubMenu(0);pPopup-TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,this);}}步骤2显示快捷菜单4.2步骤3添加菜单消息处理函数(1)编辑对话框资源静态文本框,IDC_STATIC,左上角坐标(X1,Y1)静态文本框,IDC_STATIC,右下角坐标(X2,Y2)编辑框,IDC_EDIT_X1编辑框,IDC_EDIT_Y1编辑框,IDC_EDIT_X2编辑框,IDC_EDIT_Y2按扭控件,IDOK,确定按扭控件,IDCANCEL,取消4.2步骤3添加菜单消息处理函数(2)为对话框添加控件关联变量IDC_EDIT_X1,int,m_nX1IDC_EDIT_Y1,int,m_nX1IDC_EDIT_X2,int,m_nX1IDC_EDIT_Y2,int,m_nX1(3)在主框架中加入变量并初始化intm_nRectLeft;intm_nRectTop;intm_nRectRight;intm_nRectBottom;intm_nCircleLeft;intm_nCircleTop;intm_nCircleRight;intm_nCircleBottom;4.2CMainFrame::CMainFrame(){//TODO:addmemberinitializationcodeherem_nRectLeft=50;m_nRectTop=50;m_nRectRight=200;m_nRectBottom=150;m_nCircleLeft=50;m_nCircleTop=50;m_nCircleRight=200;m_nCircleBottom=150;}步骤3添加菜单消息处理函数4.2voidCMenuTestView::OnDrawRect(){CMainFrame*pMainFrame=(CMainFrame*)AfxGetApp()-GetMainWnd();CClientDCdc(this);dc.Rectangle(pMainFrame-m_nRectLeft,pMainFrame-m_nRectTop,pMainFrame-m_nRectRight,pMainFrame-m_nRectBottom);}voidCMenuTestView::OnDrawCircle(){CMainFrame*pMainFrame=(CMainFrame*)AfxGetApp()-GetMainWnd();CClientDCdc(this);dc.Ellipse(pMainFrame-m_nCircleLeft,pMainFrame-m_nCircleTop,pMainFrame-m_nCircleRight,pMainFrame-m_nCircleBottom);}步骤3添加菜单消息处理函数(4)修改画图函数4.2步骤3添加菜单消息处理函数(5)加入菜单消息处理函数voidCMenuTestView::OnEditRect(){CDlgEditdlg(1);dlg.DoModal();}voidCMenuTestView::OnEditCircle(){CDlgEditdlg(2);dlg.DoModal();}4.2步骤4实现对话框功能(1)修改构造函数CDlgEdit::CDlgEdit(inttype,CWnd*pParent/*=NULL*/):CDialog(CDlgEdit::IDD,pParent){//{{AFX_DATA_INIT(CDlgEdit)m_nX1=0;m_nX2=0;m_nY1=0;m_nY2=0;//}}AFX_DATA_INITm_nType=type;}(2)添加确定命令按扭响应函数OnOK()4.2步骤4实现对话框功能voidCDlgEdit::OnOK(){UpdateData(TRUE);CMainFrame*pMainFrame=(CMainFrame*)AfxGetApp()-GetMainWnd();if(m_nType==1){pMainFrame-m_nRectLeft=m_nX1;pMainFrame-m_nRectTop=m_nY1;pMainFrame-m_nRectRight=m_nX2;pMainFrame-m_nRectBottom=m_nY2;}elseif(m_nType==2){pMainFrame-m_nCircleLeft=m_nX1;pMainFrame-m_nCircleTop=m_nY1;pMainFrame-m_nCircleRight=m_nX2;pMainFrame-m_nCircleBottom=m_nY2;}CDialog::OnOK();}4.2基本知识1.OnContextMenu()函数2.CMenu类1.LoadMenu()函数2.TrackPopupMenu()函数3.AfxGetApp()函数4.CWinApp类的GetMainWnd()函数4.24.3动态菜单学习目标1.菜单动态增加与删除2.用户自定义消息3.动态菜单消息响应4.3返回第4章程序的实现步骤1.编辑菜单资源2.添加菜单响应函数3.加入动态菜单响应函数4.3步骤1编辑菜单资源为菜单(IDR_POPUP_MENU)添加两个菜单项:CaptionID1.Seprator2.增加椭圆控制菜单ID_ADD_MENU3.删除椭圆控制菜单ID_DELETE_MENU4.3步骤2添加菜单响应函数1.OnAddMenu()2.OnDeleteMenu()4.3步骤2添加菜单响应函数4.3voidCMainFrame::OnAddMenu(){//TODO:AddyourcommandhandlercodehereCStringmenu[2];menu[0]=允许画椭圆;menu[1]=禁止画椭圆;CWnd*pMainFrame=AfxGetMainWnd();CMenu*pMenuDyna=NULL;CMenu*pMenu=NULL;CMenu*pTopMenu=pMainFrame-GetMenu();intiPos;for(iPos=pTopMenu-GetMenuItemCount()-1;iPos=0;iPos--){pMenu=pTopMenu-GetSubMenu(iPos);if(pMenu&&pMenu-GetMenuItemID(0)==ID_DRAW_RECT){pMenuDyna=pMenu;break;}}for(iPos=pMenuDyna-GetMenuItemCount()-1;iPos=5;iPos--)pMenuDyna-DeleteMenu(iPos,MF_BYPOSITION);for(inti=0;i2;i++){pMenuDyna-AppendMenu(MF_STRING,WM_USER+10+i,menu[i]);}}步骤2添加菜单响应函数4.3voidCMainFrame::OnDeleteMenu(){//TODO:AddyourcommandhandlercodehereCWnd*pMa

1 / 54
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功