您好,欢迎访问三七文档
VisualC++程序设计杨琦计算机教学实验中心西安交通大学授课内容3.1图形设备接口的基本概念3.2绘图工具类3.3画笔与画刷3.4位图3.5字体3.6绘图模式3.7库存图形对象3.8典型案例3.1设备环境类和图形对象图形设备接口GDI(GraphicsDeviceInterface,GDI)设备环境类:CPaintDC、CWindowDC等3.2绘图工具类CGdiObject类的常用成员函数函数含义Attach链接一个WindowsGDI对象到一个CGdiObject对象CreateStockObject返回Windows预定义的库存画笔、画刷或字体的句柄DeleteObject删除连接到CGdiObjectDeleteTempMap删除任何由FromHandle所创建的临时CGDIObject对象Detach使WindowsGDI对象与CGDIObject对象断开连接,并返回WindowsGDI对象的句柄GetSafeHandle除非this为NULL时该成员函数返回NULL;否则返回m_hObject3.3画笔与画刷CPen类:BOOLCPen::CreatePen(intnPenStyle,intnWidth,COLORREFcrColor);第1个参数是画笔样式,可取PS_SOLID创建实线笔PS_DASH创建由短线构成的虚线PS_DOT创建由点构成的虚线PS_DASHDOT创建由短线和点构成的虚线PS_DASHDOTDOT由短线、点、点构成的虚线PS_NULL创建空(空白)画笔CDC::SelectObject函数原型CPen*SelectObject(CPen*pPen);CBrush*SelectObject(CBrush*pBrush);CFont*SelectObject(CFont*pFont);CPen类CPen类voidCMyView::OnDraw(CDC*pDC){CPenpenMountain,*pOldpen;penMountain.CreatePen(PS_SOLID,3,RGB(125,50,0));pOldpen=pDC-SelectObject(&penMountain);CPointm_pointMountain[5];m_pointMountain[0]=CPoint(100,200);m_pointMountain[1]=CPoint(200,100);m_pointMountain[2]=CPoint(300,150);m_pointMountain[3]=CPoint(400,50);m_pointMountain[4]=CPoint(600,200);pDC-Polyline(m_pointMountain,5);pDC-MoveTo(0,200);pDC-LineTo(700,200);pDC-SelectObject(pOldpen);}CBrush类BOOLCreateSolidBrush(COLORREFcrColor);BOOLCreateHatchBrush(intnIndex,COLORREFcrColor);阴影风格说明HS_BDIAGONAL从左下角到右上角的45度斜线/////HS_CROSS水平线与垂直线+++++HS_DIAGCROSS相互垂直的45度线XXXXXHS_FDIAGONAL45度向下\\\\\\HS_HORIZONTAL水平线-----HS_VERTICAL垂直线|||||效果图:【例3-1】绘制蓝天、草地和大山voidCMyView::OnDraw(CDC*pDC){CBrush*pOldBrush,brushSky,brushGrass,brushMoutain;CRectrect;GetClientRect(&rect);brushSky.CreateSolidBrush(RGB(127,200,255));//画天空pOldBrush=pDC-SelectObject(&brushSky);pDC-Rectangle(rect);brushGrass.CreateSolidBrush(RGB(0,255,0));//画草地pDC-SelectObject(&brushGrass);rect.top=300;pDC-Rectangle(rect);brushMoutain.CreateSolidBrush(RGB(125,50,0));pDC-SelectObject(&brushMoutain);CPointm_pointMoutain[5];m_pointMoutain[0]=CPoint(200,300);m_pointMoutain[1]=CPoint(300,200);m_pointMoutain[2]=CPoint(400,250);m_pointMoutain[3]=CPoint(500,150);m_pointMoutain[4]=CPoint(700,300);pDC-Polygon(m_pointMoutain,5);pDC-SelectObject(pOldBrush);}使用CImage显示图片(1)在项目中的stdafx.h文件加入#includeatlimage.h(2)视图类CImagem_imgBody;(3)CA09View::CA09View(){m_imgBody.Load(vc.bmp);}(4)voidCA09View::OnDraw(CDC*pDC){if(m_imgBody.IsNull())return;else{intw=m_imgBody.GetWidth();inth=m_imgBody.GetHeight();m_imgBody.Draw(*pDC,0,0,w,h);}}(1)在项目中的stdafx.h文件的末尾添加下列包含语句。#includeatlimage.h(2)修改视图类的定义(头文件),加入一个变量定义:classCMyView:publicCView{protected://仅从序列化创建CMyView();DECLARE_DYNCREATE(CMyView)//属性public:CMyDoc*GetDocument()const;CImagem_imgBody;…(以下省略其它定义语句)CMyView::CMyView(){m_imgBody.Load(vc.jpg);}voidCMyView::OnDraw(CDC*pDC){//显示图像intnWidth=m_imgBody.GetWidth();intnHeight=m_imgBody.GetHeight();m_imgBody.BitBlt(*pDC,0,0,nWidth,nHeight,0,0,SRCCOPY);}BOOLStretchBlt(HDChDestDC,intxDest,intyDest,intnDestWidth,intnDestHeight,intxSrc,intySrc,intnSrcWidth,intnSrcHeight,DWORDdwROP=SRCCOPY);BOOLBitBlt(HDChDestDC,intxDest,intyDest,intnDestWidth,intnDestHeight,intxSrc,intySrc,DWORDdwROP=SRCCOPY);【例3-2】直接显示一张位图文件classCMyView:publicCView{protected://仅从序列化创建CMyView();DECLARE_DYNCREATE(CMyView)//属性public:CMyDoc*GetDocument()const;//定义CBitmap类对象以及位图的高度和宽度变量CBitmapm_Bitmap;intm_nHeight;intm_nWidth;…(以下省略其它定义语句)CMyView::CMyView(){HBITMAPhBitmap=(HBITMAP)LoadImage(NULL,vc.bmp,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);m_Bitmap.Attach(hBitmap);BITMAPBM;m_Bitmap.GetBitmap(&BM);m_nWidth=BM.bmWidth;m_nHeight=BM.bmHeight;}voidCMyView::OnDraw(CDC*pDC){//显示位图CDCMemDC;MemDC.CreateCompatibleDC(NULL);MemDC.SelectObject(&m_Bitmap);pDC-BitBlt(0,0,m_nWidth,m_nHeight,&MemDC,0,0,SRCCOPY);}3.5字体CFont类的常用成员函数函数含义CreateFont用指定的特征初始化一个CFont对象CreateFontIndirect用LOGFONT结构中给定的特征初始化一个CFont对象CreatePointFont用指定的高和打印字体初始化一个CFont对象FromHandle当给定一个WindowHFONT句柄时,返回一个指向CFont对象指针GetLogFont用与CFont对象关联的逻辑字体有关信息填充LOGFONToperatorHFONT返回与CFont对象关联的WindowGDI字体句柄3.4字体BOOLCreateFont(intnHeight,//字符逻辑高度intnWidth,//字体逻辑宽度intnEscapement,//出口矢量与X轴的角度intnOrientation,//字符基线与X轴的角度intnWeight,//字体磅值BYTEbItalic,//非0则为斜体BYTEbUnderline,//非0则加下划线BYTEcStrikeOut,//非0则加删除线BYTEnCharSet,//此字体的字符集BYTEnOutPrecision,//输出精度BYTEnClipPrecision,//裁剪精度BYTEnQuality,//输出质量BYTEnPitchAndFamily,//调距和字体族LPCTSTRlpszFacename);//字体的字型名显示字体CFont*pOldFont,font;font.CreateFont(50,0,0,0,FW_NORMAL,FALSE,FALSE,0,GB2312_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,楷体);//创建字体对象pOldFont=pDC-SelectObject(&font);pDC-SetTextColor(RGB(255,0,0));pDC-TextOut(10,10,Hello,VC++);pDC-SelectObject(pOldFont);3.6绘图模式intSetROP2(intnDrawMode);绘图模式说明R2_BLACKR2_WHITER2_NOPR2_NOTR2_NOTCOPYPENR2_COPYPENR2_XORPEN无论画笔色如何,只用黑色绘图;无论画笔色如何,只用白色绘图;无论画笔色如何,用无色笔绘图;用与背景色相反的颜色绘图;用与画笔色相反的颜色绘图;用画笔色绘图;对画笔色和背景色作异或(XOR)运算。思考题在例3-1中添加如下代码,效果如何?pDC-SetROP2(R2_BLACK);或者:pDC-SetROP2(R2_XORPEN);3.7库存图形对象virtualCGdiObject*SelectStockObject(intnIndex);图形对象说明BLACK_PENNULL_PENWHITE_PENBLACK_BRUSHWHITE_BRUSHDKGRAY_BRUSHGRAY_BRUSHLTGRAY_BRUSH
本文标题:图形设备接口
链接地址:https://www.777doc.com/doc-1310199 .html