天天看點

GDI+基礎程式設計(2)——畫刷的使用

代碼中的注釋還是很清楚的,在此不羅嗦了:

void CGDI畫刷Dlg::GDIBrushDrawImage(CDC* pDC)
{
	Graphics g(pDC->m_hDC);
	//用單色畫刷填充閉合曲線區域
	SolidBrush solidBrush(Color(255,0,255,0));
	PointF p1(30.0f,30.0f);
	PointF p2(100.0,100.0f);
	PointF p3(60.0,160.0f);
	PointF p4(5.0f,100.0f);
	PointF p5(30.0f,200.0f);
	PointF pt[4]={p1,p2,p3,p4};
	//填充閉合區域
	g.FillClosedCurve(&solidBrush,pt,4,FillModeAlternate,1.0);
	//構造比和多邊形
	PointF p[5]={p1,p2,p3,p4,p5};
	//填充多邊形
	solidBrush.SetColor(Color(255,155,0,200));
	g.FillPolygon(&solidBrush,p,5);
/*繪制正夜曲線
*   對應的數學公式為:
*            R=A*Sin(N*Angle)
*            X=R*Cos(Angle)
*            Y=R*Sin(Angle)
*其中A相當于葉子的長度,N是決定葉子數量的一個常量,Angle相當于葉子的旋轉角度*/

	int cx,cy;
	CRect rc;
	GetClientRect(&rc);
	//以目前視窗的正中心繪制正夜曲線
	cx=rc.Width()/2;
	cy=rc.Height()/2;
	int LeafLen=200;//設定葉子長度
	int LeafNum=20;
	float PI=3.14;
	int x,y,x2,y2,r;
	GraphicsPath tempPath(FillModeAlternate);
	for(float i=0.0f;i<PI*2+0.1f;i+=PI/180.0)
	{
		r=abs(LeafLen*cos(LeafNum*i));
		x=r*cos(float(i));
		y=r*sin(float(i));
		x2=cx+x;
		y2=cy+y;
		tempPath.AddLine(x2,y2,x2,y2);
	}
	//接下來填充區域
	g.FillPath(&solidBrush,&tempPath);
	Pen pen1(Color(255,255,0,0),1);
	g.DrawLine(&pen1,cx,0,cx,cy*2);
	g.DrawLine(&pen1,0,cy,cx*2,cy);
	//使用影線畫刷
	Color black(255,0,0,0),white(255,255,255,255);
	HatchBrush brush(HatchStyleHorizontal,black,white);//風格1
	g.FillRectangle(&brush,100,40,100,50);
	//
	HatchBrush br1(HatchStyleVertical,black,white);//風格2
	g.FillRectangle(&br1,210,40,100,50);
	//
	HatchBrush br2(HatchStyleForwardDiagonal,black,white);//風格3
	g.FillRectangle(&br2,320,40,100,50);
	//
	HatchBrush br3(HatchStyleBackwardDiagonal,black,white);//風格4
	g.FillRectangle(&br3,430,40,100,50);
	/
	HatchBrush br4(HatchStyleCross,black,white);//風格5
	g.FillRectangle(&br4,150,100,100,50);
	///
	HatchBrush br5(HatchStyleDiagonalCross,black,white);//風格6-交叉對角線
	g.FillRectangle(&br5,380,100,100,50);
	Color black(255,0,0,0);
	Color white(255,255,255,255);
	SolidBrush redBrush(Color(255,255,0,0));
	Pen pen(Color(255,0,0,255));
	CRect rc;
	GetClientRect(&rc);
	int columnCount=(int)rc.Width()/40;//設定每塊矩形的大小為40X40
	int rol=0;
	int column=0;
	CString str;
	WCHAR WideChar[2];
	Font ft(L"Arial",16);
	//在目前視窗使用53種風格的影線畫刷填充矩形
	for(int i=0;i<53;i++)
	{
		if(rol>columnCount-1)
		{
			column++;
			rol=0;
		}
		HatchBrush tempBrush(HatchStyle(i),black,white);
		g.FillRectangle(&tempBrush,rol*40,column*40,35,35);//填充矩形區域
		g.DrawRectangle(&pen,rol*40,column*40,35,35);//繪制邊框
		str.Format(L"%d",i);
		int strLen=str.GetLength();
		memcpy(WideChar,str.GetBuffer(),sizeof(WideChar));
		StringFormat format;
		RectF rect(rol*40,column*40,35,35);
		format.SetAlignment(StringAlignmentCenter);
		format.SetLineAlignment(StringAlignmentCenter);
		g.DrawString(WideChar,strLen,&ft,rect,&format,&redBrush);
		rol++;
	}
	/*設定畫刷原點*/
	Color black(255,0,0,0),white(255,255,255,255);
	HatchBrush hatchBrush(HatchStyle(20),black,white);
	//在垂直方向填充8個矩形,使用預設的畫刷原點
	for(int i=0;i<8;i++)
		g.FillRectangle(&hatchBrush,0,i*50,100,50);
	//使用不同的畫刷原點
	for(int i=0;i<8;i++)
	{
		g.SetRenderingOrigin(i,0);
		g.FillRectangle(&hatchBrush,100,i*50,100,50);
	}
    /*使用紋理畫刷,紋理化刷實際是将圖檔在目标區域進行平鋪*/
	Pen pen(Color(255,0,0,255),2);
	SolidBrush brush(Color(255,0,0,0));
	Font font(L"宋體",20);
	RectF rc1(10,10,200,200);
	RectF rc2(210,10,200,200);
	RectF rc3(410,10,200,200);
	Image image(L"2.jpg");
	TextureBrush textureBrush(&image);//構造紋理化刷使用預設方式
	g.FillEllipse(&textureBrush,rc1);
	g.DrawEllipse(&pen,rc1);
	g.DrawString(L"圖檔原始大小",6,&font,PointF(40,220),NULL,&brush);
	//方法二,隻使用給定圖檔的部分區域
	TextureBrush tBrush2(&image,Rect(0,0,60,50));
	g.FillEllipse(&tBrush2,rc2);
	g.DrawEllipse(&pen,rc2);
	g.DrawString(L"隻用部分區域",6,&font,PointF(240,220),NULL,&brush);
	/*構造紋理化刷3:将制定圖檔進行縮放*/
	TextureBrush tBrush3(&image);
	tBrush3.SetTransform(&Matrix(0.5f,0.0f,0.0f,0.5f,0.0f,0.0f));//對話刷進行50%的縮放
	g.FillEllipse(&tBrush3,rc3);
	g.DrawEllipse(&pen,rc3);
	g.DrawString(L"比例縮小圖",6,&font,PointF(440,220),NULL,&brush);



}
           

為了簡單,我把所有代碼放在了同一個函數中,繪制的時候會覆寫。可以注釋掉一部份來看效果