MFC画眼睛
换画笔(画刷)颜色(参考链接:https://blog.csdn.net/sunxiving/article/details/51272001)
- 由于画笔一旦创建后就无法修改。所以要修改画笔的颜色要用DeleteObject()函数将其删除,之后再创建新的画笔
- 例子:换为白色
CBrush brush, *oldBrush;//创建画刷
brush.CreateSolidBrush(RGB(0,0,0));//原来为黑色
oldBrush=pDC->SelectObject(&brush);
pDC->Ellipse(0,0,200,100);//画一个椭圆
brush.DeleteObject();//删掉原来brush
brush.CreateSolidBrush(RGB(255, 251, 240));//更改颜色
oldBrush=pDC->SelectObject(&brush);//重新选取
pDC->Ellipse(50,0,150,100);//再画一个颜色不同的
pDC->SelectObject(oldBrush);//还回画刷
没有参考代码,尝试自己画一波眼睛简图。
- 只给出OnDraw(CDC* pDC)函数。
void CJustPaintView::OnDraw(CDC* pDC)//paint an eye
{
CJustPaintDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect rect;
GetClientRect(&rect);
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(rect.Width(), rect.Height());
pDC->SetViewportExt(rect.Width(), -rect.Height());
pDC->SetViewportOrg(rect.Width()/2, rect.Height()/2);
rect.OffsetRect(-rect.Width()/2, -rect.Height()/2);//erweizuobiaoxi
CBrush brush, *oldBrush;
brush.CreateSolidBrush(RGB(0,0,0));
oldBrush=pDC->SelectObject(&brush);
pDC->Ellipse(0,0,200,100);
brush.DeleteObject();
//change color of brush
brush.CreateSolidBrush(RGB(255, 251, 240));
oldBrush=pDC->SelectObject(&brush);
pDC->Ellipse(50,0,150,100);
brush.DeleteObject();
//change color of brush
brush.CreateSolidBrush(RGB(0,0,0));
oldBrush=pDC->SelectObject(&brush);
pDC->Ellipse(50,25,150,75);
brush.DeleteObject();
//change color of brush
brush.CreateSolidBrush(RGB(255,0,0));
oldBrush=pDC->SelectObject(&brush);
pDC->Ellipse(75,25,125,75);
pDC->SelectObject(oldBrush);
}
效果