標準 Win32 窗口程序 + GDI+ 初始化/關閉,在 WM_PAINT 中使用 Graphics 繪製矩形、線條、圖片。
ULONG_PTR gdiplusToken; GdiplusStartupInput gdiplusStartupInput; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); // ... 窗口循環 ... GdiplusShutdown(gdiplusToken);
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_HREDRAW|CS_VREDRAW, WndProc,
0, 0, hInstance, LoadIcon(NULL,IDI_APPLICATION),
LoadCursor(NULL,IDC_ARROW), (HBRUSH)(COLOR_WINDOW+1),
NULL, L"GDIPlusWindow", NULL };
RegisterClassEx(&wc);
HWND hwnd = CreateWindowEx(0, L"GDIPlusWindow", L"GDI+ Demo",
WS_OVERLAPPEDWINDOW, 100, 100, 600, 400, NULL, NULL, hInstance, NULL);
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
Graphics graphics(hdc); // 包裝 HDC
// 紅筆畫線
Pen pen(Color(255, 255, 0, 0)); // ARGB
graphics.DrawLine(&pen, 0, 0, 500, 500);
// 實心刷填矩形
SolidBrush brush(Color(255, 0, 255, 0)); // 綠色
graphics.FillRectangle(&brush, 100, 100, 200, 150);
// 繪製圖片
Image img(L"test.png");
graphics.DrawImage(&img, 300, 100);
EndPaint(hwnd, &ps);
break;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT rc; GetClientRect(hwnd, &rc);
int w = rc.right - rc.left, h = rc.bottom - rc.top;
// 創建兼容 DC 和位圖
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP memBmp = CreateCompatibleBitmap(hdc, w, h);
HBITMAP oldBmp = (HBITMAP)SelectObject(memDC, memBmp);
// 在內存 DC 繪圖
Graphics memG(memDC);
memG.Clear(Color::White);
// ... 繪圖操作 ...
// 一次性複製到屏幕
BitBlt(hdc, 0, 0, w, h, memDC, 0, 0, SRCCOPY);
// 清理
SelectObject(memDC, oldBmp);
DeleteObject(memBmp);
DeleteDC(memDC);
EndPaint(hwnd, &ps);
break;
}
case WM_NCHITTEST: {
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
ScreenToClient(hwnd, &pt);
LRESULT hit = DefWindowProc(hwnd, message, wParam, lParam);
const wchar_t* tips[] = {
[HTCLIENT]=L"客戶區", [HTCAPTION]=L"標題欄",
[HTCLOSE]=L"關閉按鈕", [HTMAXBUTTON]=L"最大化",
[HTMINBUTTON]=L"最小化", [HTLEFT]=L"左邊框",
[HTRIGHT]=L"右邊框", [HTTOP]=L"上邊框",
[HTBOTTOM]=L"下邊框", [HTTOPLEFT]=L"左上角",
[HTTOPRIGHT]=L"右上角", [HTBOTTOMLEFT]=L"左下角",
[HTBOTTOMRIGHT]=L"右下角", [HTSYSMENU]=L"系統菜單"
};
if (hit >= HTCLIENT && hit <= HTBOTTOMRIGHT && tips[hit])
SetWindowText(hwnd, tips[hit]);
return hit;
}
gdiplus.lib:專案屬性 → 鏈接器 → 輸入 → 附加依賴項gdiplus.h,命名空間 using namespace Gdiplus;GdiplusStartupgraphics.SetTextRenderingHint(TextRenderingHintAntiAliasGridFit)dpiAware 或 SetProcessDpiAwareness