Entering the windows programming world ----- message processing functions (1)
Win32 message mechanism process DRIVER: The program is executed in a predefined order. Each step is executed and the next step is executed in a predetermined order until the program ends. Event-driven: The execution sequence of the program is unordered. The Code executed at a certain time point is notified by the outside world. We cannot determine the program execution sequence. Therefore, code execution is unordered. Win32 basic message WM_DESTROY:
When the window is destroyed, you can exit or handle the message WM_CREATE:
The Window Creation message is the first message received by the window processing function after the window is created.
You can initialize or wear a sub-window in the message.
WPARAM wParam-not used
LPARAM lParam-CREATESTRUCT pointer
WM_SIZE:
This message is received when the window size changes.
You can adjust the window layout in this message.
WM_SYSCOMMAND:
System Command message. You will receive the message when you click the system menu and button.
You can prompt the user to save the data in this message.
WM_PAINT:
Drawing message
Keyboard Message:
Mouse message
WM_TIME: gets and sends a timer message.
Get GetMessage/PeekMessage
GetMessage gets the message, blocking function
PeekMessage: gets a message. It is a non-blocking function.
Send SendMessage/PostMessage
SendMessage sends a message and returns it only after the message processing is complete.
PostMessage is returned immediately after a message is sent, regardless of the message processing result.
 
LRESULT SendMessage/PostMessage (HWND hWnd, // process the message window UINT Msg, // Message id wparam wParam, // Message Parameter LPARAM lParam); // Message Parameter
 
3. Message composition and Classification
3.1 message composition
Window handle/Message ID/message parameters (WPARAM. LPARAM)
3.2 message Classification
3.2.1 system messages-messages defined and used by the System
Example: WM_CREATE/WM_SIZE
Message ID range: 0-0x03FF (WM_USER-1)
3.2.2 User-defined message-messages that can be defined and used by applications, WM_USER (0x0400)
From WM_USER ID to 0x7FFF, messages can be defined by the user.
3.2.3 other message range
WM_APP (0x8000)-0 xBFFF: Message ID of the application access window
0xC000-0xFFFF: The application accesses the message and uses the string to register the system to generate the corresponding message ID.
3.2.4 use of user-defined messages
1) custom message ID:
 
 #define   WM_FIRSTMSG  (WM_USER+1)
 
2) Respond to messages in window processing functions
 
Switch (nMsg) {case WM_FIRSTMSG: // processing function break ;} 
3) SendMessage/PostMessage
 
    SendMessage( hWnd, WM_FIRSTMSG, 0, 0 );
 
4. Message Queue
4.1 message queue-memory space used to store messages. in the queue, messages are first-in-first-out.
4.2 Message Queue Classification
4.2.1 system message queue-A Message Queue maintained by the system.
4.2.2 application Message Queue (thread message-to-column)-A Message Queue owned by each thread.
5. Message and Message Queue
5.1 Based on the relationship between messages and message queues, messages are divided into two types:
Queue message-messages that can be stored in a message queue.
Non-queue messages-do not enter the message queue when sending messages.
5.2 queue messages
It is first stored in the message queue, and then retrieved by GetMessage/PeekMessage for processing.
For example: Mouse message/Keyboard Message/WM_PAINT/WM_QUIT/M_TIMER message
5.3 Non-queue messages
The message is sent directly to the specified window to find the processing function of the window and return the processing result.
 
6. Message acquisition
6.1 message loop
6.1.1 GetMesssage obtains the message from the queue and determines whether the message is a WM_QUIT message. If a WM_QUIT message is found, the message loop ends. Otherwise, proceed to the next step.
6.1.2 TranslateMessage
6.1.3 DispatchMessage: Find the processing function of the window in which the message is sent and process the message. After processing, return 6.1.1.
6.2 GetMesssage and PeekMessage
6.2.1 obtain a message from the thread message queue. If a message is found, the system returns the message and processes the message. If no message is found, execute 6.2.2.
6.2.2 query the system message queue. query the system message queue. If a message is found, obtain the message and return it for message processing. If no message is found, execute 6.2.3.
6.2.3 check the range to be re-drawn in the window. If a re-drawn range exists, the WM_PAINT message is generated. Then, the message is processed. If not found, run 6.2.4.
6.2.4 check the WM_TIMER timer message. If a timer already exists, the WM_TIMER message is generated for message processing. If not found, run 6.2.5.
6.2.5 perform memory management.
6.2.6 different processing results based on different functions:
GetMesssage-blocking, waiting for the next message
PeekMessage-give out control and give it to the subsequent code for execution.
7. Send messages
7.1 messages can be sent in two ways.
Send message-Send the message directly to the specified window and wait for the result.
Post messages-messages sent to the message queue are immediately returned and processed cyclically by messages.
7.2 PostMessage and SendMessage
PostMessage generates a queue message. Because it does not wait for the message processing result after sending, you cannot determine whether the message is successfully processed.
If SendMessage generates a non-queue message, you can determine whether the message is successful.
See the following code example: 
/* File: message. cpp * Auth: sjin * Date: 20140519 * Mail: 413977243@qq.com */# include <iostream> # include <Windows. h> # include <stdio. h> HINSTANCE g_hInst = NULL; HWND g_button = NULL; lresult callback WndProc (HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ); /* Registration window */BOOL RegisterWnd (LPSTR pszClassName) {WNDCLASSEX wce = {0}; wce. cbClsExtra = 0; wce. cbSize = sizeof (wce); wce. cbWndExtra = 0; wce. hbrBa Ckground = HBRUSH (COLOR_BTNFACE + 1); wce. hCursor = NULL; wce. hIcon = NULL; wce. hIconSm = NULL; wce. hInstance = g_hInst; wce. lpfnWndProc = WndProc; wce. lpszClassName = pszClassName; wce. lpszMenuName = NULL; wce. style = CS_HREDRAW | CS_VREDRAW; ATOM nAtom = RegisterClassEx (& wce); if (0 = nAtom) return FALSE; return TRUE ;} /* Create window */HWND CreateWnd (LPSTR pszClassName) {HWND hWnd = create1_wex (0, pszClass Name, "MyWnd", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, NULL, NULL, g_hInst, NULL); return hWnd ;} /* display window */void DisplayWnd (HWND hWnd) {ShowWindow (hWnd, SW_SHOW); UpdateWindow (hWnd);} void wm_create (HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) {LPCREATESTRUCT pCreateStruct = LPCREATESTRUCT (lParam);/* click "OK" to display the name of the print window. * // MessageBox (NULL, pCreateStruct-> LpszName, "WM_CREATE", MB_ OK);/* Create a subwindow */g_button = createmediawex (0, "BUTTON", "BUTTON", WS_CHILD | WS_VISIBLE, 30, 20,100, 50, hWnd, NULL, g_hInst, NULL);} void wm_size (HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) {INT nWidth = LOWORD (lParam ); INT nHeight = HIWORD (lParam); CHAR szText [256] = {'\ 0'}; sprintf (szText, "W: % d; H: % d", nWidth, nHeight); // MessageBox (NULL, szText, "WM_SIZE", MB_ OK); if (NULL! = G_button) {int nX = (nWidth-100)/2; int nY = (nHeight-100)/2; MoveWindow (g_button, nX, nY, 100,100, TRUE) ;}}/* execute the system command function to process */BOOL wm_syscommand (HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) {switch (wParam) {case SC _CLOSE: if (IDOK = MessageBox (NULL, "Whether to save files to disk", "prompt", MB_OKCANCEL | MB_ICONWARNING) {return TRUE;} else {return FALSE;} break; default: break;} return FALSE;}/* message processing function */lresult callback WndPr Oc (HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) {switch (nMsg) {/* Win 32 basic message WM_DESTROY: message when the window is destroyed, exit or follow-up WM_CREATE: Creates a message in the window. After the window is created, the first message received by the window processing function can be contained in the message, initialize or wear the Child Window WPARAM wParam-do not use LPARAM lParam-CREATESTRUCT pointer WM_SIZE: this message is received when the window size changes. You can adjust the window layout WM_SYSCOMMAND: system command message in this message. When you click the system menu and button, you will receive a message prompting you to save data and other WM_PAINT: drawing message Keyboard Message: Mouse message WM_TIME: timer message */case WM_DESTROY: // message when the window is destroyed/* Send WM_QUIT message to the window * // SendMessage (hWnd, WM_QUIT, 0, 0);/* send the message and wait until the message processing ends before returning */PostMessage (hWnd, WM_QUIT, 0, 0);/* return immediately after sending the message, do not care about the message processing result * // PostQuitMessage (0); return 0; case WM_CREATE:/* Create window */wm_create (hWnd, nMsg, wParam, lParam); break; case WM_SIZE:/* drag the window */wm_size (hWn D, nMsg, wParam, lParam); break; case WM_SYSCOMMAND:/* run the system command */if (! Wm_syscommand (hWnd, nMsg, wParam, lParam) {return 0;} break;} return DefWindowProc (hWnd, nMsg, wParam, lParam );} /* Message loop */void message () {/* MSG struct parameter description typedef struct tagMSG {// msg HWND hwnd; // Message window handle UINT message; // WPARAM wParam; // Message Parameter LPARAM lParam; // Message Parameter DWORD time; // message time POINT pt; // when a message is generated, cursor Position} MSG; */MSG msg = {0};/* BOOL GetMessage (LPMSG lpMsg, // store the acquired message data. The system fills in the HWND hWnd parameter about the message, and // obtains the message. Window handle, which can accept the specified window message UINT wMsgFilterMin, // The starting message UINT wMsgFilterMax for message filtering // The End message for message filtering); return: TRUE: the message is obtained successfully; FALSE: when the WM_QUIT message is obtained. You can use PostQuitMessage to send the WM_QUIT message to the window. GetMessage: gets the message. The blocking function is PeekMessage: gets the message. The non-blocking function */while (GetMessage (& msg, NULL, 0, 0 )) {/* TranslateMessage: converts a keyboard message to a character message. 1. check whether it is a Keyboard Message. 2. If a key message is found, press the key, when the next GetMessage is executed, the message is received. 3. If no key message is found, no processing is performed */TranslateMessage (& msg ); /* Based on the window handle in the message data, DispatchMessage finds the window processing function of the window and calls the processing function to process the message. If the HWND window handle is empty in MSG, DispatchMessage is not processed. */DispatchMessage (& msg) ;}} int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {g_hInst = hInstance; RegisterWnd ("MyWnd "); HWND hWnd = CreateWnd ("MyWnd"); DisplayWnd (hWnd); Message (); return 0 ;}