VC-6.0-下MFC基于对话框-绘制图形_第1页
VC-6.0-下MFC基于对话框-绘制图形_第2页
VC-6.0-下MFC基于对话框-绘制图形_第3页
VC-6.0-下MFC基于对话框-绘制图形_第4页
VC-6.0-下MFC基于对话框-绘制图形_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、附录表一、具体步骤:1、选择菜单栏的“新建”,选择MFC项目名为DrawGraphics,并设置为基于对话框的项目,如图-1、图-2所示;图-12、按下键盘上的Ctrl+W键,调出类向导,选择"AddClass”,并点击"New”添加一个颜色按钮类CColorButton,继承自CButton类,如图-3所示:图-33、在CColorButton中添加如下成员变量:COLORREFcolor;按钮颜色CPointarrays4;按钮顶点坐标BOOLIsShow;/是否显示按钮BOOLIsPressed;按钮是否被按下4、右击CColorButton类,选择AddVirtul

2、function,重写其DraItem函数,如图-4,图-5所示:亡工三日和S虫空百加,刈_L忸二包或叵_三山定的CDwMGr叫卜liriDIgjJ|(*llmtnihtrl*'.上CAJjQuWIq三pyWGGotoDe4inj5cnAddMeedsFuFUium.AddMrrTfctwrjariabe-AddVirtuaFunctsoriL.AddEndowsHandlcTf-AddioGdlfiy知甑钗WIDCIOhMciUrMirif(IInvalidCDialog:IdCDrduCr图-4NwVirtualOverridefnrCColorButtonNewVirtualFu

3、nctionsExistingvirtuallunctionoverridesCalcWindowRectCrealeDcfWiiidowPrucRF?strnyWinf1nwDoDataExchngeGe1Scn)IIBarCtrlOnAmbicnlPtupErtyOnChildNotifyOnCndMsgOnCommandOnFlnBlHeleasEOuNnlifyPostNcDestroyPreCrealeWindowPitSubclaiiiiV/iuduwPieTranlateMessageSerializeDrawl配m确定取情AdriHnnrlkrAddandEdilEditExi

4、stingCelcWindowREct):Computesthewindow5izebasedontheclientsize图-55、为其Drawitem函数中添加如下代码:CRectrect;GetClientRect(rect);CDCdc;dc.Attach(lpDrawItemStruct->hDC);arrays0=CPoint(rect.left,rect.top);arrays1=CPoint(rect.right,rect.top);arrays2=CPoint(rect.right,rect.bottom);arrays3=CPoint(rect.left,rect.b

5、ottom);设置背景透明dc.SetBkMode(TRANSPARENT);if(IsShow)创建一个位图画刷CBrushbrush(color);dc.SelectObject(&brush);CPenpen(PS_NULL,1,color);dc.SelectObject(&pen);dc.Rectangle(rect);if(IsPressed)CPenpen(PS_DASHDOTDOT,3,RGB(0,0,0);dc.SelectObject(&pen);dc.MoveTo(arrays0);for(inti=1;i<4;i+)dc.LineTo(ar

6、raysi);dc.LineTo(arrays0);elseCPenpen(PS_DASHDOTDOT,2,color);dc.SelectObject(&pen);dc.MoveTo(arrays0);for(inti=1;i<4;i+)dc.LineTo(arraysi);dc.LineTo(arrays0);/绘制按钮文本CStringstr;GetWindowText(str);dc.SetTextColor(RGB(255-GetRValue(color),255-GetGValue(color),255-GetBValue(color);dc.DrawText(str

7、,CRect(0,0,rect.right,rect.bottom),DT_CENTER|DT_VCENTER|DT_SINGLELINE);6、为CColorButton添加属性设置函数:voidCColorButton:SetDrawColor(COLORREFm_color,BOOLIsShow)this->color=m_color;this->IsShow=IsShow;7、为CColorButton添加WM_LBUTTONDOWN和WM_LBUTTONUP消息,其消息处理函数如下:voidCColorButton:OnLButtonDown(UINTnFlags,CPo

8、intpoint)/TODO:Addyourmessagehandlercodehereand/orcalldefaultIsPressed=true;CButton:OnLButtonDown(nFlags,point);voidCColorButton:OnLButtonUp(UINTnFlags,CPointpoint)/TODO:Addyourmessagehandlercodehereand/orcalldefaultIsPressed=false;CButton:OnLButtonUp(nFlags,point);8、为DrawGraphics项目的中的对话框资源拖拽如下控件,如图

9、-6所示,并按表-1设置其相关ID;图-6IDIDC_RADIO_RECTIDC_RADIO_ROUNDIDC_RADIO_LINEIDC_RADIO_SELFIDC_RADIO_SLOIDLINEIDC_RADIO_DOTTEDLINEIDCCOMBOLINEVALUE作用及类别矩形单选按钮圆形单选按钮直线单选按钮涂鸦单选按钮实线单选按钮虚线单选按钮线条粗细下拉列表IDC_STATIC_LINEEXAMPLE显示线条粗细的 Picture 控件IDC_CHECK_BORDER IDC_RADIO_SOLID IDC_RADIO_GRADIENTIDC_RADIO_NULLIDC_BUTTON

10、_FORECOLORIDC_BUTTON_BKCOLORIDC_RADIO_PENIDC_RADIO_CLEARIDC_STATIC_GRAPHICS是否有边界复选框填充单选按钮渐变单选按钮无填充单选按钮前景色按钮背景色按钮画笔单选按钮清空画板单选按钮画板区(Picture控件)表-19、打开类向导,按图-7所示,为相关控件添加关联成员:10、在CDrawGraphicsDlg类的OninitDialog函数中添加初始化代码:BOOLCDrawGraphicsDlg:OnInitDialog()/*省略无关代码*/TODO:AddextrainitializationhereCButton*c

11、b;设置相关按钮的默认选中状态cb=(CButton*)GetDlgItem(IDC_RADIO_RECT);cb->SetCheck;cb=(CButton*)GetDlgItem(IDC_RADIO_SLOIDLINE);cb->SetCheck;cb=(CButton*)GetDlgItem(IDC_CHECK_BORDER);cb->SetCheck(1);cb=(CButton*)GetDlgItem(IDC_RADIO_SOLID);cb->SetCheck(1);cb=(CButton*)GetDlgItem(IDC_RADIO_PEN);cb->S

12、etCheck(1);m_ComboLineValue.SetCurSel(3);m_ForeColor=RGB(255,0,0);m_BkColor=RGB(0,0,255);m_ForeCButton.SetDrawColor(m_ForeColor,TRUE);/设置前背景色按钮的颜色m_BkCButton.SetDrawColor(m_BkColor,TRUE);m_IsPressed=FALSE;m_nWidth=4;m_nPenStyle=PS_SOLID;returnTRUE;/returnTRUEunlessyousetthefocustoacontrol10、为CDrawGr

13、aphicsDlg添加WM_LBUTTONDOWN和WM_LBUTTONUP,WM_MOUSEMOVE消息,其消息处理函数如下:voidCDrawGraphicsDlg:OnLButtonDown(UINTnFlags,CPointpoint)/TODO:Addyourmessagehandlercodehereand/orcalldefaultm_IsPressed=TRUE;SetCursor(m_Hcursor);if(IsDlgButtonChecked(IDC_RADIO_SELF)m_pOld=point;elseif(IsDlgButtonChecked(IDC_RADIO_LI

14、NE)m_pOld=point;m_PointOrigin=point;elseif(IsDlgButtonChecked(IDC_RADIO_RECT)m_pOld=point;m_PointOrigin=point;elseif(IsDlgButtonChecked(IDC_RADIO_ROUND)m_pOld=point;m_PointOrigin=point;CDialog:OnLButtonDown(nFlags,point);voidCDrawGraphicsDlg:OnLButtonUp(UINTnFlags,CPointpoint)/TODO:Addyourmessagehan

15、dlercodehereand/orcalldefaultm_IsPressed=FALSE;ClipCursor(NULL);/解除鼠标的限定CClientDCdc(this);CPenpen(m_nPenStyle,m_nWidth,m_ForeColor);dc.SelectObject(&pen);if(IsDlgButtonChecked(IDC_RADIO_LINE)/选择的为画直线dc.SetROP2(R2_NOT);/逆转当前屏幕颜色来画线的绘图方式dc.MoveTo(m_PointOrigin);dc.LineTo(m_pOld);/擦去上一次的临时线dc.SetRO

16、P2(R2_COPYPEN);/缺省绘图模式,像素为画笔颜色dc.MoveTo(m_PointOrigin);dc.LineTo(point);/绘制固定线elseif(IsDlgButtonChecked(IDC_RADIO_RECT)/选择的为画矩形dc.SelectObject(GetStockObject(NULL_BRUSH);/选择空话刷dc.SetROP2(R2_NOT);CRectOldrect(m_PointOrigin,m_pOld);dc.Rectangle(&Oldrect);/清除之前的矩形绘图dc.SetROP2(R2_COPYPEN);CBrushbrus

17、h(m_BkColor);dc.SelectObject(&brush);/画这次的矩形if(IsDlgButtonChecked(IDC_RADIO_NULL)/如果选择无填充的话,使用空画刷dc.SelectObject(GetStockObject(NULL_BRUSH);CRectNowrect(m_PointOrigin,point);dc.Rectangle(&Nowrect);if(IsDlgButtonChecked(IDC_RADIO_GRADIENT)/如果选择渐变的话,调用渐变函数DrawGradient(dc.GetSafeHdc(),Nowrect,m

18、_ForeColor,m_BkColor,1);elseif(IsDlgButtonChecked(IDC_RADIO_ROUND)/如果选择绘制椭圆dc.SelectObject(GetStockObject(NULL_BRUSH);dc.SetROP2(R2_NOT);CRectOldrect(m_PointOrigin,m_pOld);dc.Ellipse(&Oldrect);dc.SetROP2(R2_COPYPEN);CBrushbrush(m_BkColor);dc.SelectObject(&brush);if(IsDlgButtonChecked(IDC_RAD

19、IO_NULL)dc.SelectObject(GetStockObject(NULL_BRUSH);CRectNowrect(m_PointOrigin,point);if(IsDlgButtonChecked(IDC_RADIO_GRADIENT)/如果选择渐变的话,调用渐变函数DrawGradient(dc.GetSafeHdc(),Nowrect,m_ForeColor,m_BkColor,1);dc.Ellipse(&Nowrect);InvalidateMyRect();/刷新工具区CDialog:OnLButtonUp(nFlags,point);voidCDrawGra

20、phicsDlg:OnMouseMove(UINTnFlags,CPointpoint)/TODO:Addyourmessagehandlercodehereand/orcalldefaultif(m_IsPressed&&point.x>205)/如果鼠标移动位置到达画板区CRectrect;m_Graphics.GetClientRect(&rect);m_Graphics.ClientToScreen(&rect);ClipCursor(rect);/限定光标在指定矩形CClientDCdc(this);if(m_IsPressed&&

21、;IsDlgButtonChecked(IDC_RADIO_SELF)/如果选择了涂鸦CPenpen(m_nPenStyle,m_nWidth,m_ForeColor);/创建画笔dc.SelectObject(&pen);/选入画笔dc.MoveTo(m_pOld);dc.LineTo(point);/绘制轨迹m_pOld=point;/将当前点设置为旧点elseif(m_IsPressed&&IsDlgButtonChecked(IDC_RADIO_LINE)/如果选择了直线dc.SetROP2(R2_NOT);/逆转当前屏幕颜色来画线的绘图方式dc.MoveTo(

22、m_PointOrigin);dc.LineTo(m_pOld);/擦去上一次的线dc.MoveTo(m_PointOrigin);dc.LineTo(point);/绘制这一次的临时线m_pOld=point;elseif(m_IsPressed&&IsDlgButtonChecked(IDC_RADIO_RECT)/如果选择了矩形dc.SetROP2(R2_NOT);CPenpen(PS_DOT,1,m_ForeColor);dc.SelectObject(&pen);dc.SelectObject(GetStockObject(HOLLOW_BRUSH);CRec

23、tOldrect(m_PointOrigin,m_pOld);dc.Rectangle(&Oldrect);CRectNowrect(m_PointOrigin,point);dc.Rectangle(&Nowrect);m_pOld=point;pen.DeleteObject();elseif(m_IsPressed&&IsDlgButtonChecked(IDC_RADIO_ROUND)/如果选择了圆CClientDCdc(this);dc.SetROP2(R2_NOT);CPenpen(PS_DOT,1,m_ForeColor);dc.SelectObj

24、ect(&pen);dc.SelectObject(GetStockObject(HOLLOW_BRUSH);CRectOldrect(m_PointOrigin,m_pOld);dc.Ellipse(&Oldrect);CRectNowrect(m_PointOrigin,point);dc.Ellipse(&Nowrect);m_pOld=point;pen.DeleteObject();CDialog:OnMouseMove(nFlags,point);11、为背景色、前景色、无填充、实线、虚线、边框、清空画板按钮添加按钮单击消息,响应函数如下:voidCDraw

25、GraphicsDlg:OnButtonBkcolor()/TODO:AddyourcontrolnotificationhandlercodehereCColorDialogcdg;if(cdg.DoModal()=IDOK)/调用颜色对话框m_BkColor=cdg.GetColor();/更改背景色m_BkCButton.SetDrawColor(m_BkColor,TRUE);/更改背景色按钮颜色InvalidateMyRect();/刷新工具区voidCDrawGraphicsDlg:OnButtonForecolor()/TODO:Addyourcontrolnotificatio

26、nhandlercodehereCColorDialogcdg;if(cdg.DoModal()=IDOK)m_ForeColor=cdg.GetColor();m_ForeCButton.SetDrawColor(m_ForeColor,TRUE);InvalidateMyRect();voidCDrawGraphicsDlg:OnRadioNull()/TODO:Addyourcontrolnotificationhandlercodehereif(!IsDlgButtonChecked(IDC_CHECK_BORDER)/无填充时不能也无边框CButton*cb=(CButton*)Ge

27、tDlgItem(IDC_CHECK_BORDER);cb->SetCheck(1);voidCDrawGraphicsDlg:OnRadioSloidline()/TODO:AddyourcontrolnotificationhandlercodehereCStringstr9="1px","2px","3px","4px","5px","6px","8px","10px","12px"m_ComboLine

28、Value.ResetContent();for(inti=0;i<9;i+)m_ComboLineValue.AddString(stri);m_ComboLineValue.SetCurSel(3);m_nWidth=4;m_nPenStyle=PS_SOLID;InvalidateMyRect();voidCDrawGraphicsDlg:OnRadioDottedline()/TODO:Addyourcontrolnotificationhandlercodeherefor(inti=11;i>0;i-)m_ComboLineValue.DeleteString(i);m_

29、ComboLineValue.SetCurSel(0);m_nWidth=1;m_nPenStyle=PS_DOT;InvalidateMyRect();voidCDrawGraphicsDlg:OnCheckBorder()/TODO:Addyourcontrolnotificationhandlercodehereif(IsDlgButtonChecked(IDC_CHECK_BORDER)m_nPenStyle=PS_SOLID;elsem_nPenStyle=PS_NULL;if(IsDlgButtonChecked(IDC_RADIO_NULL)/既不能无边框也不能无填充CButto

30、n*cb=(CButton*)GetDlgItem(IDC_CHECK_BORDER);cb->SetCheck(1);voidCDrawGraphicsDlg:OnRadioClear()/TODO:AddyourcontrolnotificationhandlercodehereInvalidate(TRUE);12、为选择画笔宽度的下拉列表添加CBN_SELCHAGE消息,消息处理函数如下:voidCDrawGraphicsDlg:OnRadioSloidline()/TODO:AddyourcontrolnotificationhandlercodehereCStringstr9

31、="1px","2px","3px","4px","5px","6px","8px","10px","12px"m_ComboLineValue.ResetContent();for(inti=0;i<9;i+)m_ComboLineValue.AddString(stri);m_ComboLineValue.SetCurSel(3);m_nWidth=4;m_nPenStyle=PS_SOLID;InvalidateMyRect();13、为CDrawGraphicsDlg类添加如下的两个自定义函数InvalidateMyRect以及DrawGradient,分别用来刷新工具区和绘制渐变:voidCDrawGraphicsDlg:InvalidateMyRect()/将工具区部分刷新CRectinva

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论