版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、附錄表一、具體步驟:1、選擇菜單欄的“新建”,選擇MFC項(xiàng)目名為DrawGraphics,并設(shè)置為基于對(duì)話框的項(xiàng)目,如圖-1、圖-2所示;圖-12、按下鍵盤上的Ctrl+W鍵,調(diào)出類向?qū)Вx擇"AddClass”,并點(diǎn)擊"New”添加一個(gè)顏色按鈕類CColorButton,繼承自CButton類,如圖-3所示:圖-33、在CColorButton中添加如下成員變量:COLORREFcolor;按鈕顏色CPointarrays4;按鈕頂點(diǎn)坐標(biāo)BOOLIsShow;/是否顯示按鈕BOOLIsPressed;按鈕是否被按下4、右擊CColorButton類,選擇AddVirtul
2、function,重寫其DraItem函數(shù),如圖-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函數(shù)中添加如下代碼: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);設(shè)置背景透明dc.SetBkMode(TRANSPARENT);if(IsShow)創(chuàng)建一個(gè)位圖畫刷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添加屬性設(shè)置函數(shù):voidCColorButton:SetDrawColor(COLORREFm_color,BOOLIsShow)this->color=m_color;this->IsShow=IsShow;7、為CColorButton添加WM_LBUTTONDOWN和WM_LBUTTONUP消息,其消息處理函數(shù)如下: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項(xiàng)目的中的對(duì)話框資源拖拽如下控件,如圖
9、-6所示,并按表-1設(shè)置其相關(guān)ID;圖-6IDIDC_RADIO_RECTIDC_RADIO_ROUNDIDC_RADIO_LINEIDC_RADIO_SELFIDC_RADIO_SLOIDLINEIDC_RADIO_DOTTEDLINEIDCCOMBOLINEVALUE作用及類別矩形單選按鈕圓形單選按鈕直線單選按鈕涂鴉單選按鈕實(shí)線單選按鈕虛線單選按鈕線條粗細(xì)下拉列表IDC_STATIC_LINEEXAMPLE顯示線條粗細(xì)的 Picture 控件IDC_CHECK_BORDER IDC_RADIO_SOLID IDC_RADIO_GRADIENTIDC_RADIO_NULLIDC_BUTTON
10、_FORECOLORIDC_BUTTON_BKCOLORIDC_RADIO_PENIDC_RADIO_CLEARIDC_STATIC_GRAPHICS是否有邊界復(fù)選框填充單選按鈕漸變單選按鈕無填充單選按鈕前景色按鈕背景色按鈕畫筆單選按鈕清空畫板單選按鈕畫板區(qū)(Picture控件)表-19、打開類向?qū)?,按圖-7所示,為相關(guān)控件添加關(guān)聯(lián)成員:10、在CDrawGraphicsDlg類的OninitDialog函數(shù)中添加初始化代碼:BOOLCDrawGraphicsDlg:OnInitDialog()/*省略無關(guān)代碼*/TODO:AddextrainitializationhereCButton*c
11、b;設(shè)置相關(guān)按鈕的默認(rèn)選中狀態(tài)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);/設(shè)置前背景色按鈕的顏色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消息,其消息處理函數(shù)如下: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);/解除鼠標(biāo)的限定CClientDCdc(this);CPenpen(m_nPenStyle,m_nWidth,m_ForeColor);dc.SelectObject(&pen);if(IsDlgButtonChecked(IDC_RADIO_LINE)/選擇的為畫直線dc.SetROP2(R2_NOT);/逆轉(zhuǎn)當(dāng)前屏幕顏色來畫線的繪圖方式dc.MoveTo(m_PointOrigin);dc.LineTo(m_pOld);/擦去上一次的臨時(shí)線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)/如果選擇漸變的話,調(diào)用漸變函數(shù)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)/如果選擇漸變的話,調(diào)用漸變函數(shù)DrawGradient(dc.GetSafeHdc(),Nowrect,m_ForeColor,m_BkColor,1);dc.Ellipse(&Nowrect);InvalidateMyRect();/刷新工具區(qū)CDialog:OnLButtonUp(nFlags,point);voidCDrawGra
20、phicsDlg:OnMouseMove(UINTnFlags,CPointpoint)/TODO:Addyourmessagehandlercodehereand/orcalldefaultif(m_IsPressed&&point.x>205)/如果鼠標(biāo)移動(dòng)位置到達(dá)畫板區(qū)CRectrect;m_Graphics.GetClientRect(&rect);m_Graphics.ClientToScreen(&rect);ClipCursor(rect);/限定光標(biāo)在指定矩形CClientDCdc(this);if(m_IsPressed&&
21、;IsDlgButtonChecked(IDC_RADIO_SELF)/如果選擇了涂鴉CPenpen(m_nPenStyle,m_nWidth,m_ForeColor);/創(chuàng)建畫筆dc.SelectObject(&pen);/選入畫筆dc.MoveTo(m_pOld);dc.LineTo(point);/繪制軌跡m_pOld=point;/將當(dāng)前點(diǎn)設(shè)置為舊點(diǎn)elseif(m_IsPressed&&IsDlgButtonChecked(IDC_RADIO_LINE)/如果選擇了直線dc.SetROP2(R2_NOT);/逆轉(zhuǎn)當(dāng)前屏幕顏色來畫線的繪圖方式dc.MoveTo(
22、m_PointOrigin);dc.LineTo(m_pOld);/擦去上一次的線dc.MoveTo(m_PointOrigin);dc.LineTo(point);/繪制這一次的臨時(shí)線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、為背景色、前景色、無填充、實(shí)線、虛線、邊框、清空畫板按鈕添加按鈕單擊消息,響應(yīng)函數(shù)如下:voidCDraw
25、GraphicsDlg:OnButtonBkcolor()/TODO:AddyourcontrolnotificationhandlercodehereCColorDialogcdg;if(cdg.DoModal()=IDOK)/調(diào)用顏色對(duì)話框m_BkColor=cdg.GetColor();/更改背景色m_BkCButton.SetDrawColor(m_BkColor,TRUE);/更改背景色按鈕顏色I(xiàn)nvalidateMyRect();/刷新工具區(qū)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)/無填充時(shí)不能也無邊框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消息,消息處理函數(shù)如下: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類添加如下的兩個(gè)自定義函數(shù)InvalidateMyRect以及DrawGradient,分別用來刷新工具區(qū)和繪制漸變:voidCDrawGraphicsDlg:InvalidateMyRect()/將工具區(qū)部分刷新CRectinva
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 老年慢病綜合征的評(píng)估
- 2024農(nóng)民承包土地合同書
- 專題 07課內(nèi)閱讀(所有課內(nèi)片段閱讀) -2022-2023學(xué)年五年級(jí)語文下冊(cè)期末專項(xiàng)復(fù)習(xí)(部編版)
- 2024簡(jiǎn)易維修合同格式
- 2024常用房屋裝修合同范本
- 2024建筑工程專業(yè)分包合同
- 2024建筑工程承包施工合同書格式
- 2024工地運(yùn)輸合同參考范文
- 計(jì)算機(jī)編程培訓(xùn)課程
- 關(guān)于實(shí)習(xí)生實(shí)習(xí)報(bào)告模板集錦7篇
- 幼兒園小班科學(xué)活動(dòng)《小手摸一摸》課件
- 文言文閱讀訓(xùn)練:《通鑒紀(jì)事本末-劉邦起兵》(附答案解析與譯文)
- 食品輻照行業(yè)發(fā)展現(xiàn)狀及潛力分析研究報(bào)告
- 離婚協(xié)議中子女撫養(yǎng)權(quán)的確定
- 全媒體運(yùn)營師職業(yè)技能競(jìng)賽題庫及答案(1-250判斷題)
- 2024場(chǎng)地轉(zhuǎn)租協(xié)議
- 家長(zhǎng)會(huì)課件:小學(xué)數(shù)學(xué)家長(zhǎng)會(huì)課件
- 傳染病防治規(guī)劃實(shí)施細(xì)則
- 第五單元中國特色社會(huì)主義社會(huì)建設(shè)單元測(cè)試-2023-2024學(xué)年中職高教版(2023)中國特色社會(huì)主義
- 電大財(cái)務(wù)大數(shù)據(jù)分析編程作業(yè)2
- 長(zhǎng)安歷史文化概論智慧樹知到期末考試答案2024年
評(píng)論
0/150
提交評(píng)論