且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-09-07 10:24:57

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

代码:

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 }
WINDOWS窗口创建及消息处理代码

 

截图:

窗口没出来。。

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