3 1 l l GDI Graphics Device InterfaceGDI

  • Slides: 46
Download presentation

3. 1 设备环境类和图形对象 l l 图形设备接口GDI (Graphics Device Interface,GDI) 设备环境类:CPaint. DC 、CWindow. DC 等

3. 1 设备环境类和图形对象 l l 图形设备接口GDI (Graphics Device Interface,GDI) 设备环境类:CPaint. DC 、CWindow. DC 等

CGdi. Object类的常用成员函数 函数 Attach Create. Stock. Object Delete. Temp. Map Detach Get. Safe. Handle

CGdi. Object类的常用成员函数 函数 Attach Create. Stock. Object Delete. Temp. Map Detach Get. Safe. Handle 含  义 链接一个Windows GDI对象到一个 CGdi. Object对象 返回Windows预定义的库存画笔、画刷 或字体的句柄 删除连接到CGdi. Object 删除任何由From. Handle所创建的临时 CGDIObject对象 使Windows GDI对象与CGDIObject对象 断开连接,并返回Windows GDI对象的 句柄 除非this为NULL时该成员函数返回NULL

CDC: : Select. Object函数原型 CPen* Select. Object( CPen* p. Pen ); CBrush* Select. Object(

CDC: : Select. Object函数原型 CPen* Select. Object( CPen* p. Pen ); CBrush* Select. Object( CBrush* p. Brush ); CFont* Select. Object( CFont* p. Font );

CPen类

CPen类

CPen类 void CMy. View: : On. Draw(CDC* p. DC) { CPen pen. Mountain, *p.

CPen类 void CMy. View: : On. Draw(CDC* p. DC) { CPen pen. Mountain, *p. Oldpen; pen. Mountain. Create. Pen(PS_SOLID, 3, RGB(125, 50, 0)); p. Oldpen=p. DC->Select. Object(&pen. Mountain); CPoint m_point. Mountain[5]; m_point. Mountain[0] = CPoint(100, 200); m_point. Mountain[1] = CPoint(200, 100); m_point. Mountain[2] = CPoint(300, 150); m_point. Mountain[3] = CPoint(400, 50); m_point. Mountain[4] = CPoint(600, 200); p. DC->Polyline(m_point. Mountain, 5); p. DC->Move. To(0, 200); p. DC->Line. To(700, 200); p. DC->Select. Object(p. Oldpen); }

CBrush类 l l BOOL Create. Solid. Brush ( COLORREF cr. Color ); BOOL Create.

CBrush类 l l BOOL Create. Solid. Brush ( COLORREF cr. Color ); BOOL Create. Hatch. Brush ( int n. Index, COLORREF cr. Color );  阴影风格   说 明 HS_BDIAGONAL 从左下角到右上角的45度斜线///// HS_CROSS 水平线与垂直线+++++ HS_DIAGCROSS 相互垂直的45度线XXXXX HS_FDIAGONAL 45度向下\\\ HS_HORIZONTAL 水平线----- HS_VERTICAL 垂直线|||||

void CMy. View: : On. Draw(CDC* p. DC) { CBrush *p. Old. Brush, brush.

void CMy. View: : On. Draw(CDC* p. DC) { CBrush *p. Old. Brush, brush. Sky, brush. Grass, brush. Moutain; CRect rect; Get. Client. Rect(&rect); brush. Sky. Create. Solid. Brush(RGB(127, 200, 255)); // 画天空 p. Old. Brush = p. DC->Select. Object(&brush. Sky); p. DC->Rectangle(rect); brush. Grass. Create. Solid. Brush(RGB(0, 255, 0)); // 画草地 p. DC->Select. Object(&brush. Grass); rect. top = 300; p. DC->Rectangle(rect);

brush. Moutain. Create. Solid. Brush(RGB(125, 50, 0)); p. DC->Select. Object(&brush. Moutain); CPoint m_point. Moutain[5];

brush. Moutain. Create. Solid. Brush(RGB(125, 50, 0)); p. DC->Select. Object(&brush. Moutain); CPoint m_point. Moutain[5]; m_point. Moutain[0] = CPoint(200, 300); m_point. Moutain[1] = CPoint(300, 200); m_point. Moutain[2] = CPoint(400, 250); m_point. Moutain[3] = CPoint(500, 150); m_point. Moutain[4] = CPoint(700, 300); p. DC->Polygon(m_point. Moutain, 5); p. DC->Select. Object(p. Old. Brush); }

使用CImage显示图片 (1)在项目中的stdafx. h文件加入     #include <atlimage. h> (2)视图类 CImage m_img. Body; (3)CA 09 View: :

使用CImage显示图片 (1)在项目中的stdafx. h文件加入     #include <atlimage. h> (2)视图类 CImage m_img. Body; (3)CA 09 View: : CA 09 View() { m_img. Body. Load("vc. bmp"); } (4)void CA 09 View: : On. Draw(CDC* p. DC)  { if(m_img. Body. Is. Null()) return ; else { int w=m_img. Body. Get. Width(); int h=m_img. Body. Get. Height(); m_img. Body. Draw(*p. DC, 0, 0, w, h); }  }

(1)在项目中的stdafx. h文件的末尾添加下列包含语句。   #include <atlimage. h> (2)修改视图类的定义(头文件),加入一个变量定义: class CMy. View : public CView { protected:

(1)在项目中的stdafx. h文件的末尾添加下列包含语句。   #include <atlimage. h> (2)修改视图类的定义(头文件),加入一个变量定义: class CMy. View : public CView { protected: // 仅从序列化创建 CMy. View(); DECLARE_DYNCREATE(CMy. View) // 属性 public: CMy. Doc* Get. Document() const; CImage m_img. Body; …(以下省略其它定义语句)

CMy. View: : CMy. View() { m_img. Body. Load("vc. jpg"); } void CMy. View:

CMy. View: : CMy. View() { m_img. Body. Load("vc. jpg"); } void CMy. View: : On. Draw(CDC* p. DC) { // 显示图像 int n. Width=m_img. Body. Get. Width(); int n. Height=m_img. Body. Get. Height(); m_img. Body. Bit. Blt(*p. DC, 0, 0, n. Width, n. Height, 0, 0, SRCCOPY); }

BOOL Bit. Blt( HDC h. Dest. DC, int x. Dest, int y. Dest, int

BOOL Bit. Blt( HDC h. Dest. DC, int x. Dest, int y. Dest, int n. Dest. Width, int n. Dest. Height, int x. Src, int y. Src, DWORD dw. ROP = SRCCOPY ); BOOL Stretch. Blt( HDC h. Dest. DC, int x. Dest, int y. Dest, int n. Dest. Width, int n. Dest. Height, int x. Src, int y. Src, int n. Src. Width, int n. Src. Height, DWORD dw. ROP = SRCCOPY );

class CMy. View : public CView { protected: // 仅从序列化创建 CMy. View(); DECLARE_DYNCREATE(CMy. View)

class CMy. View : public CView { protected: // 仅从序列化创建 CMy. View(); DECLARE_DYNCREATE(CMy. View) // 属性 public: CMy. Doc* Get. Document() const; // 定义CBitmap类对象以及位图的高度和宽度变量 CBitmap m_Bitmap; int m_n. Height; int m_n. Width; …(以下省略其它定义语句)

CMy. View: : CMy. View() { HBITMAP h. Bitmap=(HBITMAP)Load. Image(NULL, "vc. bmp", IMAGE_BITMAP, 0,

CMy. View: : CMy. View() { HBITMAP h. Bitmap=(HBITMAP)Load. Image(NULL, "vc. bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); m_Bitmap. Attach(h. Bitmap); BITMAP BM; m_Bitmap. Get. Bitmap(&BM); m_n. Width = BM. bm. Width; m_n. Height = BM. bm. Height; }

void CMy. View: : On. Draw(CDC* p. DC) { // 显示位图 CDC Mem. DC;

void CMy. View: : On. Draw(CDC* p. DC) { // 显示位图 CDC Mem. DC; Mem. DC. Create. Compatible. DC(NULL); Mem. DC. Select. Object(&m_Bitmap); p. DC->Bit. Blt(0, 0, m_n. Width, m_n. Height, &Mem. DC, 0, 0, SRCCOPY); }

3. 4 字体 l l l l BOOL Create. Font( int n. Height, //

3. 4 字体 l l l l BOOL Create. Font( int n. Height, // 字符逻辑高度 int n. Width, // 字体逻辑宽度 int n. Escapement, // 出口矢量与X轴的角度 int n. Orientation, // 字符基线与X轴的角度 int n. Weight, // 字体磅值 BYTE b. Italic, // 非 0则为斜体 BYTE b. Underline, // 非 0则加下划线 BYTE c. Strike. Out, // 非 0则加删除线 BYTE n. Char. Set, // 此字体的字符集 BYTE n. Out. Precision, // 输出精度 BYTE n. Clip. Precision, // 裁剪精度 BYTE n. Quality, // 输出质量 BYTE n. Pitch. And. Family, // 调距和字体族

显示字体 l CFont *p. Old. Font, font; l font. Create. Font(50, 0, FW_NORMAL, FALSE,

显示字体 l CFont *p. Old. Font, font; l font. Create. Font(50, 0, FW_NORMAL, FALSE, 0, GB 2312_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "楷体"); // 创建字体对象 l p. Old. Font = p. DC->Select. Object(&font); l p. DC->Set. Text. Color(RGB(255, 0, 0)); l p. DC->Text. Out(10, "Hello, VC++"); l p. DC->Select. Object(p. Old. Font);

3. 7 库存图形对象 virtual CGdi. Object* Select. Stock. Object( int n. Index ); �

3. 7 库存图形对象 virtual CGdi. Object* Select. Stock. Object( int n. Index ); � 形� 象 BLACK_PEN NULL_PEN WHITE_PEN BLACK_BRUSH WHITE_BRUSH DKGRAY_BRUSH LTGRAY_BRUSH NULL_BRUSH ANSI_FIXED_FONT ANSI_VAR_FONT �明 黑笔 空笔,不画� 或� 框 白笔 黑色刷子 白色刷子 暗灰刷子 灰色刷子 淡灰色刷子 空刷子,内部不填充 Windows固定� 角(�� 隔)系� 字 体

class CMy. View : public CView { int m_n. Color; CRect m_rect. Ellipse; protected:

class CMy. View : public CView { int m_n. Color; CRect m_rect. Ellipse; protected: // 仅从序列化创建 CMy. View(); ……(以下省略文档类的其他定义语句) public: afx_msg void On. LButton. Down(UINT n. Flags, CPoint point); }; CMy. View: : CMy. View() { m_rect. Ellipse = CRect(100, 300, 250); m_n. Color = 0; } void CMy. View: : On. Draw(CDC* p. DC) { switch(m_n. Color) {case 0: p. DC->Select. Stock. Object(GRAY_BRUSH); case 1: p. DC->Select. Stock. Object(LTGRAY_BRUSH); case 2: p. DC->Select. Stock. Object(BLACK_BRUSH); break; } p. DC->Ellipse(m_rect. Ellipse); //画椭圆 } break;

void CMy. View: : On. LButton. Down(UINT n. Flags, CPoint point) { if(m_rect. Ellipse.

void CMy. View: : On. LButton. Down(UINT n. Flags, CPoint point) { if(m_rect. Ellipse. Pt. In. Rect(point)) { m_n. Color = (m_n. Color+1)%3; Invalidate. Rect(m_rect. Ellipse); } CView: : On. LButton. Down(n. Flags, point); }

class CMy. View : public CView { protected: // 仅从序列化创建 CMy. View(); DECLARE_DYNCREATE(CMy. View)

class CMy. View : public CView { protected: // 仅从序列化创建 CMy. View(); DECLARE_DYNCREATE(CMy. View) // 属性 public: CMy. Doc* Get. Document() const; private: CImage m_bmp. Man; //创建位图对象 int m_n. Width; //位图宽度 int m_n. Height; //位图高度 CRect m_rect. Man; //位图矩形区域 int m_n. Step; //人每次走过的像素点 ……(以下省略文档类的其他定义语句) };

(4)修改视图类的构造函数 CMy. View: : CMy. View(){ m_n. Step=10; m_bmp. Man. Load("Man. bmp"); m_n. Width=m_bmp.

(4)修改视图类的构造函数 CMy. View: : CMy. View(){ m_n. Step=10; m_bmp. Man. Load("Man. bmp"); m_n. Width=m_bmp. Man. Get. Width(); m_n. Height=m_bmp. Man. Get. Height(); m_rect. Man=CRect(100, 100+m_n. Width, 100+m_n. Height); }

(5)修改视图类的On. Draw函数 void CMy. View: : On. Draw(CDC*p. DC) { int x=m_rect. Man. left;

(5)修改视图类的On. Draw函数 void CMy. View: : On. Draw(CDC*p. DC) { int x=m_rect. Man. left; int y=m_rect. Man. top; m_bmp. Man. Bit. Blt(*p. DC, x, y, m_n. Width, m_n. Height, 0, 0, SRCCOPY); }

void CMy. View: : On. Key. Down(UINT n. Char, UINT n. Rep. Cnt, UINT

void CMy. View: : On. Key. Down(UINT n. Char, UINT n. Rep. Cnt, UINT n. Flags) { CRect rect. Client; Get. Client. Rect(&rect. Client); CRect rect. Old=m_rect. Man; switch(n. Char) { case VK_UP: if(m_rect. Man. top>rect. Client. top) m_rect. Man+=CSize(0, -m_n. Step); break; case VK_DOWN: if(m_rect. Man. bottom<rect. Client. bottom) m_rect. Man+=CSize(0, m_n. Step); break; case VK_LEFT: if(m_rect. Man. left>rect. Client. left) m_rect. Man+=CSize(-m_n. Step, 0); break; case VK_RIGHT: if(m_rect. Man. right<rect. Client. right) m_rect. Man+=CSize(m_n. Step, 0); } rect. Old = rect. Old | m_rect. Man; Invalidate. Rect(rect. Old); CView: : On. Key. Down(n. Char, n. Rep. Cnt, n. Flags); }

class CMy. View : public CView { protected: // 仅从序列化创建 CMy. View(); DECLARE_DYNCREATE(CMy. View)

class CMy. View : public CView { protected: // 仅从序列化创建 CMy. View(); DECLARE_DYNCREATE(CMy. View) // 属性 public: CMy. Doc* Get. Document() const; CImage m_bmp. Explode; //创建位图对象 int m_n. Width; //位图宽度 int m_n. Height; //位图高度 CRect m_rect. Explode; //位图对象的大小 int m_n. State; //爆炸状态标记 ……(以下省略文档类的其他定义语句) };

CMy. View: : CMy. View() { m_n. State=0; m_bmp. Explode. Load("explosion. bmp"); m_n. Width=m_bmp.

CMy. View: : CMy. View() { m_n. State=0; m_bmp. Explode. Load("explosion. bmp"); m_n. Width=m_bmp. Explode. Get. Width()/15; m_n. Height=m_bmp. Explode. Get. Height(); m_rect. Explode=CRect(0, 0, m_n. Width, m_n. Height); }

void CMy. View: : On. Draw(CDC* p. DC) { if(m_n. State>0) { int x=m_rect.

void CMy. View: : On. Draw(CDC* p. DC) { if(m_n. State>0) { int x=m_rect. Explode. left; int y=m_rect. Explode. top; m_bmp. Explode. Bit. Blt(*p. DC, x, y, m_n. Width, m_n. He m_n. Width*(m_n. State-1), 0, SRCCOPY); } }

void CMy. View: : On. LButton. Down(UINT n. Flags, CPoint point) { Set. Timer(1,

void CMy. View: : On. LButton. Down(UINT n. Flags, CPoint point) { Set. Timer(1, 100, NULL); m_n. State=1; m_rect. Explode=CRect(point. x-m_n. Width/2, point. y-m_n point. x+m_n. Width/2, point. y+m_n. Height/2); Invalidate(); CView: : On. LButton. Down(n. Flags, point); }

void CMy. View: : On. Timer(UINT n. IDEvent) { if(m_n. State>0 && m_n. State<=15)

void CMy. View: : On. Timer(UINT n. IDEvent) { if(m_n. State>0 && m_n. State<=15) m_n. State++; else { m_n. State=0; Kill. Timer(1); } Invalidate. Rect(m_rect. Explode); CView: : On. Timer(n. IDEvent); }