(1)、在应用程序中添加GDI+的包含文件gdiplus.h以及附加的类库gdiplus.lib。
通常gdiplus.h包含文件添加在应用程序的stdafx.h文件中,而gdiplus.lib可用两种进行添加:第一种是直接在stdafx.h文件中添加下列语句:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#pragmacomment(lib,"gdiplus.lib")
另一种方法是:选择"项目->属性"菜单命令,在弹出的对话框中选中左侧的"链接器->输入"选项,在右侧的"附加依赖项"框中键入gdiplus.lib,
(2)、在应用程序项目的应用类中,添加一个成员变量,如下列代码:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->ULONG_PTRm_gdiplusToken;
其中,ULONG_PTR是一个DWORD数据类型,该成员变量用来保存GDI+被初始化后在应用程序中的GDI+标识,以便能在应用程序退出后,引用该标识来调用Gdiplus:: GdiplusShutdown来关闭GDI+。
(3)、在应用类的InitInstance函数中添加GDI+的初始化代码: BOOL
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->intCCGDIPlusApp::ExitInstance()
{
Gdiplus::GdiplusShutdown(m_gdiplusToken);
returnCWinApp::ExitInstance();
}
{
Gdiplus::GdiplusShutdown(m_gdiplusToken);
returnCWinApp::ExitInstance();
}
(4)、在应用类中添加ExitInstance的重载,并添加下列代码用来关闭GDI+:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->intCGDIPlusApp::ExitInstance()
{
Gdiplus::GdiplusShutdown(m_gdiplusToken);
returnCWinApp::ExitInstance();
}
{
Gdiplus::GdiplusShutdown(m_gdiplusToken);
returnCWinApp::ExitInstance();
}
(5)、在需要绘图的窗口或视图类中添加GDI+的绘制代码:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->voidCCGDIPlusView::OnDraw(CDC*pDC)
{
CCGDIPlusDoc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
if(!pDoc)
return;
usingnamespaceGdiplus;
Graphicsgraphics(pDC->m_hDC);
GraphicsPathpath;//构造一个路径
path.AddEllipse(50,50,200,100);
//使用路径构造一个画刷
PathGradientBrushpthGrBrush(&path);
//将路径中心颜色设为蓝色
pthGrBrush.SetCenterColor(Color(255,0,0,255));
//设置路径周围的颜色为蓝芭,但alpha值为
Colorcolors[]={Color(0,0,0,255)};
INTcount=1;
pthGrBrush.SetSurroundColors(colors,&count);
graphics.FillRectangle(&pthGrBrush,50,50,200,100);
LinearGradientBrushlinGrBrush(
Point(300,50),
Point(500,150),
Color(255,255,0,0),//红色
Color(255,0,0,255));//蓝色
graphics.FillRectangle(&linGrBrush,300,50,200,100);
}
{
CCGDIPlusDoc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
if(!pDoc)
return;
usingnamespaceGdiplus;
Graphicsgraphics(pDC->m_hDC);
GraphicsPathpath;//构造一个路径
path.AddEllipse(50,50,200,100);
//使用路径构造一个画刷
PathGradientBrushpthGrBrush(&path);
//将路径中心颜色设为蓝色
pthGrBrush.SetCenterColor(Color(255,0,0,255));
//设置路径周围的颜色为蓝芭,但alpha值为
Colorcolors[]={Color(0,0,0,255)};
INTcount=1;
pthGrBrush.SetSurroundColors(colors,&count);
graphics.FillRectangle(&pthGrBrush,50,50,200,100);
LinearGradientBrushlinGrBrush(
Point(300,50),
Point(500,150),
Color(255,255,0,0),//红色
Color(255,0,0,255));//蓝色
graphics.FillRectangle(&linGrBrush,300,50,200,100);
}
