WINDOWS窗口创建及消息处理代码

简介:

以前看过很多次,现在默默的一个一个打出来。。

代码:

复制代码
 1 /* header file */
 2 #include <Windows.h>
 3 
 4 /* global var */
 5 HINSTANCE hinst;
 6 
 7 /* function declaire*/
 8 int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
 9 LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
10 
11 /* function display a window */
12 int WINAPI WinMain(HINSTANCE hInstance,
13     HINSTANCE hPrevInstance,
14     LPSTR lpCmdLine,
15     int nCmdShow)
16 {
17     WNDCLASSEX wcx;        //windows class
18     HWND hwnd;            //windows handle
19     MSG msg;            //message
20     BOOL fGotMessage;    //if get message succeed
21     hinst = hInstance;    //app object handle , it's save global var
22     //fill window form data struct
23     wcx.cbSize = sizeof(wcx);    //size of struct
24     wcx.style = CS_HREDRAW | CS_VREDRAW;    //style:redraw GUI when change size
25     wcx.lpfnWndProc = MainWndProc;        //function of deal windows Message
26     wcx.cbClsExtra = 0;        //don't use class mem
27     wcx.cbWndExtra = 0;        //don't use windows form mem
28     wcx.hInstance = hInstance;        //belong to APP instance handle
29     wcx.hIcon = LoadIcon(NULL,IDI_APPLICATION);        //ICON,default
30     wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //background:white
31     wcx.lpszMenuName = NULL;        //menu:null
32     wcx.lpszClassName = "MainWClass";        //windows form class name
33     wcx.hIconSm = (HICON)LoadImage(hInstance,        //little icon
34         MAKEINTRESOURCE(5),
35         IMAGE_ICON,
36         GetSystemMetrics(SM_CXSMICON),
37         GetSystemMetrics(SM_CYSMICON),
38         LR_DEFAULTCOLOR);
39 
40     //register windows form class
41     if(!RegisterClassEx(&wcx))
42     {
43         return 1;
44     }
45 
46     //create windows form
47     hwnd = CreateWindow(
48         "MainWClass",        //windows form name
49         "CH 2-3",        //windows form title
50         WS_OVERLAPPEDWINDOW,    //windows form style
51         CW_USEDEFAULT,        //x:default
52         CW_USEDEFAULT,        //Y:default
53         CW_USEDEFAULT,        //width:default
54         CW_USEDEFAULT,        //heigh:default
55         (HWND) NULL,        //parent class:none
56         (HMENU) NULL,        //menu:make window form menu
57         hInstance,        //APP programe instance handle
58         (LPVOID) NULL);        //data when create window form:none
59 
60     if(!hwnd)
61     {
62         return 1;
63     }
64 
65     //display window form
66     ShowWindow(hwnd, nCmdShow);
67     UpdateWindow(hwnd);
68 
69     //get message again and again
70     while(
71         (fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0
72         && fGotMessage != -1)
73     {
74         TranslateMessage(&msg);
75         DispatchMessage(&msg);
76     }
77     return msg.wParam;
78 }
79 
80 /* MainWndProc 
81 function of deal with windows form message*/
82 LRESULT CALLBACK MainWndProc(HWND hwnd,
83     UINT uMsg,
84     WPARAM wParam,
85     LPARAM lParam
86     )
87 {
88     switch (uMsg)
89     {
90     case WM_DESTROY:
91         ExitThread(0);
92         return 0;
93     default:
94         return DefWindowProc(hwnd, uMsg, wParam, lParam);
95     }
96 }
复制代码

 

截图:

窗口没出来。。

目录
相关文章
|
安全 生物认证 网络安全
windows10无法设置默认保存位置怎么办?显示错误代码0x80070002怎么解决?
Win10系统下载文件时,默认会保存在特定位置,但用户可自行修改。若更改后仍无效,可通过删除目标磁盘中的特定文件夹、修改注册表权限、“干净启动”排除干扰软件或使用第三方修复工具等方式解决此问题。
2050 0
|
缓存 开发工具 git
windows系统通过git上传代码
windows系统通过git上传代码
267 1
|
Windows
office出现0xc0000142错误?windows错误代码为0xc0000142?
office出现0xc0000142错误?windows错误代码为0xc0000142?
705 0
Windows7电脑启动时提示文件winload.exe无法验证其数字签名,错误代码0xc0000428的解决方法
Windows7电脑启动时提示文件winload.exe无法验证其数字签名,错误代码0xc0000428的解决方法
|
Linux 开发者 Python
从Windows到Linux,Python系统调用如何让代码飞翔🚀
【9月更文挑战第10天】在编程领域,跨越不同操作系统的障碍是常见挑战。Python凭借其“编写一次,到处运行”的理念,显著简化了这一过程。通过os、subprocess、shutil等标准库模块,Python提供了统一的接口,自动处理底层差异,使代码在Windows和Linux上无缝运行。例如,`open`函数在不同系统中以相同方式操作文件,而`subprocess`模块则能一致地执行系统命令。此外,第三方库如psutil进一步增强了跨平台能力,使开发者能够轻松编写高效且易维护的代码。借助Python的强大系统调用功能,跨平台编程变得简单高效。
599 1
|
Java Windows
【Azure Developer】Windows中通过pslist命令查看到Java进程和线程信息,但为什么和代码中打印出来的进程号不一致呢?
【Azure Developer】Windows中通过pslist命令查看到Java进程和线程信息,但为什么和代码中打印出来的进程号不一致呢?
485 1
|
安全 Windows
Microsoft Windows远程桌面服务远程执行代码漏洞(CVE-2019-0708)
Microsoft Windows远程桌面服务远程执行代码漏洞(CVE-2019-0708)
1327 2
|
Python Windows
在 Windows 平台下打包 Python 多进程代码为 exe 文件的问题及解决方案
在使用 Python 进行多进程编程时,在 Windows 平台下可能会出现将代码打包为 exe 文件后无法正常运行的问题。这个问题主要是由于在 Windows 下创建新的进程需要复制父进程的内存空间,而 Python 多进程机制需要先完成父进程的初始化阶段后才能启动子进程,所以在这个过程中可能会出现错误。此外,由于没有显式导入 Python 解释器,也会导致 Python 解释器无法正常工作。为了解决这个问题,我们可以使用函数。
1267 5
|
API Windows
Windows之窗口原理
这篇文章主要介绍了Windows窗口原理和如何使用Windows API创建和管理窗口。
481 0
|
Windows
windows系统vbs脚本 恶搞关不掉的窗口 以及解决办法
windows系统vbs脚本 恶搞关不掉的窗口 以及解决办法
823 2