[C++] 纯文本查看 复制代码
#include <Windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
// 全局变量
HWND g_hWnd;
ULONG_PTR g_gdiplusToken;
Bitmap* g_pOffscreenBitmap = nullptr;
Graphics* g_pOffscreenGraphics = nullptr;
// 按钮结构
struct Button {
Rect rect;
bool hovered;
bool clicked;
WCHAR text[32];
};
// 初始化GDI+
void InitGDIPlus()
{
GdiplusStartupInput input;
GdiplusStartup(&g_gdiplusToken, &input, nullptr);
}
// 创建离屏位图
void CreateOffscreenBitmap(int width, int height)
{
if (g_pOffscreenGraphics) delete g_pOffscreenGraphics;
if (g_pOffscreenBitmap) delete g_pOffscreenBitmap;
g_pOffscreenBitmap = new Bitmap(width, height);
g_pOffscreenGraphics = Graphics::FromImage(g_pOffscreenBitmap);
g_pOffscreenGraphics->SetSmoothingMode(SmoothingModeAntiAlias);
}
// 绘制图形到离屏位图
void DrawToOffscreen()
{
if (!g_pOffscreenGraphics) return;
g_pOffscreenGraphics->Clear(Color(255, 255, 255));
// 绘制其他图形
Pen redPen(Color(255, 0, 0), 3);
Pen bluePen(Color(0, 0, 255), 2);
// 直线
g_pOffscreenGraphics->DrawLine(&redPen, 200, 50, 300, 100);
// 圆弧(椭圆的一部分)
g_pOffscreenGraphics->DrawArc(&bluePen, 200, 100, 100, 50, 45, 180);
// 圆环(空心圆)
g_pOffscreenGraphics->DrawEllipse(&redPen, 350, 50, 80, 80);
// 文本
SolidBrush brush(Color(0, 128, 0));
Font textFont(L"宋体", 16);
g_pOffscreenGraphics->DrawString(L"持久化绘图示例", -1, &textFont, PointF(200, 200), &brush);
}
// 窗口过程
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
InitGDIPlus();
break;
case WM_SIZE:
{
int width = LOWORD(lParam);
int height = HIWORD(lParam);
CreateOffscreenBitmap(width, height);
DrawToOffscreen();
InvalidateRect(hWnd, nullptr, FALSE);
break;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
if (g_pOffscreenBitmap)
{
Graphics graphics(hdc);
graphics.DrawImage(g_pOffscreenBitmap, 0, 0);
}
EndPaint(hWnd, &ps);
break;
}
case WM_ERASEBKGND:
return 1; // 阻止背景擦除
case WM_DESTROY:
if (g_pOffscreenGraphics) delete g_pOffscreenGraphics;
if (g_pOffscreenBitmap) delete g_pOffscreenBitmap;
GdiplusShutdown(g_gdiplusToken);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
// 注册窗口类
void RegisterWindowClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.lpszClassName = L"GDIPlusWindow";
RegisterClassExW(&wcex);
}
// 创建主窗口
HWND CreateMainWindow(HINSTANCE hInstance)
{
return CreateWindowW(L"GDIPlusWindow", L"GDI+ 持久化绘图示例",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 800, 600, nullptr,
nullptr, hInstance, nullptr);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
RegisterWindowClass(hInstance);
g_hWnd = CreateMainWindow(hInstance);
ShowWindow(g_hWnd, nCmdShow);
UpdateWindow(g_hWnd);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}