GDI+在画刷方面对gdi进行了功能上的扩展,他支持线形渐变和渐变画刷,可以填充图形,路径和区域.渐变画刷同时还可以用于绘制直线,曲线和路径.因此,可以用一个路径来设置渐变颜色.在vc++.net2005中使用gdi+,首先,需要在stafx.h中包括<gdiplus.h>头文件,然后在属性中->连接器->附加引用库添加gdiplus.lib文件.然后在应用程序中的oninitapplication(),初始化Gdiplus设备,代码如下:
using namespace Gdiplus;
GdiplusStartupInput ginput;
GdiplusStartup(&m_gdiplusToken,&ginput,NULL);
当应用程序退出之后,要在ExitInstance中添加以下函数:
using namespace Gdiplus;
GdipusShutdown(m_gdiplusToken);
接下来,就可以使用GDI+了.在OnDraw里添加以下代码
using namespace Gdiplus;
Graphics g(pDC->m_hDC);
Point points[] = Point points[] = {Point(75,0),Point(100,50),Point(150,40),Point(112,75),
Point(150,150),Point(75,100),Point(0,150),Point(47,75),Point(0,50),Point(50,50)};
GraphicsPath path;
path.AddLines(points,10);
PathGradientBrush pgBrush(&path);
pgBrush.SetCenterColor(Color(255,255,0,0));
Color colors[] = {Color(255,0,0,0),Color(255,0,255,0),
Color(255,0,0,2555),Color(255,255,255,255),
Color(255,0,0,0),Color(255,0,255,0),
Color(255,0,0,255),Color(255,255,255,255),
Color(255,0,0,0),Color(255,0,255,0)};
int count = 10;
pgBrush.SetSurroundColors(colors,&count);
g.FillPath(&pgBrush,&path);