1 / 18

实验一 图形程序设计基础

实验一 图形程序设计基础. 一 . 概述:. 本实验是在 Microsoft Visual C++ 6.0 平台上,建立一个 Project ,并在该 Project 的 View 文件内,找到 Visual C++ 6.0 自动创建的 CView 类的虚拟成员函数 OnDraw(CDC* pDC) ,并在该函数内添加相应的绘图程序,绘图程序通过 CDC 类指针 pDC 调用相应的绘图土函数,来完成所需图形的绘制。

Download Presentation

实验一 图形程序设计基础

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 实验一 图形程序设计基础 一. 概述: 本实验是在Microsoft Visual C++ 6.0平台上,建立一个Project,并在该Project的View文件内,找到Visual C++ 6.0自动创建的CView类的虚拟成员函数OnDraw(CDC* pDC),并在该函数内添加相应的绘图程序,绘图程序通过CDC类指针pDC调用相应的绘图土函数,来完成所需图形的绘制。 因此,在此实验中,除了要掌握Visual C++ 6.0平台上编制图形程序的步骤之外,还应初步了解MFC的结构,尤其需要了解与绘图有关的相关类的关系和类中成员函数的调用方式。

  2. MFC结构如下:

  3. 二.实验的主要目的: 1、让学生掌握利用Microsoft Visual C++ 6.0平台,进行图形程序设计的基本方法与步骤; 2、训练学生利用计算机分析和解决实际问题的能力; 3、锻炼学生撰写科技实验报告的能力。

  4. 三.实验步骤: • 建立工程: • ①打开Visual C++; • ②选择File→New→Projects→MFC AppWizard[exe],在Project Name内输入工程名(如FtistP),并在Location选择程序文件存储路径,最后点击OK。

  5. 2. 选择欲创建的文档类型: 在Step1对话框中,选择Single Document或Multiple Documents或Dialog based中任意一项均可,但后续操作不一样。(如选Multiple Documents)点击Finish(若还有其他需要则选Next>);

  6. 在如下New Project Information框内下点击OK。

  7. 3. 编写图形程序:

  8. 在如上图所示的此新建的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实例来练习。

  9. 教材上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 (&dashPen); //选择新画笔,用pOldPen保留原画笔 pDC->LineTo(300,100); //使用新画笔绘制直线 pDC->SelectObject (pOldPen); //绘制完毕一定要恢复原画笔 pDC->LineTo (400,200); //再次使用原画笔再绘制直线

  10. 4.编译调试程序,直到通过运行后得到需要的结论。4.编译调试程序,直到通过运行后得到需要的结论。

  11. 注: 若在: 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")); } }

  12. 再在书上代码: pDC->MoveTo (100,100); pDC->LineTo (200,200); 下添加下列语句: ::LineDDA(100,100,200,200,(LINEDDAPROC)DrawZero,(long)pDC); //画“0”函数 即可在直线段(100,100)到(200,200)上每隔20个段位长度处输出一个“0”符号。 其运行结果如下图所示:

  13. 5. 分析总结,并提交实验报告。(略) ★注:各函数说明如下。 1.CDC::MoveTo  CPoint MoveTo( int x, int y ); Return Value The x- and y-coordinates of the previous position as a CPoint object. Parameters x 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).

  14. 2.CDC::LineTo  BOOL LineTo( int x, int y ); Return Value Nonzero if the line is drawn; otherwise 0. Parameters x 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.

  15. 3.CPen::CreatePen BOOL CreatePen( int nPenStyle, int nWidth, COLORREFcrColor ); Return Value Nonzero, or the handle of a logical pen, if successful; otherwise 0. Parameters nPenStyle 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.

  16. 4.CDC::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. Parameters pPen 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”。

  17. 本实验讲解完毕 谢谢!

More Related