VC6.0 預設不支援 GDI+,需手動部署 Windows SDK 提供的標頭檔與匯入庫。本文整理最精簡的整合流程,包含檔案部署、專案設定、啟動/關閉程式碼,讓舊專案也能使用 GDI+ 豐富的圖形功能。
Include 資料夾下的 GdiPlus.h、GdiPlusFlat.h、GdiPlusGpStubs.h 等複製到 VC6 安裝目錄 VC98\Include。Lib 資料夾下的 gdiplus.lib 複製到 VC98\Lib。在 StdAfx.h 最前端加入:
#include <comdef.h> // 初始化 COM
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#endif
#include "GdiPlus.h"
using namespace Gdiplus;
於 CWinApp 衍生類別(如 CMyApp)中宣告:
private:
GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;
BOOL CMyApp::InitInstance()
{
// ... 原有初始化 ...
// 啟動 GDI+
GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
return TRUE;
}
int CMyApp::ExitInstance()
{
// 關閉 GDI+
GdiplusShutdown(m_gdiplusToken);
return CWinApp::ExitInstance();
}
gdiplus.lib。VC98\Lib。void CMyView::OnDraw(CDC* pDC)
{
Graphics g(pDC->m_hDC);
SolidBrush brush(Color(255, 255, 0, 0));
g.FillRectangle(&brush, 10, 10, 100, 50);
FontFamily ff(L"Arial");
Font font(&ff, 24, FontStyleBold, UnitPixel);
g.DrawString(L"GDI+ Works!", -1, &font, PointF(10, 70), &brush);
}
ULONG_PTR 未定義:確保 #define ULONG_PTR unsigned long* 在 #include "GdiPlus.h" 之前。GdiplusStartup 未解析:檢查 gdiplus.lib 是否確實加入連結器模組清單。GdiplusStartup 回傳 InvalidParameter:確認 GdiplusStartupInput 結構未被竄改,且未重複呼叫啟動。#pragma comment(lib, "gdiplus.lib") // 直接在 StdAfx.h 指定連結庫
即可省去手動設定 Project Settings 步驟。