1 在項(xiàng)目屬性中添加GDI+靜態(tài)庫(kù)鏈接
打開(kāi)項(xiàng)目,選擇【項(xiàng)目】——》【XXX屬性】(XXX為當(dāng)前項(xiàng)目名稱),打開(kāi)“項(xiàng)目屬性”對(duì)話框,展開(kāi)“屬性配置”,選擇“連接器”下的“輸入”,然后在”依賴附加項(xiàng)“中添加“gdiplus.lib”
2 添加必要的代碼
(1)打開(kāi)“解決方案資源管理器”,打開(kāi)stdafs。h文件,添加代碼如下:
#include "gdiplus.h"
using namespace Gdiplus;
(2)打開(kāi)類視圖 選中相應(yīng)的應(yīng)用程序類CXXXApp 為其添加兩個(gè)成員變量
GdiplusStartupInput m_GdiplusStartupInput;
ULONG_PTR m_GdiplusToken;
(3)在CXXXApp類的InitInstance函數(shù)中添加
//GDI+圖像庫(kù)初始化
GdiplusStartup(&m_GdiplusToken,&m_GdiplusStartupInput,NULL);
(4)重寫CXXXApp類的ExitInstance函數(shù)
int CBMPProApp::ExitInstance(void)
{
//關(guān)閉gdi+圖像庫(kù)
GdiplusShutdown(m_GdiplusToken);
return CWinApp::ExitInstance();
}
void CGDIplusDemoView::OnDraw(CDC* )
{
CGDIplusDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此處為本機(jī)數(shù)據(jù)添加繪制代碼
Graphics graphics(this->GetDC()->m_hDC);
Pen pen(Color(255,0,0,255));
SolidBrush brush(Color(255,0,0,255));
FontFamily fontFamily(L"宋體");
Font font(&fontFamily,24,FontStyleRegular,UnitPixel);
CRect rect;
this->GetClientRect(&rect);
PointF pointF(rect.right/2,rect.bottom/2);
graphics.DrawString(L"GDI+程序",-1,&font,pointF,&brush);
}
結(jié)果為生成一個(gè)窗口:顯示字體“GDI+程序”