现在的位置: 首页 > 综合 > 正文

GDI+基础编程(2)——画刷的使用

2014年01月26日 ⁄ 综合 ⁄ 共 3861字 ⁄ 字号 评论关闭

代码中的注释还是很清楚的,在此不罗嗦了:

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);



}

 

为了简单,我把所有代码放在了同一个函数中,绘制的时候会覆盖。可以注释掉一部份来看效果

抱歉!评论已关闭.