|
发表于 2025-8-19 05:42:56
|
显示全部楼层
作为一名老师,对于您提到的“C++驱动GDI绘制源码”的需求,我理解您可能希望获取一份详细的C++代码示例,用于展示如何在Windows环境下利用GDI(Graphics Device Interface)进行图形绘制。以下是一份简化的代码示例,供您参考学习:
- cpp
- #include
- #include
- using namespace Gdiplus;
- // 窗口过程函数
- LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
- switch (msg) {
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- default:
- return DefWindowProc(hwnd, msg, wParam, lParam);
- }
- }
- int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
- _In_opt_ HINSTANCE hPrevInstance,
- _In_ LPWSTR lpCmdLine,
- _In_ int nCmdShow) {
- WNDCLASSEX wc = {};
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = WindowProc;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wc.lpszClassName = L"GDI+Drawing";
- wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- if (!RegisterClassEx(&wc)) {
- return -1;
- }
- HWND hwnd = CreateWindowEx(0, L"GDI+Drawing", L"C++ GDI+", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, NULL, NULL, hInstance, NULL);
- if (hwnd == NULL) {
- return -1;
- }
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
- MSG msg;
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int)msg.wParam;
- }
- void DrawShapes() {
- Graphics graphics(NULL);
- RectF rect(10.0f, 10.0f, 200.0f, 150.0f);
- Pen pen(Color(255, 0, 0, 255), 2.0f); // 红色,不透明
- graphics.DrawEllipse(&pen, rect);
- pen.SetColor(Color(0, 255, 0, 255)); // 绿色,不透明
- graphics.DrawRectangle(&pen, rect);
- pen.SetColor(Color(0, 0, 255, 255)); // 蓝色,不透明
- graphics.DrawRoundRect(&pen, rect, 20.0f, 20.0f);
- pen.SetColor(Color(255, 0, 255, 255)); // 紫色,不透明
- graphics.DrawLine(&pen, PointF(10.0f, 10.0f), PointF(200.0f, 200.0f));
- }
复制代码
[本文内容由人工智能科大讯飞 - 星火认知辅助生成,仅供参考] |
|