实验一图形程序设计基础.ppt
实验一 图形程序设计基础,一.概述:,本实验是在Microsoft Visual C+6.0平台上,建立一个Project,并在该Project的View文件内,找到Visual C+6.0自动创建的CView类的虚拟成员函数OnDraw(CDC*pDC),并在该函数内添加相应的绘图程序,绘图程序通过CDC类指针pDC调用相应的绘图土函数,来完成所需图形的绘制。因此,在此实验中,除了要掌握Visual C+6.0平台上编制图形程序的步骤之外,还应初步了解MFC的结构,尤其需要了解与绘图有关的相关类的关系和类中成员函数的调用方式。,MFC结构如下:,二实验的主要目的:,1、让学生掌握利用Microsoft Visual C+6.0平台,进行图形程序设计的基本方法与步骤;2、训练学生利用计算机分析和解决实际问题的能力;3、锻炼学生撰写科技实验报告的能力。,三实验步骤:,建立工程:打开Visual C+;选择FileNewProjectsMFC AppWizardexe,在Project Name内输入工程名(如FtistP),并在Location选择程序文件存储路径,最后点击OK。,2.选择欲创建的文档类型:,在Step1对话框中,选择Single Document或Multiple Documents或Dialog based中任意一项均可,但后续操作不一样。(如选Multiple Documents)点击Finish(若还有其他需要则选Next);,在如下New Project Information框内下点击OK。,3.编写图形程序:,在如上图所示的此新建的Project内,Workspace视窗下选择FileView,然后点击FirstP Files展开它,在Source Files中找到FirstPView.cpp文件,在该文件中找到CView类下的列函数:void CFirstPView:OnDraw(CDC*pDC)CFirstPDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);/TODO:add draw code for native data here(添加代码处),在该函数内添加相应的绘图程序代码,图形形状不作统一要求,可自由设计,代码自己编写,也可用教材上P20实例来练习。,教材上P20实例代码如下:/使用缺省画笔画了一条直线,画笔的属性是实线型、1个像素宽、黑色 pDC-MoveTo(100,100);pDC-LineTo(200,200);CPen*pOldPen;/申请一个画笔指针,用于保存当前设备环境下的画笔 CPen dashPen;/以下创建画笔并绘制直线/创建一个画笔,其属性是虚线型、1个像素宽、红色 dashPen.CreatePen(PS_DASH,1,RGB(255,0,0);/PS_SOLID:Pen is solid;/PS_DASH:Pen is dashed;PS_DOT:Pen is dotted;PS_DASHDOT:Pen has alternating dashes and dots/PS_DASHDOTDOT;PS_INSIDEFRAME:Pen is solid;PS_NULL:Pen is invisible pOldPen=pDC-SelectObject(/再次使用原画笔再绘制直线,4编译调试程序,直到通过运行后得到需要的结论。,注:若在:void CFirstPView:OnDraw(CDC*pDC)CFirstPDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);/TODO:add draw code for native data here。函数前添加下列函数,即可在相应线段中插入字符“0”,VOID CALLBACK DrawZero(int X,int y,LPARAM lpData)CDC*pDC;pDC=(CDC*)lpData;if(X%20=0)pDC-TextOut(X,y,_T(0);,再在书上代码:pDC-MoveTo(100,100);pDC-LineTo(200,200);下添加下列语句::LineDDA(100,100,200,200,(LINEDDAPROC)DrawZero,(long)pDC);/画“0”函数即可在直线段(100,100)到(200,200)上每隔20个段位长度处输出一个“0”符号。其运行结果如下图所示:,5.分析总结,并提交实验报告。(略),注:各函数说明如下。1CDC:MoveTo CPoint MoveTo(int x,int y);Return Value The x-and y-coordinates of the previous position as a CPoint object.Parametersx Specifies the logical x-coordinate of the new position.y Specifies the logical y-coordinate of the new position.Remarks Moves the current position to the point specified by x and y(or by point).,2CDC:LineTo BOOL LineTo(int x,int y);Return Value Nonzero if the line is drawn;otherwise 0.Parametersx Specifies the logical x-coordinate of the endpoint for the line.y Specifies the logical y-coordinate of the endpoint for the line.Remarks Draws a line from the current position up to,but not including,the point specified by x and y(or point).The line is drawn with the selected pen.The current position is set to x,y or to point.,3CPen:CreatePen BOOL CreatePen(int nPenStyle,int nWidth,COLORREF crColor);Return Value Nonzero,or the handle of a logical pen,if successful;otherwise 0.ParametersnPenStyle Specifies the style for the pen.For a list of possible values,see the nPenStyle parameter in the CPen constructor.nWidth Specifies the width of the pen.if this value is 0,the width in device units is always 1 pixel,regardless of the mapping mode.crColor Contains an RGB color for the pen.Remarks The CreatePen initializes a pen with the specified style,width,and color.The pen can be subsequently selected as the current pen for any device context.,4CDC:SelectObject CPen*SelectObject(CPen*pPen);Return Value A pointer to the object being replaced.This is a pointer to an object of one of the classes derived from CGdiObject.The return value is NULL if there is an error.ParameterspPen A pointer to a CPen object to be selected.Remarks Selects an object into the device context.,5、TextOut(x,y,_T(“0”);在点(x,y)处输出符号“0”。,本实验讲解完毕 谢谢!,