In Windows, each thread can have a message queue. Generally, the UI thread has its own message queue by default. The work thread needs to call peekmessage to create its own message queue. A message is a data structure defined below: typedef struct tagmsg {hwnd; uint message; wparam; lparam; DWORD time; point pt; # ifdef _ Mac DWORD lprivate; # endif} MSG each message contains the handle of the window to receive the message, the Message ID, two parameters, and other information. The message loop in the SDK is usually written as follows: While (getmessage (& MSG, null, 0, 0) {If (! Translateaccelerator (MSG. hwnd, hacceltable, & MSG) {translatemessage (& MSG); dispatchmessage (& MSG) ;}} unless the getmessage function obtains the wm_quit message from the message queue, otherwise, the loop will continue forever. Inside the loop, the accelerator key and Keyboard Message are processed first, and then the message is sent to the specified window. Because the message itself contains the window handle, the destination does not need to be explicitly specified. When a message is received in the target window, the window procedure function is executed and processed based on different messages. Because the window procedure function and the window class name have a corresponding relationship when registering the window. When creating a window, we use the window class name again. Therefore, we can infer that the window handle and Window Process letter obtained after the window is created are mapped. The following is a typical window procedure function: lresult callback wndproc (hwnd, uint message, wparam, lparam) {int wmid, wmevent; paintstruct pS; HDC; switch (Message) {Case wm_command: wmid = loword (wparam); wmevent = hiword (wparam); // parse the menu selections: Switch (wmid) {Case idm_about: dialogbox (hinst, makeintresource (idd_aboutbox), hwnd, about); break; Case idm_exit: destroywindow (hwnd); break; default: Return defwindowproc (hwnd, message, wparam, lparam) ;}break; Case wm_paint: HDC = beginpaint (hwnd, & PS); // todo: add any drawing code here... endpaint (hwnd, & PS); break; Case wm_destroy: postquitmessage (0); break; default: Return defwindowproc (hwnd, message, wparam, lparam);} return 0 ;}