
极简测试//编译运行生成task.json后应在 // ${fileDirname}\\${fileBasenameNoExtension}.exe之后添加以下内容 // , -mwindows #include windows.h LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc BeginPaint(hwnd, ps); HBRUSH red CreateSolidBrush(RGB(255,0,0)); RECT sq {50, 50, 150, 150}; // 局部变量安全取地址 FillRect(hdc, sq, red); DeleteObject(red); HBRUSH blue CreateSolidBrush(RGB(0,0,255)); SelectObject(hdc, blue); Ellipse(hdc, 200, 50, 300, 150); DeleteObject(blue); EndPaint(hwnd, ps); return 0; } case WM_DESTROY: PostQuitMessage(0); return 0; default: return DefWindowProcW(hwnd, msg, wParam, lParam); } } int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nShow) { WNDCLASSW wc {0}; wc.lpfnWndProc WndProc; wc.hInstance hInst; wc.lpszClassName LDraw; wc.hbrBackground (HBRUSH)(COLOR_WINDOW1); RegisterClassW(wc); HWND hwnd CreateWindowExW(0, LDraw, L绘图, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 200, NULL, NULL, hInst, NULL); ShowWindow(hwnd, nShow); UpdateWindow(hwnd); MSG msg; while (GetMessageW(msg, NULL, 0, 0)) { TranslateMessage(msg); DispatchMessageW(msg); } return msg.wParam; }面向过程写法//编译运行生成task.json后应检查是否在 // ${fileDirname}\\${fileBasenameNoExtension}.exe之后添加了以下内容 // ,-lgdi32, -mwindows #include windows.h #include cstdlib // 绘图函数直接拿到窗口句柄绘图 void DrawPoint(HWND hwnd, int x, int y, COLORREF color) { HDC hdc GetDC(hwnd); SetPixel(hdc, x, y, color); ReleaseDC(hwnd, hdc); } void DrawLine(HWND hwnd, int x1, int y1, int x2, int y2, COLORREF color) { HDC hdc GetDC(hwnd); HPEN pen CreatePen(PS_SOLID, 1, color); HPEN oldPen (HPEN)SelectObject(hdc, pen); MoveToEx(hdc, x1, y1, nullptr); LineTo(hdc, x2, y2); SelectObject(hdc, oldPen); DeleteObject(pen); ReleaseDC(hwnd, hdc); } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { return msg WM_DESTROY ? (PostQuitMessage(0), 0) : DefWindowProcA(hwnd, msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) { //1.初始化程序 const char* className SimpleWin; WNDCLASSEXA wc{}; wc.cbSize sizeof(wc); wc.lpfnWndProc WndProc; wc.hInstance hInstance; wc.lpszClassName className; RegisterClassExA(wc); HWND hwnd CreateWindowExA(0, className, Draw Window, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, nullptr, nullptr, hInstance, nullptr ); ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd); // 2. 在主函数直接调用绘图函数 srand(12); for(int i 0; i 10000; i) { int x rand() % 800; int y rand() % 600; COLORREF color RGB(rand()%256, rand()%256, rand()%256); DrawPoint(hwnd, x, y, color); } DrawLine(hwnd, 200, 200, 200, 400, RGB(0, 0, 0)); DrawLine(hwnd, 200, 400, 400, 300, RGB(255, 0, 0)); DrawLine(hwnd, 400, 300, 200, 200, RGB(0, 128, 255)); // 3. 等待5秒 DWORD startTick GetTickCount(); while (GetTickCount() - startTick 5000) { MSG msg{}; if (PeekMessageA(msg, nullptr, 0, 0, PM_REMOVE)) DispatchMessageA(msg); } DestroyWindow(hwnd); // 4. 关闭窗口 return 0; }面向对象版本//编译运行生成task.json后应检查是否在 // ${fileDirname}\\${fileBasenameNoExtension}.exe之后添加了以下内容 // ,-lgdi32, -mwindows #include windows.h #include cstdlib #include string class Paint { private: HWND hwnd; DWORD startTick; int waitTime; // 保存等待时长毫秒 std::string className; // 静态窗口过程处理基本消息 static LRESULT CALLBACK WndProcStatic(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { if (msg WM_DESTROY) { PostQuitMessage(0); return 0; } return DefWindowProcA(hwnd, msg, wParam, lParam); } // 内部辅助注册窗口类 void RegisterWindowClass(HINSTANCE hInstance) { WNDCLASSEXA wc{}; wc.cbSize sizeof(wc); wc.lpfnWndProc WndProcStatic; wc.hInstance hInstance; wc.lpszClassName className.c_str(); wc.hbrBackground (HBRUSH)(COLOR_WINDOW 1); // 添加背景刷避免闪烁 RegisterClassExA(wc); } public: // 构造函数初始化窗口 Paint(HINSTANCE hInstance, const char* titleWindow, int width800, int height600) : hwnd(nullptr), startTick(0), waitTime(0), className(OOPPaintClass) { RegisterWindowClass(hInstance); hwnd CreateWindowExA( 0, className.c_str(), title, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, width, height, nullptr, nullptr, hInstance, nullptr ); if (hwnd) { ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd); } } // 成员函数DrawPoint void DrawPoint(int x, int y, COLORREF color) { if (!hwnd) return; HDC hdc GetDC(hwnd); SetPixel(hdc, x, y, color); ReleaseDC(hwnd, hdc); } // 成员函数DrawLine void DrawLine(int x1, int y1, int x2, int y2, COLORREF color) { if (!hwnd) return; HDC hdc GetDC(hwnd); HPEN pen CreatePen(PS_SOLID, 1, color); HPEN oldPen (HPEN)SelectObject(hdc, pen); MoveToEx(hdc, x1, y1, nullptr); LineTo(hdc, x2, y2); SelectObject(hdc, oldPen); DeleteObject(pen); ReleaseDC(hwnd, hdc); } // 成员函数Wait // 接受外部传参的延时毫秒数保存到 waitTime并记录开始时间 void Wait(int ms) { waitTime ms; startTick GetTickCount(); } // 析构函数等待指定时间后关闭窗口 ~Paint() { if (waitTime 0) { // 延时循环同时处理消息以保持窗口响应 while (GetTickCount() - startTick (DWORD)waitTime) { MSG msg{}; // 使用 PM_REMOVE 取出消息并分发防止界面无响应 if (PeekMessageA(msg, nullptr, 0, 0, PM_REMOVE)) { DispatchMessageA(msg); } } } if (hwnd) { DestroyWindow(hwnd); hwnd nullptr; } } }; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) { Paint obj1(hInstance, Object 1: Points); srand(12); for(int i 0; i 10000; i) { int x rand() % 800; int y rand() % 600; COLORREF color RGB(rand()%256, rand()%256, rand()%256); obj1.DrawPoint(x, y, color); } Paint obj2(hInstance, Object 2: Lines); obj2.DrawLine(200, 200, 200, 400, RGB(0, 0, 0)); obj2.DrawLine(200, 400, 400, 300, RGB(255, 0, 0)); obj2.DrawLine(400, 300, 200, 200, RGB(0, 128, 255)); obj1.Wait(3000); obj2.Wait(5000); return 0; }