原始日期:2014-03-29 20:35

众所周知,google的安卓机器人形象十分经典,包括眼睛的位置,胳膊以及天线的位置都是有固定位置和比例的,而且是最恰当的,看起来最美。而微软基础类库MFC绘图本领十分强大,相比之前Turbo C,强的不是一丁半点,其可以绘制出各种复杂炫丽的图形,不过我今天做的是一个十分简单的绘图,用刷子CBrush,画笔CPen,矩形Rectangle和椭圆Ellipse来画一个安卓机器人,没有画文字,十分简单。下面是效果图和对比图:

对比图:                                                                                         效果图:

MFC简单绘制安卓机器人-LMLPHP

MFC简单绘制安卓机器人-LMLPHP

代码写在View类中的OnDraw函数中,非常简单,以下为代码:

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);/*使用MM_ANISOTROPIC模式自定义中心为原点的直角坐标系*/
 
 //机器人的脸
 CBrush NewBrush,*pOldBrush;
 NewBrush.CreateSolidBrush(RGB(151,192,61));
 pOldBrush=pDC->SelectObject(&NewBrush);
 pDC->Ellipse(-80,-10,80,110);
 NewBrush.DeleteObject();
  
 CPen NewPen,*pOldPen;
 NewPen.CreatePen(PS_SOLID,1,RGB(151,192,61));
 pOldPen=pDC->SelectObject(&NewPen);
 pDC->Ellipse(-80,-10,80,110);
 NewPen.DeleteObject();

//脖子
 NewBrush.CreateSolidBrush(RGB(255,255,255));
 pOldBrush=pDC->SelectObject(&NewBrush);
 pDC->Rectangle(-80,-50,80,55);
 NewBrush.DeleteObject();

NewPen.CreatePen(PS_SOLID,1,RGB(255,255,255));
 pOldPen=pDC->SelectObject(&NewPen);
 pDC->Rectangle(-80,-50,80,55);
 NewPen.DeleteObject();
 
 //刷子画机器人身体
 NewBrush.CreateSolidBrush(RGB(151,192,61));
 pOldBrush=pDC->SelectObject(&NewBrush);
 pDC->Rectangle(-80,-100,80,40);
 NewBrush.DeleteObject();
 
 NewPen.CreatePen(PS_SOLID,1,RGB(151,192,61));
 pOldPen=pDC->SelectObject(&NewPen);
 pDC->Rectangle(-80,-100,80,40);
 NewPen.DeleteObject();

//刷子画眼睛
 NewBrush.CreateSolidBrush(RGB(255,255,255));
 pOldBrush=pDC->SelectObject(&NewBrush);
 pDC->Ellipse(-45,70,-25,90);//左眼睛
 pDC->Ellipse(25,70,45,90);//右眼睛
 NewBrush.DeleteObject();

//四肢
 NewBrush.CreateSolidBrush(RGB(151,192,61));
 pOldBrush=pDC->SelectObject(&NewBrush);
  //左胳膊
 pDC->Ellipse(90,15,130,55);
 pDC->Ellipse(90,-70,130,-30);
 pDC->Rectangle(90,35,130,-50);
  //右胳膊
 pDC->Ellipse(-130,15,-90,55);
 pDC->Ellipse(-130,-70,-90,-30);
 pDC->Rectangle(-130,35,-90,-50);
  //左腿
 pDC->Ellipse(-55,-170,-15,-130);
 pDC->Rectangle(-55,-100,-15,-150);
  //右腿
 pDC->Ellipse(15,-170,55,-130);
 pDC->Rectangle(15,-100,55,-150);

NewBrush.DeleteObject();

NewPen.CreatePen(PS_SOLID,5,RGB(151,192,61));
 pOldPen=pDC->SelectObject(&NewPen);
 pDC->MoveTo(-30,105);
 pDC->LineTo(-50,130);

//天线
 pDC->MoveTo(30,105);
 pDC->LineTo(50,130);

05-04 07:21