实验一 图形程序设计基础

18
实实 实实实实实实实实 . 实实实实实实实 Microsoft Visual C++ 6. 0 实实实 实实实 实 ,一 Project 实实实 Project 实 View 实实实实实 Visual C++ 6.0 实实实实实 CView 实实实实实实实实 OnDr aw(CDC* pDC) 实实实实实实实实实实实实实实实实 实实实实实实 ,, CDC 实 实实 pDC 实实实实实实实实实实 实实实实实实实实实实实 ,。 实实 实实实实实实 实实实实实 ,, Visual C++ 6.0 实 实实实实实实实实实实实实实 实实实实实实 MFC 实实实 实实实实实实实实实实实 实实实实实实实实实实实实实实实实实实实

Upload: jemima-young

Post on 03-Jan-2016

155 views

Category:

Documents


11 download

DESCRIPTION

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

TRANSCRIPT

Page 1: 实验一    图形程序设计基础

实验一 图形程序设计基础

一 . 概述:

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

因此,在此实验中,除了要掌握 Visual C++ 6.0

平台上编制图形程序的步骤之外,还应初步了解 MFC的结构,尤其需要了解与绘图有关的相关类的关系和类中成员函数的调用方式。

Page 2: 实验一    图形程序设计基础

MFC 结构如下:

Page 3: 实验一    图形程序设计基础

二.实验的主要目的:

1 、让学生掌握利用 Microsoft Visual C++ 6.0 平台,进行图形程序设计的基本方法与步骤;

2 、训练学生利用计算机分析和解决实际问题的能力; 3 、锻炼学生撰写科技实验报告的能力。

Page 4: 实验一    图形程序设计基础

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

Page 5: 实验一    图形程序设计基础

2. 选择欲创建的文档类型:

在 Step1 对话框中,选择 Single Document 或 Multiple Documents 或 Dialog based 中任意一项均可,但后续操作不一样。(如选 Multiple Documents )点击 Finish (若还有其他需要则选 Next> );

Page 6: 实验一    图形程序设计基础

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

Page 7: 实验一    图形程序设计基础

3. 编写图形程序:

Page 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 实例来练习。

Page 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); // 再次使用原画笔再绘制直线

Page 10: 实验一    图形程序设计基础

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

Page 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"));}

}

Page 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” 符号。 其运行结果如下图所示:

Page 13: 实验一    图形程序设计基础

5. 分析总结,并提交实验报告。(略)

★ 注:各函数说明如下。1 . CDC::MoveTo 

CPoint MoveTo( int x, int y );

Return Value The x- and y-coordinates of the previous position as a CPoi

nt 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).

Page 14: 实验一    图形程序设计基础

2 . CDC::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 l

ine.y Specifies the logical y-coordinate of the endpoint for the l

ine.Remarks Draws a line from the current position up to, but not inclu

ding, 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.

Page 15: 实验一    图形程序设计基础

3 . CPen::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 dev

ice 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, a

nd color. The pen can be subsequently selected as the current pen for any device context.

Page 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.

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” 。

Page 17: 实验一    图形程序设计基础
Page 18: 实验一    图形程序设计基础