用MFC画一个美国队长的盾牌

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lijinshanba/article/details/83742872
    //美国队长的盾牌	
	CPen p1Pane(PS_SOLID,1,RGB(255,0,0));//创建画表盘的笔
	CBrush b1Pane(RGB(225,0,0));			//创建画表盘的刷子CBrush b1Pane(RGB(225,0,0));			
	
    //使用笔和刷子
	pDC->SelectObject(&p1Pane);
	pDC->SelectObject(&b1Pane);

	CRect rect;  
	GetClientRect(&rect);//获取客户端的矩形窗口

	int w = rect.Width()/2;
	int h = rect.Height()/2;
	CPoint center(w,h); //定义圆心
	int R = w<h?w:h;	//定义半径
	int x = center.x;	//得到圆心横纵坐标的数值
	int y = center.y;

	// 开始绘图
	pDC->Ellipse(x-R,y-R,x+R,y+R);				//画外圈

	CBrush b2Pane(RGB(255,255,255));			//定义刷子
	pDC->SelectObject(&b2Pane);					//换白色刷子
	pDC->Ellipse(x-(R-(R*3)/13),y-(R-(R*3)/13),x+(R-(R*3)/13),y+(R-(R*3)/13));//画里圈

	pDC->SelectObject(&b1Pane);					//换红色刷子
	pDC->Ellipse(x-(R-2*(R*3)/13),y-(R-2*(R*3)/13),x+(R-2*(R*3)/13),y+(R-2*(R*3)/13));//画里圈
	
	CBrush b3Pane(RGB(0,0,255));			
	pDC->SelectObject(&b3Pane);					//换蓝色刷子
	pDC->Ellipse(x-(R*4)/12,y-(R*4)/12,x+(R*4)/12,y+(R*4)/12);//画里圈

	//CBrush b4Pane(RGB(0,0,0));			//´´½¨»­±íÅ̵ÄË¢×Ó
	CBrush b4Pane(RGB(255,255,255));
	pDC->SelectObject(&b4Pane);					//换白色刷子
	
	//mPoint.x = x + int(hl*sin(hAngle*PI/180));
	//mPoint.y = y - int(hl*cos(hAngle*PI/180));
	const double PI = 3.1415926;
	int sR = (R*4)/12;   //small  R
	int ssR = sR/2;     // small small R

	pDC->BeginPath();					    //建立路径,给五角星涂色
	pDC->MoveTo(x, y-sR);				//先算SR,再算SSR,不然会乱掉的
	pDC->LineTo(x + int(ssR*sin(36*PI/180)),y - int(ssR*cos(36*PI/180)));
	pDC->LineTo(x + int(sR*sin(72*PI/180)),y - int(sR*cos(72*PI/180)));
	pDC->LineTo(x + int(ssR*sin(108*PI/180)),y - int(ssR*cos(108*PI/180)));
	pDC->LineTo(x + int(sR*sin(144*PI/180)),y - int(sR*cos(144*PI/180)));
	pDC->LineTo(x + int(ssR*sin(180*PI/180)),y - int(ssR*cos(180*PI/180)));	
	pDC->LineTo(x + int(sR*sin(216*PI/180)),y - int(sR*cos(216*PI/180)));
	pDC->LineTo(x + int(ssR*sin(252*PI/180)),y - int(ssR*cos(252*PI/180)));
	pDC->LineTo(x + int(sR*sin(288*PI/180)),y - int(sR*cos(288*PI/180)));
	pDC->LineTo(x + int(ssR*sin(324*PI/180)),y - int(ssR*cos(324*PI/180)));
	pDC->LineTo(x, y-sR);		//构成闭合回路啊大哥
	pDC->EndPath();					//路径结束
	
	//看的那篇博客代码写错了,那个人真是误人子弟,还是我自己改对的
	CRgn rgn;
	rgn.CreateFromPath(pDC);
	pDC->InvertRgn(&rgn);
	pDC->FillRgn(&rgn,&b4Pane);

一开始结果是:

后来仔细一想肯定哪里画错了,果然!!!!

结果对了!!!!

原因分析:

见下图所示,正确的路径是A--G--B--H--C--I--D--J--E--F--A(最后要回到A点)

然而我一开始做法是A--B---C--D--E--A,肯定就错了呀。

剩下的没什么好讲的,注释说得很清楚了,要注意的就是你要不断地去计算每个点的坐标,保证正确,用三角函数,高中学的。

我也算是完成了用MFC画美国队长盾牌的小目标了哈哈哈,之前好像学着别人用python画过一次???我忘记了。

参考资料:

都有错的地方,不过好歹给了我指导,唉。不知道他们是不是哪里抄来的

https://blog.csdn.net/u011467044/article/details/50488992?locationNum=11

https://blog.csdn.net/wang1025475397/article/details/9014241

猜你喜欢

转载自blog.csdn.net/lijinshanba/article/details/83742872