标签:
(1)新建立一个MFC空白的基于视类的工程
(2)在View类中重写OnPaint消息
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 //客户区绘图 /*HDC hdc; hdc=::GetDC(m_hWnd); MoveToEx(hdc,0,0,NULL); LineTo(hdc,200,200); ::ReleaseDC(m_hWnd,hdc);*/ CDC *pdc; pdc=GetDC(); //绘制棋盘 CPen pen(PS_SOLID, 2, RGB(0,0,0)); CPen *pOldpen = pdc->SelectObject(&pen);//获取指针 int Ox=100; int Oy=100; int numx=10;//行数 int numy=10;//列数 for(int i=0;i<numx;i++)//画列线 { pdc->MoveTo(Ox+30*i,Oy); pdc->LineTo(Ox+30*i,370); } for(int j=0;j<numy;j++)//画行线 { 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);*/ //颜色填充 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,&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,&brush2); } } //绘制多边形 /*CPoint ps[6]={CPoint(300,200),CPoint(300,300),CPoint(320,350),CPoint(400,400),CPoint(400,250)}; pdc->Polygon(ps,4);*/ //客户区绘图 /*pdc->MoveTo(0,0); pdc->LineTo(200,100); ReleaseDC(pdc); //整个窗口绘图 CWindowDC dw(GetParent()); dw.MoveTo(0,0); dw.LineTo(CPoint(300,300)); //非客户区绘图 CClientDC du(GetParent()); du.MoveTo(0,0); du.LineTo(CPoint(30,200)); UpdateData(true);*/ }
(3)如果想要在菜单中设置改变其中部分格子的颜色,可以在View类中添加一个COLORREF的数据成员
然后在新建的菜单的响应函数中添加下面的代码
CColorDialog cDlg; if(cDlg.DoModal()==IDOK) { //LOGFONT logfont; m_color=cDlg.GetColor(); OnPaint(); }
(4)运行后,出来的结果如下
原始的着色效果 改变颜色后的着色效果
参考代码:
1,pund,Basic-graphics, 2014.
标签:
原文地址:http://www.cnblogs.com/arxive/p/5097217.html