C++ MFC 实验报告.docx

上传人:太** 文档编号:96997710 上传时间:2024-04-09 格式:DOCX 页数:26 大小:60.33KB
返回 下载 相关 举报
C++ MFC 实验报告.docx_第1页
第1页 / 共26页
C++ MFC 实验报告.docx_第2页
第2页 / 共26页
点击查看更多>>
资源描述

《C++ MFC 实验报告.docx》由会员分享,可在线阅读,更多相关《C++ MFC 实验报告.docx(26页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、result! =miniume(num l,num2);CDC* pDC二GetDC();CString strl;str.Format(_T(%d),resultl);pDC-TextOut(200,15O,strl);菜单项2.1.2.1 在菜单中添加一个选项显示谙在此处健人文件也)编辑更)视图92.1.2.2 为菜单项添加一个事件处理函数以弹出对话框在view头文件中添加一个变量MyDialog dig;在事件处理函数中添加代码void CEx21 View:OnDialog() (/ TODO:在此添加命令处理程序代码dlg.DoModal();)2.1.2.3 结2.1.3.1 U

2、pdateData(true);为传值函数,将输入的文本赋予变量。2.1.3.2 CDC* pDC=GetDC();获取view类的OnDraw函数功能。2.1.3.3 变量result , resultl 为int型,应该用CString strl;str.Format(_T(%d),result 1);语句转化成CString型2.1.3.4 pDC-TextOut(200,l5O,str 1);一次只能输出一个值,分2个按钮输出结果。2.1.3.5 dlg.DoModal();语句触发对话框。实验三3.1 编写一个单文档应用程序Ex5。在主菜单中增加【曲线】、【线宽】和【颜色】 3个菜单

3、项,【曲线】菜单中包含【正弦】和【余弦】两个菜单项;【线宽】菜单 中包含【线宽2】和【线宽3】两个菜单项;【颜色】菜单中包含【红色】和【蓝 色】两个菜单项。为该应用程序创建一个新工具栏和一个快捷菜单,工具栏包含 4个按钮,分别与菜单项【线宽2】、【线宽3】、【红色】和【蓝色】相对应,快 捷菜单上有【正弦】、【余弦】、【线宽2】、【线宽3】、【红色】和【蓝色】6个菜 单项。在状态栏中增加一个窗格,用来显示曲线的类型、线宽以及颜色的提示信3.2 实验步骤321制作菜单项,快捷菜单项,按钮项将对应选项ID分别设为正弦 ID.S1NE余弦 ID_COSINE线宽 2 ID_DRAW_LINE2线宽 3

4、 ID_DRAW_LINE3红色 ID_SETLINE_RED蓝色 ID_S ETLINE_B LUE3.2.2 为菜单项添加click事件在view类的头文件中添加变量:public:int OperationType;/曲线类型int LineType; 线宽int ColorType;/颜色void CEx5View:0nSine() (/ TODO:在此添加命令处理程序代码OperationType=l;OnDraw(NULL);this-Invalidate(true);void CEx5View:OnCosine()/ TODO:在此添加命令处理程序代码OperationType=

5、2;OnDraw(NULL);this-Invalidate(true);)void CEx5View:OnDrawLine2()(/ TODO:在此添加命令处理程序代码LineType=5;OnDraw(NULL);this-Invalidate(true);)void CEx5View:OnDrawLine3()/ TODO:在此添加命令处理程序代码 LineType=16;OnDraw(NULL);this-Invalidate(true);void CEx5View:OnSetlineRed()(/ TODO:在此添加命令处理程序代码ColorType=2;OnDraw(NULL);t

6、his-Invalidate(true);void CEx5View:OnSetlineBlue()(/ TODO:在此添加命令处理程序代码ColorType=3;OnDraw(NULL);this-Invalidate(true);)为快捷菜单添加代码:在mainframe的头文件中添加:public:CToolBar m_wndMYToolBar;在mainframe.cpp中的OnCreate函数中添加:if (!m_wndMYToolBar.CreateEx(this, TBSTYLE.FLAT, WS.CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIP

7、PER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) | !m_wndToolBar.LoadToolBar(IDR_TOOLBARl)(TRACE。(未能创建工具栏n”);return-1;/未能创建)/TODO:如果不需要可停靠工具栏,则删除这三行m_wndMYToolBar.EnabIeDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_ANY);DockControlBar(&m_wndMYToolBar);在 mainframe 类中重写函数 OnContextMenuvoid CMa

8、inFrame:OnContextMenu(CWnd* pWnd, CPoint point)/TODO:在此处添加消息处理程序代码CMenu menu;if(menu.LoadMenu(IDR_POPUP_MENU) (CMenu *pPopup=menu.GetSubMenu(0);point.x, point.y,this);pPopup-TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,3.2.3 添加画法代码在view类的OnDraw函数中添加:void CEx5View:OnDraw(CDC* pDC) (CEx5Doc* pDoc 二 Ge

9、tDocument();ASSERT_VALID(pDoc);if (!pDoc) return;CClientDC pc(this);pc.SetMapMode(MM_TEXT);pc.SetWindowOrg(CPoint(-300,-300);pc.MoveTo(0,0);pc.LineTo(420,0);pc.MoveTo(0,0);pc.LineTo(0,-220);/pc-TextOut(50,10,1,r,);CStringArray s;s.SetSize(8,-l);s0=,r,;sl=,2,;s2=3,;s3=,4;s4=,5;s5=6,;s6=7,;s7=,8;fbr(i

10、nt i=l ;i=8;i+)pc.MoveTo(i*50,-l 0);pc.LineTo(i*50,+10);pc.TextOut(i*50,10,si-l);)int x,y,j;float a;pc.MoveTo(0,0);CPen Pen;switch (ColorType) ( case 2:Pen.CreatePen(PS_SOLID,LineType,RGB(255,0,0);break;case 3:Pen.CreatePen(PS_SOLID,LineType,RGB(0,0,255); break;CPen *OldPen=pc.SelectObject(&Pen);swi

11、tch(OperationType) (for(j=0;j=400;j+)(x二j;a=j;y=-50*sin(a/50);pc.LineTo(x,y);break ;for(j=0;jGetAt(position);StuName=student-StuName;StuGrade=student-StuGrade;StuAddress=student-StuAddress;StuNumber=student-StuNumber;elseGetlnfo();)UpdateData(false);)void CEx41 View:GetInfo()StuName=MH;StuGrade=O;St

12、uAddress=,1H;StuNumber=O;UpdateData(false);(CDialog*)this)-GotoDlgCtrl(GetDlgItem(IDC_STU_NAME);)void CEx41 View:Insertstudent(POSITION position) if(UpdateData(true) Student* student=new Student();student-StuName=StuName;student-StuGrade=StuGrade;student-StuAddress=StuAddress;student-StuNumber=StuNu

13、mber;m_position=m_pList-InsertAfter(m_position, student);4.1.1.3 添加消息处理函数 DoDataExchange OnlnitialUpdate 和 OnUpdatevoid CEx41 View:DoDataExchange(CDataExchange* pDX) (/TODO:在此添加专用代码和/或调用基类CFormView:DoDataExchange(pDX);DDX_Text(pDX, IDC_STU_NAME, StuName);DDV_MaxChars(pDX, StuName, 30);DDX_Text(pDX,

14、IDC_STU_GRADE, StuGrade);DDV_MinMaxInt(pDX, StuGrade, 0, 100);DDX_Text(pDX, IDC_STU_ADRESS, StuAddress);DDV_MaxChars(pDX, StuAddress, 50);DDX_Text(pDX, IDC_STU_NUMBER, StuNumber); DDV_MinMaxInt(pDX, StuNumber, 0, 999999);)void CEx41 View:OnInitialUpdate()(/TODO:在此添加专用代码和/或调用基类 m_pList=GetDocument()-

15、GetList();CFormView:OnInitialUpdate();void CEx41 View:OnUpdate(CView* pSender, LPARAM IHint, CObject* pHint) (/TODO:在此添加专用代码和/或调用基类m_position=m_pList-GetHeadPosition();)4.1.1.4 为控件clear添加事件处理函数void CEx41 View:OnBnClickedStuClear()/ TODO:在此添加控件通知处理程序代码Getlnfo();制作一个菜单项学生信息话在此处键入insertdeleteFirstPrevi

16、 ousNextLast话在此处键入为菜单项添加消息处理函数Command 型void CEx41 View:OnStuInsert()/ TODO:在此添加命令处理程序代码TRACE(Entering CEx41 View:OnStuInsertn);InsertStudent(m_position);GetDocument()-SetModifiedFlag();GetDocument()-UpdateAllViews(this);void CEx41 View:OnStuDelete()/ TODO:在此添加命令处理程序代码POSITION pos;i f(pos=m_position)

17、! =NULL) m_pList-GetNext(pos); if(pos=NULL)pos=m_pList-GetHeadPosition(); if(pos=m_position)pos=NULL;Getlnfo(pos);Student* stu=m_pList-GetAt(Disposition);m_pList-RemoveAt(m_position);delete stu;m_position=pos;GetDocument()-SetModifiedFlag();GetDocument()-UpdateAllViews(this);void CEx41 View:OnStuFir

18、st()(/ TODO:在此添加命令处理程序代码if(! m_pList-IsEmpty() m_position=m_pList-GetHeadPosition();Getlnfo(m_position);void CEx41 View:OnStuPrevious()(/ TODO:在此添加命令处理程序代码POSITION pos;if(pos=m_position) !=NULL) m_pList-GetPrev(pos); if(pos)Getlnfo(pos);m_position=pos;void CEx41 View:OnStuNext()(/ TODO:在此添加命令处理程序代码

19、POSITION pos;if(pos=m_position)! =NULL) m_pList-GetNext(pos); if(pos)Getlnfo(pos);m_position=pos;实验一1.1 显示一首诗。在view类的ondraw函数中添加代码如下:void CExl 1 View:OnDraw(CDC* pDC) (CExl I Doc* pDoc 二 GetDocumentO;ASSERT_VALID(pDoc);if (!pDoc) return;/ TODO:在此处为本机数据添加绘制代码CString s4;s0=人生得意须尽欢,莫使金樽空对,月。sl二两岸青山相对出,

20、孤帆一片日边来。s2=孤帆远影碧空尽,惟见长江天际流。”;s3=飞流直下三千尺,疑是银河落九天。”;for(int i=0;iTextOut(l 00,100+20*i,si);)1.2 画一个坐标系,画函数y = sin(x),x(-2i,2)的图像。在view类的ondraw函数中添加代码如下:void CEx 12View:OnDraw(CDC5fs pDC) (CExl2Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);if (!pDoc) return;/ TODO:在此处为本机数据添加绘制代码pDC-SetMapMode(MM_TEXT);p

21、DC-SetWindowOrg(CPoint(-370,-250);pDC-MoveTo(0,0);pDC-LineTo(420,0);pDC-MoveTo(0,0);pDC-LineTo(0,-220);pDC-MoveTo(0,0);pDC-LineTo(-420,0);pDC-MoveTo(0,0);pDC-LineTo(0,220);void CEx41 View:OnStuLast()/ TODO:在此添加命令处理程序代码if(! m_pList-IsEmpty()m_position=m_pList-GetTailPosition();Getlnfo(m_position);Upd

22、ate 型void CEx41 View:OnUpdateStuFirst(CCmdUI *pCmdUI) (/ TODO:在此添加命令更新用户界面处理程序代码POSITION pos;pos=m_pList-GetHeadPosition();pCmdUI-Enable(m_position!=NULL)&(pos!=m_position); )void CEx41 View:OnUpdateStuLast(CCmdUI *pCmdUI) (/ TODO:在此添加命令更新用户界面处理程序代码POSITION pos;pos=m_pList-GetTailPosition();pCmdUI-E

23、nable(m_position!=NULL)&(pos!=m_position);)void CEx41 View:OnUpdateStuDelete(CCmdUI *pCmdUI) (/ TODO:在此添加命令更新用户界面处理程序代码 pCmdUI-Enable(m_position!=NULL);4. 2对附件3加以修改,增加对颜色,笔宽的处理(参考附件2)做以下修改:4. 2. 1在view类中加入2个变量protected:CStroke*m_pStrokeCur;/ the stroke in progressCPointm_ptPrev;/ the last mouse pt i

24、n the stroke in progress4. 2. 2加入3个消息处理函数:void CScribbleView:OnLButtonDown(UINT, CPoint point) (/ Pressing the mouse button in the view window starts a new strokem_pStrokeCur = GetDocument()-NewStroke();/ Add first point to the new strokem_pStrokeCur-m_pointArray.Add(point);SetCapture(); / Capture t

25、he mouse until button up.m_ptPrev = point; / Serves as the MoveTo() anchor point for the/ LineTo() the next point, as the user drags the/ mouse.return;)void CScribbleView:OnLButtonUp(UINT, CPoint point) (/ Mouse button up is interesting in the Scribble application/ only if the user is currently draw

26、ing a new stroke by dragging/ the captured mouse.if (GetCapture() != this)return; / If this window (view) didnt capture the mouse, / then the user isnt drawing in this window.CScribbleDoc* pDoc 二 GetDocument();CClientDC dc(this);CPen* pOldPen 二 dc.SelectObject(pDoc-GetCurrentPen();dc.MoveTo(m_ptPrev

27、);dc.LineTo(point);dc.SelectObject(pOldPen);m_pStrokeCur-m_pointArray.Add(point);ReleaseCapture(); / Release the mouse capture established at / the beginning of the mouse drag.return;)void CScribbleView:OnMouseMove(UINT, CPoint point) (/ Mouse movement is interesting in the Scribble application/ onl

28、y if the user is currently drawing a new stroke by dragging/ the captured mouse.if (GetC叩ture() != this) return; / If this window (view) didnt capture the mouse,/ then the user isnt drawing in this window.CClientDC dc(this);m_pStrokeCur-m_pointArray.Add(point);/ Draw a line from the previous detecte

29、d point in the mouse/ drag to the current point.CPen* pOldPen = dc.SelectObject(GetDocument()-GetCurrentPen();dc.MoveTo(m_ptPrev);dc.LineTo(point);dc.SelectObject(pOldPen);m_ptPrev = point;return;)4. 2. 3在OnDraw函数中加入代码:void CScribbleView:OnDraw(CDC* pDC) CScribbleDoc* pDoc = GetDocument();ASSERT_VAL

30、ID(pDoc);/ The view delegates the drawing of individual strokes to/ CStroke:DrawStroke().CTypedPtrList& strokeList = pDoc-m_strokeList;POSITION pos = strokeList.GetHeadPosition();while (pos != NULL) (CStroke* pStroke = strokeList.GetNext(pos);pStroke-DrawStroke(pDC);实验五5. 1实验内容(1)使用单文档应用研究程序及分裂视图方法,

31、左窗格使用CformView,添加两个列表框,一个设 定颜色红、绿、兰;一个设定sin()、cos();右窗格使用Cview,画一个坐标系,显示正余弦图 像。(2)参考msdn示例之mdi;在多文档应用程序框架下使用菜单分别打开两个不同的文档。一个 文档及相关视图用来进行诗行显示;另一个实现正、余弦曲线显示。.诗行显示,正、余弦曲线 显示由菜单控制。5.2制作过程521建立一个工程Curve并在原工程中选择添力口一现有项一CurveView.h/CurveView.cpp在MainFrm.cpp中添加#include CurveView.h在 CurveView.cpp 中添加#include

32、 nstdafx.hinclude ”Ex5.h”#include Ex5View.h#include MainFrm.h#include nEx5Doc.hn#include nmath.hn4CurveView.hl1tl#pragma once上方添加:include nEx5Doc.h5.2.1.1 CurveView.cpp5.2.1.1.1 在Curve类中添加2个变量public:int m_string;int color;在视图类的头文件中加上#include math.h5.2. L1.2在OnDraw函数中添加画sin和cos曲线的代码void CCurve:OnDraw

33、(CDC* pDC) (CEx5Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);if(!pDoc)return;/TODO:在此处为本机数据添加绘制代码画出坐标系CRect rectClient;GetClientRect(rectClient);pDC-SetMapMode (MM.ANISOTROPIC);pDC-SetWindowExt (1000,1000);pDC-SetViewportExt (rectClient.right ,-rectClient.bottom);pDC-SetViewportOrg (rectClient.right

34、 /2,rectClient.bottom /2);/pDC-Ellipse (-500,500,500,-500);pDC-MoveTo (-500,0);pDC-LineTo (500,0);pDC-MoveTo (500,0);pDC-LineTo (490,10);pDC-MoveTo (500,0);pDC-LineTo (490,-10);pDC-MoveTo (0,-500);pDC-LineTo (0,500);pDC-MoveTo (0,500);pDC-LineTo (-10,490);pDC-MoveTo (0,500);pDC-LineTo (10,490);for(i

35、nt i=-4;iv=4;i+) (CString str;str.Format (_T(%d),i);pDC-MoveTo(i*100,-5);pDC-LineTo(i*100,+5);pDC-TextOutW (i* 100,-5,str);)for(int i=-l;iMoveTo(-5,i*100);pDC-LineTo(+5,i*100);pDC-TextOutW (+5,i*lOO,str);)m_string=pDoc-m_string;color=pDoc-color;CPen pen;选择颜色switch(color)case 0:pen.CreatePen(PS_SOLID

36、,3,RGB(255,0,0); break;pen.CreatePen(PS_SOLID,3,RGB(0,0,255); break;pen.CreatePen(PS_SOLID,3,RGB(0,255,0); break;)CPen* oldPen=pDC-SelectObject(&pen);选择线形 switch(m_string)case 0:pDC-MoveTo (-400,(int) 100*sin(float)-4);for(int i=-400;iLineTo (i,y);break;case 1:pDC-MoveTo(-400,(int)100*cos(float)-4);

37、for(int i=-400;iLineTo (i,y);break;)pDC-SelectObject(oldPen);在MainFrame类中重写函数OnCreateClient,在头文件中加入 public:CSplitterWnd m_wndSplitter;在M ainFrame.cpp中加入头文件#include nCurveView.hinclude HEx5View.hn并添加代码:BOOL CMainFrame:OnCreateClient(LPCREATESTRUCT /*lpcs*/,CCreateContext* pContext) (/*return m_wndSpl

38、itter.Create(this,2, 2,/TODO:调整行数和列数CSize(10, 10),/ TODO:调整最小窗格大小pContext);*/VERIFY(m_wndSplitter.CreateStatic(this,l,2);VERIFY(m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CEx5View),CSize(l 00,100),pContext);VERIFY(m_wndSplitter.CreateView(O,l ,RUNTIME_CLASS(CCurveView),CSize(l 00,100),pContext); ret

39、urn true;)5.2.3 制作一个对话框Buttonl设置Vertical ScrolTrue设置3个控件ID依次为:IDC.LIST1IDC.LIST2IDC_BUTTON1为3个控件添加Ex5View的事件处理函数:void CEx5View:OnBnClickedButton 1()(/ TODO:在此添加控件通知处理程序代码CWnd * pwnd;pwnd=GetDIgItem( IDC_STATIC);CDC* pDC=pwnd-GetDC ();pDC-Ellipse (0,0,100,100);void CEx5 View:OnLbnSelchangeList 1 ()/

40、TODO:在此添加控件通知处理程序代码CEx5Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);if(!pDoc)return;CListBox *pCtrl = (CListBox *)GetDlgItem( IDC_LIST1 );m_string=pCtrl-GetCurSel();pDoc-m_string =m_string;pDoc-Update All Views (NULL);void CEx5View:OnLbnSelchangeList2()(/ TODO:在此添加控件通知处理程序代码CEx5Doc* pDoc 二 GetDocume

41、nt();ASSERT_VALID(pDoc);if (!pDoc)return;CListBox *pCtrl = (CListBox *)GetDlgItem( IDC.LIST2 );col or=pC tri -GetC urS el ();pDoc-color =color;pDoc-UpdateAHViews (NULL);)重写函数 OnlrdtialUpdatevoid CEx5View:OnInitialUpdate()(CFormView:OnInitialUpdate();GetParentFrame()-RecalcLayout();ResizeParentToFit();CListBox *ptrl=(CListBox*)GetDlgItem(IDC_LISTl);ptrl-AddString(_T(Hsin(x);ptrl-AddString(_T(cos(x);ptrl-SetCurSel(O);CListBox *ptrl2=(CListBox*)GetDlgItem(I

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 解决方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁