(1)、在應用程式中添加GDI+的包含檔案gdiplus.h以及附加的類庫gdiplus.lib。
通常gdiplus.h包含檔案添加在應用程式的stdafx.h檔案中,而gdiplus.lib可用兩種進行添加:第一種是直接在stdafx.h檔案中添加下列語句:
#pragma comment( lib, "gdiplus.lib" )
另一種方法是:選擇"項目->屬性"菜單指令,在彈出的對話框中選中左側的"連結器->輸入"選項,在右側的"附加依賴項"框中鍵入gdiplus.lib,
(2)、在應用程式項目的應用類中,添加一個成員變量,如下列代碼:
ULONG_PTR m_gdiplusToken;
其中,ULONG_PTR是一個DWORD資料類型,該成員變量用來儲存GDI+被初始化後在應用程式中的GDI+辨別,以便能在應用程式退出後,引用該辨別來調用Gdiplus:: GdiplusShutdown來關閉GDI+。
(3)、在應用類的InitInstance函數中添加GDI+的初始化代碼: BOOL
int CCGDIPlusApp::ExitInstance()
{
Gdiplus::GdiplusShutdown(m_gdiplusToken);
return CWinApp::ExitInstance();
}
(4)、在應用類中添加ExitInstance的重載,并添加下列代碼用來關閉GDI+:
int CGDIPlusApp::ExitInstance()
Gdiplus::GdiplusShutdown(m_gdiplusToken);
return CWinApp::ExitInstance();
}
(5)、在需要繪圖的視窗或視圖類中添加GDI+的繪制代碼:
複制代碼
void CCGDIPlusView::OnDraw(CDC* pDC)
CCGDIPlusDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
using namespace Gdiplus;
Graphics graphics( pDC->m_hDC );
GraphicsPath path; // 構造一個路徑
path.AddEllipse(50, 50, 200, 100);
// 使用路徑構造一個畫刷
PathGradientBrush pthGrBrush(&path);
// 将路徑中心顔色設為藍色
pthGrBrush.SetCenterColor(Color(255, 0, 0, 255));
// 設定路徑周圍的顔色為藍芭,但alpha值為
Color colors[] = {Color(0, 0, 0, 255)};
INT count = 1;
pthGrBrush.SetSurroundColors(colors, &count);
graphics.FillRectangle(&pthGrBrush, 50, 50, 200, 100);
LinearGradientBrush linGrBrush(
Point(300, 50),
Point(500, 150),
Color(255, 255, 0, 0), // 紅色
Color(255, 0, 0, 255)); // 藍色
graphics.FillRectangle(&linGrBrush, 300, 50, 200, 100);
本文轉自Phinecos(洞庭散人)部落格園部落格,原文連結:http://www.cnblogs.com/phinecos/archive/2009/03/01/1400685.html,如需轉載請自行聯系原作者