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頁
免費(fèi)預(yù)覽已結(jié)束,剩余33頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、VC-6.0-下MF終于對話框繪制圖形附錄表一、具體步驟:1、選擇菜單欄的“新建”,選擇MFC項(xiàng)目 名為DrawGraphics ,并設(shè)置為基于對話框的項(xiàng) 目,如圖-1、圖-2所示;文件 工程I工作區(qū) 其它支檔I望ATI CUM AppWIzard Clutter Revuum Type WkardCiisnm AppWi?nrdDatabase Project口ErvShjdE Add-in V/izardEVended Slorcd Hroc WizardISAPI ExTension WizardMakefileMl C AdiivcX C o ntrwlWh ? ard位置區(qū)J:Fil

2、 I MICROSUf 二一一iMFCAppVmard mIFT 卸KpWi,nlll |勺工 N w DnlAihnn WizarrliIl Utility Prled 亙Win* Apiplknlinn 2jWini3? Con>!R(iilr ApplintinnWin32 DynamlcrLIiik Library % Win32 Static Litirary宗創(chuàng)定新的二作空間也) 廣棟加到當(dāng)忸工作空陶出r k屆十21:工口回12d Win32砒定 I 取消|圖-22、按下鍵盤上的Ctrl+W 鍵,調(diào)出類向?qū)В?選擇"Add Class",并點(diǎn)擊"

3、New"添加一個(gè)顏 色按鈕類CColorButton ,繼承自CButton類, 如圖-3所示:圖-33、在CColorButton中添加如下成員變量:COLORREF color; 按鈕顏色CPoint arrays4; 按鈕頂點(diǎn)坐標(biāo)BOOL IsShow; 是 否顯示按鈕BOOL IsPressed; 按 鈕是否被按下七三產(chǎn)習(xí)雨用宜昔目入3 ±152:包彳腰謚;L -L 閾晝q - G區(qū)巖晶舊c tCDrawraiphic血為三I的七怕"metnbE舊三|,。出加好Wr-DrswGraphicp dascec小 CAbuutDIig- 4a p Go to D

4、dlni&cnAdd MeedsFuFUium.Add MrrTfctwr jariab e-訐(IInvalid丫旦口用匚配XGiAdd Virtua Funcort.Add Endows Handlpr.3 RffvrwcKH.CDialog:Id CDrduCr4、右擊 CColorButton 類,選擇 Add Virtul function )重寫其Draltem 函數(shù))如圖-4)圖-5 所示:圖-4New Virtual Override for dass CColorButtonNew Virtual FunctionsExiting virtual function o

5、verridesCalcWindnwRectCreale DEfWindowProc DesUuyWJuduivDo口自加 ExchangeGetScrollHarClrlOnAmbientProperiy UnChtldNotity OnCmdMsgOnCrimmandOnFiMlHeleaeeOnNclifyPostN Destroy Pre Create Window Pre Sub cl ass Window PreTranslateMessage SerializeDrawltfm確定取消Add HanijicrAdd and Edit IEdit ExistingC s I cWi

6、 nd wRcctQ; CampulJEs the window size based gri the cl it: nt sift:圖-55、為其Drawitem 函數(shù)中添加如下代碼:CRect rect;GetClientRect(rect);CDC dc;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.lef

7、t,rect.bottom);/設(shè)置背景透明dc.SetBkMode(TRANSPARENT);if (IsShow)/創(chuàng)建一個(gè)位圖畫刷CBrush brush(color);dc.SelectObject(&brush);CPen pen(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(int i=1;i<4;i

8、+)dc.LineTo(arraysi);dc.LineTo(arrays0);elseCPen pen(PS_DASHDOTDOT,2,color);dc.SelectObject(&pen);dc.MoveTo(arrays0);for(int i=1;i<4;i+)dc.LineTo(arraysi);dc.LineTo(arrays0);/繪制按鈕文本CString str;GetWindowText(str);dc.SetTextColor(RGB(255-GetRValue(color), 255-GetGValue(color),255-GetBValue(colo

9、r);dc.DrawText(str,CRect(0,0,rect.right,rect.bott om),DT_CENTER|DT_VCENTER|DT_SINGL ELINE);6、為CColorButton 添加屬性設(shè)置函數(shù):voidCColorButton:SetDrawColor(COLORREF m_color,BOOL IsShow)this->color = m_color;this->IsShow=IsShow;7、為CColorButton 添加WM_LBUTTONDOWN 和 WM_LBUTTONUP消息,其消息處理函數(shù)如下:void CColorButton

10、:OnLButtonDown(UINTnFlags, CPoint point)/ TODO: Add your message handler codehere and/or call defaultIsPressed = true;CButton:OnLButtonDown(nFlags, point);void CColorButton:OnLButtonUp(UINTnFlags, CPoint point)/ TODO: Add your message handler codehere and/or call defaultIsPressed = false;CButton:OnL

11、ButtonUp(nFlags, point); 8、為DrawGraphics項(xiàng)目的中的對話框資源拖拽如下控件,如圖-6所示,并按表-1設(shè)置其相關(guān) ID ;作用及類別矩形單選按鈕圓形單選按鈕直線單選按鈕涂鴉單選按鈕實(shí)線單選按鈕虛線單選按鈕線條粗細(xì)下拉列表圖-6IDIDC_RADIO_RECTIDC_RADIO_ROUNDIDC_RADIO_LINEIDC_RADIO_SELFIDC_RADIO_SLOIDLINEIDC_RADIO_DOTTEDLINEIDC_COMBO_LINEVALUEIDC_STATIC_LINEEXAMPLE顯示線條粗細(xì)的 Picture 控件IDC_CHECK_BO

12、RDER IDC_RADIO_SOLID IDC_RADIO_GRADIENTIDC_RADIO_NULLIDC_BUTTON_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ù)中添加初始化代碼:BOOL CDrawGraphics

13、Dlg:OnInitDialog()/*省略無關(guān)代碼*/ TODO: Add extra initialization hereCButton *cb;/設(shè)置相關(guān)按鈕的默認(rèn)選中狀態(tài)cb=(CButton*)GetDlgItem(IDC_RADIO_RECT);cb->SetCheck(1);cb=(CButton*)GetDlgItem(IDC_RADIO_SLOIDLINE);cb->SetCheck(1);cb=(CButton*)GetDlgItem(IDC_CHECK_BORDER);cb->SetCheck(1);cb=(CButton*)GetDlgItem(ID

14、C_RADIO_SOLID);cb->SetCheck(1);cb=(CButton*)GetDlgItem(IDC_RADIO_PEN);cb->SetCheck(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,TRU E);m_IsPressed=FALSE;m_nWidth=4;m_

15、nPenStyle=PS_SOLID;return TRUE; / return TRUE unless you set the focus to a control 10、為CDrawGraphicsDlg 添加WM_LBUTTONDOWN 和WM_LBUTTONUP , WM_MOUSEMOVE 消息,其消息處理函數(shù)如下:voidCDrawGraphicsDlg:OnLButtonDown(UINTnFlags, CPoint point)/ TODO: Add your message handler code here and/or call defaultm_IsPressed=TR

16、UE;SetCursor(m_Hcursor);if(IsDlgButtonChecked(IDC_RADIO_SELF)m_pOld=point;else if(IsDlgButtonChecked(IDC_RADIO_LINE)m_pOld=point;m_PointOrigin=point;else if(IsDlgButtonChecked(IDC_RADIO_RECT)m_pOld=point;m_PointOrigin=point;else if(IsDlgButtonChecked(IDC_RADIO_ROUND)m_pOld=point;m_PointOrigin=point;

17、CDialog:OnLButtonDown(nFlags, point);voidCDrawGraphicsDlg:OnLButtonUp(UINT nFlags, CPoint point)/ TODO: Add your message handler code here and/or call defaultm_IsPressed=FALSE;ClipCursor(NULL);/解除鼠標(biāo)的限定CClientDC dc(this);CPenpen(m_nPenStyle,m_nWidth,m_ForeColor);dc.SelectObject(&pen);if(IsDlgButt

18、onChecked(IDC_RADIO_LINE)/選擇的為畫直線dc.SetROP2(R2_NOT);/逆轉(zhuǎn)當(dāng)前屏幕顏色來畫線的繪圖方式dc.MoveTo(m_PointOrigin);dc.LineTo(m_pOld);/ 擦去上一次的臨時(shí) 線dc.SetROP2(R2_COPYPEN);/ 缺省繪圖模式,像素為畫筆顏色dc.MoveTo(m_PointOrigin);dc.LineTo(point);/ 繪制固定線else if(IsDlgButtonChecked(IDC_RADIO_RECT) /選擇的為畫矩形dc.SelectObject(GetStockObject(NULL_B

19、RUS H);/選擇空話刷dc.SetROP2(R2_NOT);CRectOldrect(m_PointOrigin,m_pOld);dc.Rectangle(&Oldrect);/清除之前的矩形繪圖dc.SetROP2(R2_COPYPEN);CBrush brush(m_BkColor); dc.SelectObject(&brush);/畫這次的矩形if(IsDlgButtonChecked(IDC_RADIO_NULL)/如果選擇無填充的話,使用空畫刷dc.SelectObject(GetStockObject(NULL_BRUS H);CRect Nowrect(m_

20、PointOrigin,point);dc.Rectangle(&Nowrect);if(IsDlgButtonChecked(IDC_RADIO_GRADIENT)如果選擇漸變的話,調(diào)用漸變函數(shù)DrawGradient(dc.GetSafeHdc(),Nowrect,m_For eColor,m_BkColor,1);elseif(IsDlgButtonChecked(IDC_RADIO_ROUND) /如果選擇繪制橢圓dc.SelectObject(GetStockObject(NULL_BRUSH);dc.SetROP2(R2_NOT);CRectOldrect(m_PointO

21、rigin,m_pOld);dc.Ellipse(&Oldrect);dc.SetROP2(R2_COPYPEN);CBrush brush(m_BkColor); dc.SelectObject(&brush);if(IsDlgButtonChecked(IDC_RADIO_NULL)dc.SelectObject(GetStockObject(NULL_BRUS H);CRect Nowrect(m_PointOrigin,point);if(IsDlgButtonChecked(IDC_RADIO_GRADIEN T)如果選擇漸變的話,調(diào)用漸變函數(shù)DrawGradient

22、(dc.GetSafeHdc(),Nowrect,m_For eColor,m_BkColor,1);dc.Ellipse(&Nowrect);InvalidateMyRect();/ 刷新工具區(qū)CDialog:OnLButtonUp(nFlags, point);voidCDrawGraphicsDlg:OnMouseMove(UINT nFlags, CPoint point)/ TODO: Add your message handler code here and/or call defaultif (m_IsPressed&&point.x>205)/

23、如果鼠標(biāo) 移動(dòng)位置到達(dá)畫板區(qū)CRect rect;m_Graphics.GetClientRect(&rect);m_Graphics.ClientToScreen(&rect);ClipCursor(rect);/ 限定光標(biāo)在指定矩 形CClientDC dc(this);if(m_IsPressed&&IsDlgButtonChecked(IDC_RA DIO_SELF)/ 如果選擇了涂鴉CPen pen(m_nPenStyle,m_nWidth,m_ForeColor);/創(chuàng)建畫筆dc.SelectObject(&pen);/選入畫筆dc.Move

24、To(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(m_PointOrigin);dc.LineTo(m_pOld); / 擦去上一次的線dc.MoveTo(m_PointOrigin);dc.LineTo(point);/ 繪制這一次的臨時(shí)線m_pOld=point;else if(m_IsPr

25、essed&&IsDlgButtonChecked(IDC_R ADIO_RECT)/ 如果選擇了矩形dc.SetROP2(R2_NOT);CPen pen(PS_DOT,1,m_ForeColor);dc.SelectObject(&pen);dc.SelectObject(GetStockObject(HOLLOW_BR USH);CRect Oldrect(m_PointOrigin,m_pOld); dc.Rectangle(&Oldrect);CRect Nowrect(m_PointOrigin,point);dc.Rectangle(&No

26、wrect);m_pOld=point;pen.DeleteObject();else if(m_IsPressed&&IsDlgButtonChecked(IDC_RA DIO_ROUND)/ 如果選擇了圓CClientDC dc(this); dc.SetROP2(R2_NOT);CPen pen(PS_DOT,1,m_ForeColor);dc.SelectObject(&pen);dc.SelectObject(GetStockObject(HOLLOW_BR USH);CRect Oldrect(m_PointOrigin,m_pOld); dc.Ellips

27、e(&Oldrect);CRect Nowrect(m_PointOrigin,point);dc.Ellipse(&Nowrect);m_pOld=point;pen.DeleteObject();CDialog:OnMouseMove(nFlags, point);11、為背景色、前景色、無填充、實(shí)線、虛線、邊框、 清空畫板按鈕添加按鈕單擊消息,響應(yīng)函數(shù)如下:void CDrawGraphicsDlg:OnButtonBkcolor()/ TODO: Add your control notification handler code hereCColorDialog cd

28、g;if (cdg.DoModal()=IDOK) /調(diào)用顏色對 話框 m_BkColor=cdg.GetColor();/更改背景色m_BkCButton.SetDrawColor(m_BkColor,TR UE);/更改背景色按鈕顏色I(xiàn)nvalidateMyRect();/刷新工具區(qū)voidCDrawGraphicsDlg:OnButtonForecolor()/ TODO: Add your control notification handler code hereCColorDialog cdg;if (cdg.DoModal()=IDOK)m_ForeColor=cdg.GetCo

29、lor();m_ForeCButton.SetDrawColor(m_ForeColor, TRUE);InvalidateMyRect();void CDrawGraphicsDlg:OnRadioNull()/ TODO: Add your control notification handler code hereif(!IsDlgButtonChecked(IDC_CHECK_BORDER)/無填充時(shí)不能也無邊框CButton*cb=(CButton*)GetDlgItem(IDC_CHECK_BORDER);cb->SetCheck(1);void CDrawGraphicsD

30、lg:OnRadioSloidline()/ TODO: Add your control notification handler code hereCStringstr9="1px","2px","3px","4px","5px","6px","8px","10px","12px"m_ComboLineValue.ResetContent();for (int i=0;i<9;i+)m_ComboLineV

31、alue.AddString(stri);m_ComboLineValue.SetCurSel(3);m_nWidth=4;m_nPenStyle=PS_SOLID;InvalidateMyRect();voidCDrawGraphicsDlg:OnRadioDottedline()/ TODO: Add your control notification handler code herefor (int i=11;i>0;i-)m_ComboLineValue.DeleteString(i);m_ComboLineValue.SetCurSel(0);m_nWidth=1;m_nPe

32、nStyle=PS_DOT;InvalidateMyRect();void CDrawGraphicsDlg:OnCheckBorder()/ TODO: Add your control notification handler code hereif(IsDlgButtonChecked(IDC_CHECK_BORDER)m_nPenStyle=PS_SOLID;elsem_nPenStyle=PS_NULL;if(IsDlgButtonChecked(IDC_RADIO_NULL)/既不能無邊框也不能無填充CButton*cb=(CButton*)GetDlgItem(IDC_CHECK

33、_BORDER);cb->SetCheck(1);void CDrawGraphicsDlg:OnRadioClear()/ TODO: Add your control notification handler code hereInvalidate(TRUE);12、為選 擇 畫 筆 寬 度 的 下 拉 列 表 添 加CBN_SELCHAGE 消息,消息處理函數(shù)如下:void CDrawGraphicsDlg:OnRadioSloidline()/ TODO: Add your control notification handler code hereCStringstr9=&qu

34、ot;1px","2px","3px","4px","5px","6px" ,"8px","10px","12px"m_ComboLineValue.ResetContent();for (int i=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

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論