新闻详情

新闻详情

首页 / 资讯中心 / 详情

Win32 GUI画图极简框架

发布时间:2026/9/26 7:33:03来源:尧图网络
Win32 GUI画图极简框架
极简测试//编译运行生成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; }
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

Baserow 完整指南:如何 3 条命令搭出团队能用的无代码数据库 2026/9/26 8:16:04

Baserow 完整指南:如何 3 条命令搭出团队能用的无代码数据库

Baserow 完整指南:如何 3 条命令搭出团队能用的无代码数据库 【免费下载链接】baserow Build databases, automations, apps & agents with AI — no code. Open source platform available on cloud and self-hosted. GDPR, HIPAA, SOC 2 compliant. Best Airt…

阅读更多 →
基于DeepSeek与Dify搭建智能拍照解题应用 2026/9/26 8:16:04

基于DeepSeek与Dify搭建智能拍照解题应用

1. 这个项目到底在做什么 先说说最原始的场景。朋友把孩子的一道几何题拍成照片发给我,说“帮看看怎么解”。我盯着图片里的辅助线看了半天,最后还是得先把题目文字敲进搜索引擎。这个动作重复几次之后,我就琢磨:能不能让 DeepSee…

阅读更多 →
AI-Infra-Guard:技能扫描与漏报复盘实战 2026/9/26 8:16:04

AI-Infra-Guard:技能扫描与漏报复盘实战

周五下午三点,我在线上例会开到一半,群里突然弹出一张截图:data-service 技能三分钟前挂掉,调用端超时率直接飘红。这已经不是第一次了,我维护的内部AI技能平台已经塞了几十个技能组件——有接业务库的,有调…

阅读更多 →
企业级AI Agent落地实战:从Demo到生产全路径 2026/9/26 8:16:04

企业级AI Agent落地实战:从Demo到生产全路径

1. 这不是“又一个AI课程”,而是企业级Agent落地的完整作战地图你有没有遇到过这样的场景:技术团队兴奋地拉出一版基于LangChain的Agent Demo,演示时能自动查天气、写周报、调API,老板点头说“不错”,但三个月后项目悄…

阅读更多 →
Multipass 中的 Flutter 管理与 GUI 客户端维护指南 2026/9/26 8:15:58

Multipass 中的 Flutter 管理与 GUI 客户端维护指南

虚拟化开发工具云原生 【免费下载链接】multipass Multipass orchestrates virtual Ubuntu instances 项目地址: https://gitcode.com/gh_mirrors/mu/multipass 点击查看 免费下载 Multipass 的图形化客户端(GUI)基于 Flutter 构建&#xff…

阅读更多 →
Delta 金手指实战指南:3 步启用作弊码,不生效时照着排查 2026/9/26 8:15:58

Delta 金手指实战指南:3 步启用作弊码,不生效时照着排查

Delta 金手指实战指南:3 步启用作弊码,不生效时照着排查 【免费下载链接】Delta Delta is an all-in-one classic video game emulator for non-jailbroken iOS devices. 项目地址: https://gitcode.com/GitHub_Trending/delt/Delta Delta 是一款…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉