MFC draws Checkers

Source: Internet
Author: User

(1) Create a new MFC blank based on the visual class of the project

(2) overriding the OnPaint message in the View class

void Cmy005view::onpaint () {CPAINTDC DC (this),//device context for painting//todo:add your message Handler code here//    Do not call Cview::onpaint () for painting messages//client area drawing/*hdc hdc;hdc=::getdc (m_hwnd); Movetoex (Hdc,0,0,null); LineTo (hdc,200,200);:: ReleaseDC (M_HWND,HDC); */CDC *PDC;PDC=GETDC ();//Draw Checkerboard CPen pen (ps_solid, 2, RGB (0,0,0)); CPen *poldpen = Pdc->selectobject (&pen);//get pointer int ox=100; int oy=100; int numx=10;//number of rows int numy=10;//column number for (int i=0;i<numx;i++)//Draw line {Pdc->moveto (Ox+30*i,oy); Pdc->lineto (Ox+30*i , 370);  } for (int j=0;j<numy;j++)//Draw line {Pdc->moveto (100,oy+30*j); Pdc->lineto (370,oy+30*j);}   /*cdc::fillrect CBrush () CBrush Brush (RGB (255, 0, 0)); dc.   Filllrect (Crect (M_point, point), &brush); *//Color fill CBrush brush1 (m_color);   for (int n=0;n<numy;n=n+2) {for (int m=0;m<numx;m=m+2) {CRect rc (ox+30*m,oy+n*30,ox+30+30*m,oy+30+n*30);  Pdc->fillrect (RC,&AMP;BRUSH1); }} CBrush Brush2 (RGB (0,0,255));        for (int p=1;p<numy-1;p=p+2) {for (int q=1;q<numx-1;q=q+2) {CRect rc (ox+30*q,oy+p*30,ox+30+30*q,oy+30+p*30);   Pdc->fillrect (RC,&AMP;BRUSH2);}}   Draw polygons/*cpoint Ps[6]={cpoint (300,200), CPoint (300,300), CPoint (320,350), CPoint (400,400), CPoint (400,250)}; Pdc->polygon (ps,4); *//client area Drawing/*pdc->moveto (0,0);pd C->lineto (200,100);    ReleaseDC (PDC); The entire window is plotted CWINDOWDC DW (GetParent ());d W. MoveTo (0,0);d W.    LineTo (CPoint (300,300));//non-client area drawing CClientDC du (GetParent ()); Du. MoveTo (0,0);d U. LineTo (CPoint (30,200)); UpdateData (TRUE); */}

(3) If you want to change the color of some of the squares in the menu, you can add a COLORREF data member to the view class

Then add the following code in the response function of the newly created menu

CColorDialog cdlg;if (Cdlg.domodal () ==idok) {//logfont LOGFONT;       M_color=cdlg.getcolor ();   OnPaint ();}

(4) After the operation, the results are as follows

The original shading effect changes color after coloring effect

Reference code:

1,pund,basic-graphics, 2014.

MFC draws Checkers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.