用MFC畫一個美國隊長的盾牌

 

    //美國隊長的盾牌	
	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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章