1、windows程序设计实验指导书3GDI编程正确解析实验三、Windows GDI编程实验目的:1、 了解windows GDI设备环境的获取与设置。2、 了解windows GDI 绘图对象CPen,CBrush,CFont等的相关基本操作。3、 掌握建立简单图形交互的图形数据管理和鼠标工具设计等。实验要求:建立一个windows SDI(单文档)应用程序,并编辑建立图形属性设置的无模式对话框、简单图形工具条等。实现直线、矩形、椭圆、多边形的交互绘制及文字显示,并能进行图形属性设置。实验可以选择竞赛作业题目,则本次的基本操作实验可以不做,竞赛作业可在课程后期交上,计算成绩有加分因素。竞赛作业
2、:分析微软Office系列软件的绘图工具箱组件的图形编辑功能和操作特点,抽象出其数据组织的结构体系,并分析其需要的鼠标工具类型,设计出相应合理的数据结构和鼠标工具,模拟实现简单几何图形的绘制工具(能交互设置图形属性、有图形成组操作功能,可以实现图形的简单几何变换为佳)。实验指导:一、 在VC+6.0环境中建立一个单文档工程。1、 通过资源编辑器编辑如下所示对话框资源和工具条资源。注意生成对话框类后,为几个组合框添加字符串变量,为Spin控件添加控制变量,并为几个组合对话框添加初始数据:映射模式:MM_ANISOTROPICMM_HIENGLISHMM_HIMETRICMM_ISOTROPICM
3、M_LOENGLISHMM_LOMETRICMM_TEXTMM_TWIPS画笔风格:PS_SOLIDPS_DASHPS_DOTPS_DASHDOTPS_DASHDOTDOTPS_NULLPS_INSIDEFRAME画刷风格:BS_HATCHEDBS_SOLID线刷风格:HS_BDIAGONALHS_CROSSHS_DIAGCROSSHS_FDIAGONALHS_HORIZONTALHS_VERTICAL为对话框类添加构造函数和变量:C*Dlg(C*View* pView);G*View* m_pView;C*Dlg:C*Dlg(C*View* pView) m_pView = (C*View*
4、 ) pView;/重载消息void C*Dlg:PostNcDestroy() / TODO: Add your specialized code here and/or call the base class /用于在无模式对话框关闭时自动释放堆空间 delete this; CDialog:PostNcDestroy(); /通过“颜色”通用对话框设置画笔颜色void C*Dlg:OnButtonPencolor() / TODO: Add your control notification handler code here CColorDialog dlgColor; if (dlg
5、Color.DoModal() = IDOK) m_pView-m_lPen.lopnColor = dlgColor.GetColor(); m_pView-Invalidate(); /通过“颜色”通用对话框设置画刷颜色void C*Dlg:OnButtonBrucol() / TODO: Add your control notification handler code here CColorDialog dlgColor; if (dlgColor.DoModal() = IDOK) m_pView-m_lBrush.lbColor = dlgColor.GetColor(); m_
6、pView-Invalidate(); /通过“字体”通用对话框设置字体属性void C*Dlg:OnButtonFont() / TODO: Add your control notification handler code here /声明一个“字体”通用对话框对象 CFontDialog dlg; /定义“字体”通用对话框的初值 dlg.m_cf.lpLogFont = &(m_pView-m_lFont); dlg.m_cf.rgbColors = m_pView-m_cFont; dlg.m_cf.Flags |= CF_INITTOLOGFONTSTRUCT; /打开“字体”通用
7、对话框,若单击了“确定”按钮 if (dlg.DoModal() = IDOK) /获取所选择的字体属性 dlg.GetCurrentFont(&(m_pView-m_lFont); /获取所选择的字体颜色 m_pView-m_cFont = dlg.GetColor(); /重画窗口 m_pView-Invalidate(); /重载消息BOOL C*Dlg:OnInitDialog() CDialog:OnInitDialog(); / TODO: Add extra initialization here /控件必要的初始化 m_ctrlSpin.SetRange(1,1000); m_
8、ctrlSpin.SetBase(10); m_ctrlSpin.SetPos(1); m_ctrlPS.SetCurSel(0); m_ctrlMM.SetCurSel(6); m_ctrlHBS.SetCurSel(0); m_ctrlBS.SetCurSel(0); return TRUE; / return TRUE unless you set the focus to a control / EXCEPTION: OCX Property Pages should return FALSEvoid C*Dlg:OnApply() / TODO: Add your control n
9、otification handler code here UpdateData(); /调用视图类的方法将图形属性设置变量传过去 m_pView-OnApply();void C*Dlg:OnOK() / TODO: Add extra validation here UpdateData(); /也调用视图类的方法将图形属性设置变量传过去 m_pView-OnApply(); CDialog:OnOK();2、 在View类中添加数据成员:int m_nMapMode;/映射模式LOGBRUSH m_lBrush;/用来交互设置CDC画刷属性的逻辑画刷LOGPEN m_lPen;/用来交互
10、设置CDC画笔属性的逻辑画笔COLORREF m_cFont;/用来交互设置CDC字体颜色LOGFONT m_lFont;/用来交互设置CDC字体属性的逻辑字体/将字符串变量与整形变量之间建立映射关系,用于对话框的数据交互CMap m_mapMode;CMap m_mapPen;CMap m_mapBrush;CMap m_mapHBrush;3、 在View类中添加成员函数和实现:头文件(.h)中所加内容: /无模式对话框 C*Dlg* pDlg;/图形属性对话框Apply、OK按钮的消息处理 void OnApply();源文件(.cpp)中所加内容:构造函数中添加内容:*View:*Vi
11、ew() / TODO: add construction code here /初始化映射模式的值-字符串映射表 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_L
12、OMETRIC,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_mapPe
13、n.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
14、_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
15、= 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 =
16、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
17、_MODERN; strcpy(m_lFont.lfFaceName,楷体); /无模式对话框的指针初始化 pDlg = NULL; C*View:*View() m_mapMode.RemoveAll(); m_mapPen.RemoveAll(); m_mapBrush.RemoveAll(); m_mapHBrush.RemoveAll();OnDraW()函数中添加显示文字代码:void *View:OnDraw(CDC* pDC) CExm_Menu_ToolBarDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); / TODO: add d
18、raw code for native data here /根据逻辑画笔、画刷设置设备环境画笔、画刷的属性 CBrush brush; if (!brush.CreateBrushIndirect(&m_lBrush) return; CPen pen; if (!pen.CreatePenIndirect(&m_lPen) return; CBrush* pOldBrush = pDC-SelectObject(&brush); CPen* pOldPen = pDC-SelectObject(&pen); /画矩形 pDoc-DrawRects(this,pDC); /画椭圆 pDoc-
19、DrawEllipses(this,pDC); /画多边形 pDoc-DrawPolygons(this,pDC); /画直线 pDoc-DrawLines(this,pDC); /还原设备环境 pDC-SelectObject(pOldBrush); pDC-SelectObject(pOldPen); /根据“字体”通用对话框中的选项绘制文本 CFont myFont; myFont.CreateFontIndirect(&m_lFont); CFont* pOldFont = pDC-SelectObject(&myFont); pDC-SetTextColor(m_cFont); /根
20、据逻辑字体显示文字 pDC-TextOut(80,80,字体实例,显示选择字体); /由CreateFont函数直接创建一种显示字体 CFont myFont1; myFont1.CreateFont(30, /字符的逻辑高度 0, /字符的平均宽度,缺省值 -450, /文本行顺时针旋转45度 0, FW_BOLD, /加粗体 TRUE, /不倾斜 TRUE, /加下划线 FALSE, /不加删除线 GB2312_CHARSET, /中文字符集 OUT_OUTLINE_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF
21、_ROMAN, 仿宋); /宋体 pOldFont = pDC-SelectObject(&myFont1); /将字体颜色设置为兰色 pDC-SetTextColor(RGB(0,0,255); /根据创建字体显示文字 pDC-TextOut(200,130,自定义仿宋字体); /还原设备环境 pDC-SelectObject(pOldFont);void C*View:On*() / TODO: Add your command handler code here /无模式对话框的建立 if(pDlg = NULL) pDlg = new C*Dlg(this); pDlg-Create(
22、IDD_DIALOG1); pDlg-ShowWindow(SW_SHOW);/由无模式对话框设置设备环境属性void C*View:OnApply() m_lPen.lopnWidth.x = pDlg-m_nWeight; POSITION pos; pos = m_mapMode.GetStartPosition(); while (pos != NULL) CString szMapMode; int nMapMode; m_mapMode.GetNextAssoc(pos,nMapMode,szMapMode); if (szMapMode = pDlg-m_strMode) if(
23、m_nMapMode != nMapMode) m_nMapMode = nMapMode; m_bSelected = FALSE; break; pos = m_mapPen.GetStartPosition(); while (pos != NULL) CString szPenStyle; int nPenStyle; m_mapPen.GetNextAssoc(pos,nPenStyle,szPenStyle); if (szPenStyle = pDlg-m_strPStyle) m_lPen.lopnStyle= nPenStyle; break; pos = m_mapBrus
24、h.GetStartPosition(); while (pos != NULL) CString szBrushStyle; int nBrushStyle; m_mapBrush.GetNextAssoc(pos,nBrushStyle,szBrushStyle); if (szBrushStyle = pDlg-m_strBStyle) m_lBrush.lbStyle= nBrushStyle; break; pos = m_mapHBrush.GetStartPosition(); while (pos != NULL) CString szHBrushStyle; int nHBr
25、ushStyle; m_mapHBrush.GetNextAssoc(pos,nHBrushStyle,szHBrushStyle); if (szHBrushStyle = pDlg-m_strHBrush) m_lBrush.lbHatch= nHBrushStyle; break; Invalidate();4、 在文挡类中添加相关绘图对象数据成员和方法:头文件中:/存储图形数据的数据结构 CArray m_lineObj; CArray m_rectObj; CArray m_ellipseObj; CTypedPtrList m_polygonObj;/向数据集中追加新的数据的函数
26、void AddLine(CRect rect); void AddRect(CRect rect); void AddEllipse(CRect rect); void AddPolygon(CDrawPolygon* pPoly); /画图形的函数 void DrawLines(C*View* pView,CDC* pDC); void DrawRects(C*View* pView,CDC* pDC); void DrawEllipses(C*View* pView,CDC* pDC); void DrawPolygons(C*View* pView,CDC* pDC);源文件中:C*D
27、oc:C*Doc() /释放内存 m_lineObj.RemoveAll(); m_rectObj.RemoveAll(); m_ellipseObj.RemoveAll(); while(!m_polygonObj.IsEmpty() delete m_polygonObj.GetHead(); m_polygonObj.RemoveHead(); void C*Doc:AddLine(CRect rect) m_lineObj.Add(rect); UpdateAllViews(NULL);void C*Doc:AddRect(CRect rect) m_rectObj.Add(rect)
28、; UpdateAllViews(NULL);void C*Doc:AddEllipse(CRect rect) m_ellipseObj.Add(rect); UpdateAllViews(NULL);void C*Doc:AddPolygon(CDrawPolygon* pPoly) pPoly-m_nID = m_polygonObj.GetCount()+1; m_polygonObj.AddTail(pPoly); UpdateAllViews(NULL);void C*Doc:DrawLines(C*View* pView,CDC* pDC) CString str; for(int i = 0; i DocToClient(rect); pDC-MoveTo(rect.TopLeft(); pDC-LineTo(rect.BottomRight(); str.Format(%d,i+1); pDC-TextOut(rect.left+rect.Width()/2+
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1