您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > windows程序设计实验指导书
实验四、WindowsGDI编程实验目的:1、了解windowsGDI设备环境的获取与设置。2、了解windowsGDI绘图对象CPen,CBrush,CFont等的相关基本操作。3、掌握建立简单图形交互的图形数据管理和鼠标工具设计等。实验要求:建立一个windowsSDI(单文档)应用程序,并编辑建立图形属性设置的无模式对话框、简单图形工具条等。实现直线、矩形、椭圆、多边形的交互绘制及文字显示,并能进行图形属性设置。实验可以选择竞赛作业题目,则本次的基本操作实验可以不做,竞赛作业可在课程后期交上,计算成绩有加分因素。竞赛作业:分析微软Office系列软件的绘图工具箱组件的图形编辑功能和操作特点,抽象出其数据组织的结构体系,并分析其需要的鼠标工具类型,设计出相应合理的数据结构和鼠标工具,模拟实现简单几何图形的绘制工具(能交互设置图形属性、有图形成组操作功能,可以实现图形的简单几何变换为佳)。实验指导:一、在VC++6.0环境中建立一个单文档工程。1、通过资源编辑器编辑如下所示对话框资源和工具条资源。注意生成对话框类后,为几个组合框添加字符串变量,为Spin控件添加控制变量,并为几个组合对话框添加初始数据:映射模式:MM_ANISOTROPICMM_HIENGLISHMM_HIMETRICMM_ISOTROPICMM_LOENGLISHMM_LOMETRICMM_TEXTMM_TWIPS画笔风格:PS_SOLIDPS_DASHPS_DOTPS_DASHDOTPS_DASHDOTDOTPS_NULLPS_INSIDEFRAME画刷风格:BS_HATCHEDBS_SOLID线刷风格:HS_BDIAGONALHS_CROSSHS_DIAGCROSSHS_FDIAGONALHS_HORIZONTALHS_VERTICAL为生成的对话框类添加构造函数和变量:.h文件CGraphPropertyDlg(CView*pView);GDI_ExeView*m_pView;.cpp文件CGraphPropertyDlg::CGraphPropertyDlg(CView*pView){m_pView=(GDI_ExeView*)pView;}2、在View类中添加数据成员:public://无模式对话框CGraphPropertyDlg*pDlg;intm_nMapMode;//映射模式LOGBRUSHm_lBrush;//用来交互设置CDC画刷属性的逻辑画刷LOGPENm_lPen;//用来交互设置CDC画笔属性的逻辑画笔COLORREFm_cFont;//用来交互设置CDC字体颜色LOGFONTm_lFont;//用来交互设置CDC字体属性的逻辑字体private://将字符串变量与整形变量之间建立映射关系,用于对话框的数据交互CMapint,int,CString,CStringm_mapMode;CMapint,int,CString,CStringm_mapPen;CMapint,int,CString,CStringm_mapBrush;CMapint,int,CString,CStringm_mapHBrush;View类中数据成员的初始化:CGDI_ExeView::CGDI_ExeView(){//TODO:addconstructioncodehere//初始化映射模式的值-字符串映射表m_mapMode.SetAt(MM_ANISOTROPIC,MM_ANISOTROPIC);m_mapMode.SetAt(MM_HIENGLISH,MM_HIENGLISH);m_mapMode.SetAt(MM_HIMETRIC,MM_HIMETRIC);m_mapMode.SetAt(MM_ISOTROPIC,MM_ISOTROPIC);m_mapMode.SetAt(MM_LOENGLISH,MM_LOENGLISH);m_mapMode.SetAt(MM_LOMETRIC,MM_LOMETRIC);m_mapMode.SetAt(MM_TEXT,MM_TEXT);m_mapMode.SetAt(MM_TWIPS,MM_TWIPS);//初始化画笔样式的值-字符串映射表m_mapPen.SetAt(PS_SOLID,PS_SOLID);m_mapPen.SetAt(PS_DASH,PS_DASH);m_mapPen.SetAt(PS_DOT,PS_DOT);m_mapPen.SetAt(PS_DASHDOT,PS_DASHDOT);m_mapPen.SetAt(PS_DASHDOTDOT,PS_DASHDOTDOT);m_mapPen.SetAt(PS_NULL,PS_NULL);m_mapPen.SetAt(PS_INSIDEFRAME,PS_INSIDEFRAME);//初始化画刷样式的值-字符串映射表m_mapBrush.SetAt(BS_HATCHED,BS_HATCHED);m_mapBrush.SetAt(BS_SOLID,BS_SOLID);m_mapBrush.SetAt(BS_NULL,BS_NULL);//初始化线模式画笔样式的值-字符串映射表m_mapHBrush.SetAt(HS_BDIAGONAL,HS_BDIAGONAL);m_mapHBrush.SetAt(HS_CROSS,HS_CROSS);m_mapHBrush.SetAt(HS_DIAGCROSS,HS_DIAGCROSS);m_mapHBrush.SetAt(HS_FDIAGONAL,HS_FDIAGONAL);m_mapHBrush.SetAt(PS_DASHDOTDOT,PS_DASHDOTDOT);m_mapHBrush.SetAt(HS_HORIZONTAL,HS_HORIZONTAL);m_mapHBrush.SetAt(HS_VERTICAL,HS_VERTICAL);//初始化映射模式,画笔、画刷、字体样式m_nMapMode=MM_TEXT;m_lBrush.lbColor=RGB(0,0,0);m_lBrush.lbHatch=HS_BDIAGONAL;m_lBrush.lbStyle=BS_HATCHED;m_lPen.lopnColor=RGB(0,0,0);m_lPen.lopnStyle=PS_SOLID;m_lPen.lopnWidth.x=1;m_cFont=RGB(0,0,0);m_lFont.lfHeight=30;m_lFont.lfWidth=0;m_lFont.lfEscapement=0;m_lFont.lfOrientation=0;m_lFont.lfWeight=FW_NORMAL;m_lFont.lfItalic=FALSE;m_lFont.lfUnderline=FALSE;m_lFont.lfStrikeOut=FALSE;m_lFont.lfCharSet=GB2312_CHARSET;m_lFont.lfOutPrecision=OUT_STROKE_PRECIS;m_lFont.lfClipPrecision=CLIP_STROKE_PRECIS;m_lFont.lfQuality=DRAFT_QUALITY;m_lFont.lfPitchAndFamily=VARIABLE_PITCH|FF_MODERN;strcpy(m_lFont.lfFaceName,楷体_GB2312);//无模式对话框的指针初始化pDlg=NULL;}//析构函数的内存释放CGDI_ExeView::~CGDI_ExeView(){m_mapMode.RemoveAll();m_mapPen.RemoveAll();m_mapBrush.RemoveAll();m_mapHBrush.RemoveAll();}二、DOC类中的图形数据项及数据管理函数的添加。数据项添加(*Doc.h)private://存储图形数据的数据结构CArrayCRect,CRectm_lineObj;CArrayCRect,CRectm_rectObj;CArrayCRect,CRectm_ellipseObj;CTypedPtrListCObList,CDrawPolygon*m_polygonObj;数据操作功能://向数据集中追加新的数据的函数voidAddLine(CRectrect);voidAddRect(CRectrect);voidAddEllipse(CRectrect);voidAddPolygon(CDrawPolygon*pPoly);//在数据集中删除相应目标BOOLDeleteLine(CRectrect);BOOLDeleteRect(CRectrect);BOOLDeleteEllipse(CRectrect);BOOLDeletePolygon(intnPolyID);//调整多边形位置于链表最后voidPolygonToTail(intnPolyID);//用于图形选择的函数,主要用了CRgn类的RectInRegion()方法BOOLGetLineAt(CRect&rect,CPointpoint,LOGPENlogpen);BOOLGetRectAt(CRect&rect,CPointpoint);BOOLGetEllipseAt(CRect&rect,CPointpoint);BOOLGetPolygonAt(CDrawPolygon&poly,CPointpoint);//画图形的函数voidDrawLines(CGDI_ExeView*pView,CDC*pDC);voidDrawRects(CGDI_ExeView*pView,CDC*pDC);voidDrawEllipses(CGDI_ExeView*pView,CDC*pDC);voidDrawPolygons(CGDI_ExeView*pView,CDC*pDC);//移动选中对象voidMoveObj(CRectrect,intnPolyID,DrawShapetype,CPointdxy);多边形的数据结构及相关操作函数请参考例程相关代码,争取看懂代码。三、建立右键菜单资源并在View类中添加对右键菜单的消息处理。voidCGDI_ExeView::OnContextMenu(CWnd*pWnd,CPointpoint){//TODO:AddyourmessagehandlercodehereCMenumenu;if(menu.LoadMenu(IDR_MENU_POP)){CMenu*pPopup=menu.GetSubMenu(0);if(pPopup!=NULL)pPopup-TrackPopupMenu(TPM_RIGHTBUTTON|TPM_LEFTALIGN,point.x,point.y,pWnd);}}voidCGDI_ExeView::OnDelete(){//TODO:AddyourcommandhandlercodehereCGDI_ExeDoc*pDoc=GetDocument();ASSERT_VALID(pDoc);CRectrdoc=m_rectSelect;ClientToDoc(rdoc);rdoc.NormalizeRect();intidpoly=m_polygonSelect.m_nID;if(m_bSelected){switch(m_selectType){caserectangle:if(pDoc-DeleteRect(rdoc))m_bSelected=FALSE;Invalidate();break;caseellipse:if(pDoc-DeleteEllipse(rdoc))m_bSelected=FALSE;Invalidate();break;caseline:if(pDoc-DeleteLine(rdoc))m
本文标题:windows程序设计实验指导书
链接地址:https://www.777doc.com/doc-2856446 .html