




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验五 菜单、工具栏、状态栏程序设计【实验目的】1、 掌握主菜单程序设计方法2、 学会工具栏程序设计3、 掌握快捷菜单程序设计方法4、 学会增加状态栏窗格【实验内容】1、 建立一个单文档应用程序,添加菜单显示,其中包含“文本”和“图形”2个菜单项,分别输出文本和图形。参考代码:void CSDIDispView:OnText() / TODO: Add your command handler code hereCClientDC dc(this);dc.TextOut(20,20,我已经学会了如何设计菜单程序!);void CSDIDispView:OnPicture() / TODO: Add your command handler code hereCClientDC dc(this);CBrush *BrushOld,BrushNew;BrushNew.CreateSolidBrush(RGB(255,0,0); BrushOld=dc.SelectObject(&BrushNew); /选用画刷dc.Rectangle(50,50,250,150); dc.SelectObject(BrushOld); /还原画刷BrushNew.DeleteObject(); /释放画刷2、 为上述应用程序添加菜单控制功能,选中“文本”时,“图形”菜单项无效;当选中“图形”时,“文本”菜单项无效。参考代码:void CSDIDispView:OnText() / TODO: Add your command handler code hereCClientDC dc(this);dc.TextOut(20,20,我已经学会了如何设计菜单程序!);m_picture=false;void CSDIDispView:OnPicture() / TODO: Add your command handler code hereCClientDC dc(this);CBrush *BrushOld,BrushNew;BrushNew.CreateSolidBrush(RGB(255,0,0); BrushOld=dc.SelectObject(&BrushNew); /选用画刷dc.Rectangle(50,50,250,150); dc.SelectObject(BrushOld); /还原画刷BrushNew.DeleteObject(); /释放画刷m_text=false;void CSDIDispView:OnUpdateText(CCmdUI* pCmdUI) / TODO: Add your command update UI handler code herepCmdUI-Enable(m_text);void CSDIDispView:OnUpdatePicture(CCmdUI* pCmdUI) / TODO: Add your command update UI handler code herepCmdUI-Enable(m_picture);3、为上述应用程序工具栏添加两个工具按钮,单击第一个按钮,在视图窗口中弹出“打开文件”对话框,单击第二个按钮,在消息框中显示文本信息。参考代码:void CXiTi4_3View:OnText() / TODO: Add your command handler code hereMessageBox(我已经学会了使用默认工具栏了!);4、创建一个单文档应用程序,在状态栏中显示鼠标光标的坐标。(1)在MainFrm.h中将状态栏对象修改为publicpublic:CStatusBar m_wndStatusBar;(2)在MainFrm.cpp中添加状态栏窗格ID_INDICATOR_COR(3)在串表中设置添加窗格的字符串格式(4)添加WM_MOUSEMOVE消息及其相应函数void CXiTi4_4View:OnMouseMove(UINT nFlags, CPoint point) / TODO: Add your message handler code here and/or call defaultCString str;CMainFrame *pFrame=(CMainFrame*)AfxGetApp()-m_pMainWnd; CStatusBar *pStatus=&pFrame-m_wndStatusBar;str.Format(%d,%d,point.x,point.y);pStatus-SetPaneText(pStatus-CommandToIndex(ID_INDICATOR_COR),str);CView:OnMouseMove(nFlags, point);5、 建立一个单文档应用程序,添加“时间”主菜单项,包含时、分、秒三个菜单项,分别在视图窗口中显示当前系统时间的时、分和秒。添加相应的工具栏和快捷菜单,并在状态栏中显示系统时间。(1)添加菜单项、添加工具栏插入工具栏并编辑,添加工具栏类定义工具栏对象:CToolBar m_wndToolBar, m_wndToolBar1;加载工具栏:OnCreate( ):if (!m_wndToolBar1.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) |!m_wndToolBar1.LoadToolBar(IDR_TOOLBAR1)TRACE0(Failed to create toolbarn);return -1; / fail to createm_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);m_wndToolBar1.EnableDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_ANY);DockControlBar(&m_wndToolBar);DockControlBar(&m_wndToolBar1);设置时钟:SetTimer(1,1000,NULL);(2)添加状态栏添加状态栏窗格:ID_INDICATOR_TIME,设置字符串格式状态栏数据成员设置为公有成员void CMainFrame:OnTimer(UINT nIDEvent) / TODO: Add your message handler code here and/or call defaultCTime time;time=CTime:GetCurrentTime();/获得系统时间CString str=time.Format(%H:%M:%S);/将系统时间转换成时:分:秒格式的字符串m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_TIME),str);/更新时间窗格显示的时间内容CFrameWnd:OnTimer(nIDEvent);void CMainFrame:OnClose() / TODO: Add your message handler code here and/or call defaultKillTimer(1);/关闭计时器CFrameWnd:OnClose();(3)添加菜单消息响应定义CTime类的对象time添加菜单消息响应函数:void CSTimeView:OnSecond() / TODO: Add your command handler code heretime=CTime:GetCurrentTime();/获得系统时间CString str=time.Format(%S);/将系统时间转换成秒格式的字符串CClientDC dc(this);dc.TextOut(100,100,现在是+str+秒); void CSTimeView:OnHour() / TODO: Add your command handler code heretime=CTime:GetCurrentTime();/获得系统时间CString str=time.Format(%H);/将系统时间转换成时格式的字符串CClientDC dc(this);dc.TextOut(100,100,现在是+str+时); void CSTimeView:OnMinute() / TODO: Add your command handler code heretime=CTime:GetCurrentTime();/获得系统时间CString str=time.Format(%M);/将系统时间转换成分格式的字符串CClientDC dc(this);dc.TextOut(100,100,现在是+str+分); (4)添加快捷菜单设计快捷菜单添加WM_ContextMenu消息响应函数:void CSTimeView:OnContextMenu(CWnd*, CPoint point)/ CG: This block was added by the Pop-up Menu componentif (point.x = -1 & point.y = -1)/keystroke invocationCRect rect;GetClientRect(rect);ClientToScreen(rect);point = rect.TopLeft();point.Offset(5, 5);CMenu menu;VERIFY(menu.LoadMenu(CG_IDR_POPUP_STIME_VIEW);CMenu* pPopup = menu.GetSubMenu(0);ASSERT(pPopup != NULL);CWnd* pWndPopupOwner = this;while (pWndPopupOwner-GetStyle() & WS_CHILD)pWndPopupOwner = pWndPopupOwner-GetParent();pPopup-TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,pWndPopupOwner);6、 绘制正弦和余弦曲线,设置线型和颜色的变换。1)添加菜单添加工具栏资源添加快捷菜单资源2)加载工具栏:Mainfrm.h:CToolBar m_wndToolBar1;Mainfrm.cpp:Oncreate:if (!m_wndToolBar1.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) |!m_wndToolBar1.LoadToolBar(IDR_TOOLBAR1)TRACE0(Failed to create toolbarn);return -1; / fail to create/ TODO: Delete these three lines if you dont want the toolbar to/ be dockablem_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);m_wndToolBar1.EnableDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_ANY);DockControlBar(&m_wndToolBar);DockControlBar(&m_wndToolBar1);3)加载快捷菜单:添加视图类得wm_context消息void CP1View:OnContextMenu(CWnd* pWnd, CPoint point) / TODO: Add your message handler code hereCMenu menu,*pPopup;menu.LoadMenu(IDR_MENU1);/加载快捷菜单pPopup = menu.GetSubMenu(0);CWnd* pWndPopupOwner = this;pWndPopupOwner = pWndPopupOwner-GetParent();/显示快捷菜单pPopup-TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,point.x,point.y,pWndPopupOwner);4)添加视图类的成员变量及成员函数Cview.hconst double PI=3.1416;public: int m_able;int m_typ; int m_wid;COLORREF m_col;double calsin(int x);double calcos(int x);void drawcurve(CDC* pDC);void drawline(CDC* pDC, int x1,int y1,int x2,int y2);void drawaxis(CDC* pDC);Cview.cppCCurveView:CCurveView()/ TODO: add construction code herem_typ=0;m_col=RGB(0,0,0);m_wid=1;m_able=0;void CCurveView:OnDraw(CDC* pDC)CCurveDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ TODO: add draw code for native data heredrawcurve(pDC);double CCurveView:calsin(int x)double y; y=sin(x*PI/180);return y;double CCurveView:calcos(int x)double y; y=cos(x*PI/180);return y;void CCurveView:drawaxis(CDC *pDC)CString str;CFont myfont,*oldfont;myfont.CreatePointFont(80,Arial,pDC);oldfont=pDC-SelectObject(&myfont); drawline(pDC,50,10,50,290); drawline(pDC,45,150,780,150);for(int i=50;iTextOut(i,155,str);for(i=0;iTextOut(15,45+20*i,str);else pDC-TextOut(15,45+20*i,0);pDC-SelectObject(oldfont);myfont.DeleteObject();void CCurveView:drawline(CDC* pDC,int x1, int y1, int x2, int y2)pDC-MoveTo(x1,y1);pDC-LineTo(x2,y2);void CCurveView:drawcurve(CDC* pDC)drawaxis(pDC); CPoint point750;CPen mypen,*oldpen;mypen.CreatePen(PS_SOLID,m_wid,m_col);oldpen=pDC-SelectObject(&mypen);for(int i=0;i722;i+)pointi.x=i+50;if(m_typ=1)pointi.y=(int)(150-calsin(i)*100);else if(m_typ=2)pointi.y=(int)(150-calcos(i)*100);for(i=0;iSelectObject(oldpen);mypen.DeleteObject();5)添加菜单消息响应函数void CCurveView:OnColb() / TODO: Add your command handler code herem_col=RGB(0,0,255);Invalidate();void CCurveView:OnUpdateColb(CCmdUI* pCmdUI) / TODO: Add your command update UI handler code herepCmdUI-Enable(m_able);if(m_col=RGB(0,0,255)pCmdUI-SetCheck(true);elsepCmdUI-SetCheck(false);void CCurveView:OnColr() / TODO: Add your command handler code herem_col=RGB(255,0,0);Invalidate();void CCurveView:OnUpdateColr(CCmdUI* pCmdUI) / TODO: Add your command update UI handler code herepCmdUI-Enable(m_able);if(m_col=RGB(255,0,0)pCmdUI-SetCheck(true);elsepCmdUI-SetCheck(false);void CCurveView:OnCos() / TODO: Add your command handler code herem_typ=2;m_able=1;Invalidate();void CCurveView:OnSin() / TODO: Add your command handler code herem_typ=1;m_able=1; Invalidate();void CCurveView:OnWidsp2() / TODO: Add your command handler code herem_wid=2;Invalidate();void CCurveView:OnUpdateWidsp2(CCmdUI* pCmdUI) / TODO: Add your command update UI handler code herepCmdUI-Enable(m_able);if(m_wid=2)pCmdUI-SetCheck(true);elsepCmdUI-SetCheck(false);void CCurveView:OnWidsp3() / TODO: Add your command handler code herem_wid=3;Invalidate();void CCurveView
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 疝术后的康复治疗
- 青光眼病例分析
- 体育类广告讲解
- 农产品补血食品创新创业项目商业计划书
- 现金流风险预警系统创新创业项目商业计划书
- 医院核酸检测操作流程规范
- 物业招标审核方案(3篇)
- 商业保险规划方案(3篇)
- 基层贴面施工方案(3篇)
- 芯奢品牌课程讲解
- 黑土地知识科学普及-黑土地保护法宣贯课件
- 卷尺、直尺、角尺校验规程
- Englishpod-1-365-完美打印版内容
- 高边坡施工监理细则
- 学习适应性测验(AAT)(小学五、六年级)
- GB/T 35051-2018选煤厂洗水闭路循环等级
- 项目三 金属的塑性变形与再结晶
- 2022年重庆市水务资产经营有限公司校园招聘笔试试题及答案解析
- 急诊与灾难医学:昏迷课件
- 垃圾焚烧发电厂项目重点及难点施工方案
- 公路工程质量检验评定jtgf80-1
评论
0/150
提交评论