Tool file name based on MFC development: Draw Create a single document, MFC standard one. Displays the View 1.mainfrm.cpp int cmainframe::oncreate (lpcreatestruct lpcreatestruct) SetMenu (NULL); Remove the menu Cbrs_bottom the toolbar to the reality below to remove the status code from the bool CMainFrame::P Recreatewindow (createstruct& cs) cs.style=ws_popup;2. Draw.cpp (App Class) BOOL cdrawapp::initinstance () M_pmainwnd->showwindow (sw_maximize); 3. Add press ESC to exit the interface in Class View CDrawView add Message onkeydownif (NChar ==vk_escape) {::P ostquitmessage (0);} 4. Create a new toolbar in the Resource view and add the message processing in cdrawview.hafx_msg void OnQuit ();//exit afx_msg void Oncureve (); afx_msg void OnLine (); afx_ msg void Onellipse (); afx_msg void Onrect (); afx_msg void Ontriangle (); afx_msg void Onfill (); afx_msg void OnColor (); afx_ msg void OnSave (); in Cdrawview.cppon_command (Id_quit,&cmainframe::onquit) on_command (id_curve,&cmainframe: : Oncureve) On_command (id_online,&cmainframe::online) on_command (id_els,&cmainframe::onellipse) ON_COMMAND (Id_rect,&cmainframe::onrect) On_command (Id_tra,&cmainframe::ontriangle) on_command (id_rect,&cmainframe::onrect) ON_COMMAND (ID_FILL, &cmAinframe::onfill) On_command (id_save,&cmainframe::onsave) on_command (Id_color,&cmainframe::oncolor) 5. Display the desktop picture to the view in Cmainfram.hlist<cbitmap *> m_lstdesktopbmp;//each time you draw a pen to the list of int m_nscreenx;int M_nscreeny; In Cmainframe.cppcmainframe::cmainframe () {m_nscreenx=getsystemmetrics (Sm_cxscreen); M_nscreeny=getsystemmetrics ( Sm_cyscreen);//Get the picture of the desktop CWINDOWDC m_dcdesktop (GetDesktopWindow ());//Save this picture CBitmap *bitmap=new cbitmap;bitmap-> CreateCompatibleBitmap (&m_dcdesktop,m_nscreenx,m_nscreeny);//Create a DCCDC CDC;CDC. CreateCompatibleDC (&m_dcdesktop); CDC. SelectObject (bitmap);//Put the desktop picture in CDC. BitBlt (0,0,m_nscreenx,m_nscreeny,&m_dcdesktop,0,0,srccopy);//put bitmap into the list m_lstdesktopbmp.push_back (bitmap) ;} In CDrawView.h void Cdrawview::ondraw (cdc*/*pdc*/)//Put the picture of the tail node of the list on the view cmainframe* frame = (cmainframe*) AfxGetMainWnd (); CCLIENTDC DC (this); Windows CDC CDC; The picture CDC in the tail node of the chain. CreateCompatibleDC (&DC); CDC. SelectObject (Frame->m_lstdesktopbmp.back ()); Put the link in the tail nodeThe picture is loaded into the CDCDC. BitBlt (0,0,frame->m_nscreenx,frame->m_nscreeny,&cdc,0,0,srccopy); draw 1, the left mouse button lifted to save the picture to the tail of the list (M_ndrawstyle What graphics are broken, m_nbdrawflag judge whether to draw, to initialize in constructor) void Cdrawview::onlbuttonup (UINT nflags, CPoint point) {//TODO: Add Message Handler code here and/ or call the default value m_bdrawflag=false;//save the picture to the linked list CMainFrame *frame= (cmainframe*) AfxGetMainWnd (); CCLIENTDC DC (this); CBitmap *bitmap=new Cbitmap;bitmap->createcompatiblebitmap (&dc,frame->m_nscreenx,frame->m_nscreeny) ; CDC CDC;CDC. CreateCompatibleDC (&DC); CDC. SelectObject (bitmap); CDC. BitBlt (0,0,frame->m_nscreenx,frame->m_nscreeny,&dc,0,0,srccopy); Frame->m_lstdesktopbmp.push_back ( Bitmap); Cview::onlbuttonup (nflags, point);} 2. Paint void Cdrawview::onmousemove (UINT nflags, CPoint point) {//TODO: Add the message Handler code here and/or call the default value if (m_bdrawflag==true) { CMainFrame *frame= (cmainframe*) AfxGetMainWnd (); CCLIENTDC DC (this);//Create Compatibility dc//The last picture of the linked list refreshes the CDC CDC;CDC. CreateCompatibleDC (&DC); CDC. SelectObject (Frame->m_lstdesktopbmp.back ());//Create a bitmap bitmap//Copy the end node of the linked listPicture CBitmap Bitmap;bitmap. CreateCompatibleBitmap (&dc,frame->m_nscreenx,frame->m_nscreeny); CDC TEMPDC;TEMPDC. CreateCompatibleDC (&DC); TEMPDC. SelectObject (bitmap); TEMPDC. BitBlt (0,0,frame->m_nscreenx,frame->m_nscreeny,&cdc,0,0,srccopy);//cannot transmit DC to tempdc//see what graphics switch (m_ Ndrawstyle) {case ID_CURVE:DC. MoveTo (M_POINTMOUSEDOWN.X,M_POINTMOUSEDOWN.Y);d C. LineTo (POINT.X,POINT.Y); M_pointmousedown=point;break;case id_online://cannot use CDC paint because CDC is the last bitmap in the list, changing CDC is changing the last picture So TEMPDC on the top of the picture//move once created bitmap deleted, no change TEMPDC. MoveTo (M_POINTMOUSEDOWN.X,M_POINTMOUSEDOWN.Y); TEMPDC. LineTo (POINT.X,POINT.Y);d C. BitBlt (0,0,frame->m_nscreenx,frame->m_nscreeny,&tempdc,0,0,srccopy); Break;case ID_ELS:TEMPDC. Selectstockobject (Null_brush); TEMPDC. Ellipse (M_POINTMOUSEDOWN.X,M_POINTMOUSEDOWN.Y,POINT.X,POINT.Y);d C. BitBlt (0,0,frame->m_nscreenx,frame->m_nscreeny,&tempdc,0,0,srccopy); Break;case ID_RECT:TEMPDC. Rectangle (M_POINTMOUSEDOWN.X,M_POINTMOUSEDOWN.Y,POINT.X,POINT.Y);d C. BitBlt (0,0,frame->m_nscreenx,frame->m_nscreeny,&tempdc,0,0,srccopy); Break;case ID_TRA:TEMPDC. Selectstockobject (Null_brush); Point rect[3]={{(point.x+m_pointmousedown.x)/2,m_pointmousedown.y},{m_pointmousedown.x,point.y},{point.x, POINT.Y}};TEMPDC. Polygon (rect,3);d C. BitBlt (0,0,frame->m_nscreenx,frame->m_nscreeny,&tempdc,0,0,srccopy); break;}} Cview::onmousemove (nflags, point);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
MFC tools (constantly updated)