Windows Message Queuing Learning Notes

Source: Internet
Author: User

1.windows Message and message structure

A message is passed to the application as a structure that contains information such as the message number, the type of message, the character parameter, and the long character parameter. The structure is defined as follows:

typedefstruct tagMSG {  HWND hwnd;  UINT message;  WPARAM wParam;  LPARAM lParam;  DWORD time;  POINT pt; } MSG;
    • The first member variable HWND represents the window to which the message belongs. In a Windows program, the window is identified with a variable of type HWND.
    • The second member variable message specifies the identifier of the message. In Windows, messages are represented by a numeric value, and different messages correspond to different values. However, because the numeric value is not easy to remember, Windows defines the value of the message as the WM_XXX macro (the WM is the abbreviation for window message), and XXX corresponds to the uppercase form of the English spelling of a message. For example, the left mouse button press the message is WM_LBUTTONDOWN, the keyboard presses the message is WM_KEYDOWN, the character message is WM_CHAR, and so on. In the program we usually use the message in the form of a WM_XXX macro.
      Tip: If you want to know the exact value of the WM_XXX message, you can see the specific definition of the macro by selecting Wm_xxx in the Visual C + + development environment and then right-clicking and choosing Goto Definition in the pop-up menu. You can use this method to track or view the definition of a variable.
    • The third and fourth member variables, wparam and lparam, are used to specify additional information for a message. For example, when we receive a character message, the value of the message member variable is WM_CHAR, but the user actually enters what character, then it is explained by wparam and lparam. The information represented by WParam and lparam varies depending on the message. If you want to know the information that these two member variables represent, you can see it in MSDN for a description of a specific message. Readers can look at the definition of wparam and lparam in the VC + + development environment by Goto definition, and you can see that both types are actually unsigned int and long.
    • The last two variables represent the time the message was posted to the message queue and the current position of the mouse.
2.Windows Message Category


Messages form the main framework of the messaging system, with approximately hundreds of common standard messages as follows:

Windows uses different prefix symbols to Peugeot different message types, such as BM for button class messages, CB for combo box messages, DM for press button control messages, and EM for Edit control messages.

3. Message Delivery

In order to give the runtime a strong running environment, requiring each thread to be relatively independent, the system allocates some resources to the thread, the more special one is the allocation of a THREADINFO structure, and when the thread has the THREADINFO structure associated with it, The thread has its own collection of message queues, each of which maintains its own collection of message queues and extracts the messages from them, and then uses the window functions to process them.

The general call to the PostMessage function sends the message to the thread's rank message queue, with the function prototype as follows:

BOOL  PostMessage(HWND hWnd,  //窗口句柄UINT Msg,    //要发送的消息WPARAM wParam,   //第一个参数LPARAM lParam     //第二个参数);

When a thread calls the PostMessage function, the system first knows which thread established the window with the HWND parameter, and then allocates a piece of memory, stores the message parameter in this memory, and adds the memory to the corresponding thread's enlistment message queue.The postmessage function returns immediately after registering the message, so the thread calling the function does not know whether the registered message can be processed by the specified window.
In addition, you can call the PostThreadMessage function to send the message to the thread's enlistment message queue.

BOOL PostThreadMessage(DWORD idThread;    //线程标致。UINT Msg;WPARAM wParam;LPARAM lParam};

To terminate a message loop, you can call the PostQuitMessage function:

void PostQuitMessage(int nExitCode);

The function does not actually register a message into any Threadinfo structure queue, but inside, the PostQuitMessage function sets Qs_quit to wake Peugeot and set the Nexitcode member of the THREADINFO structure, because these operations never fail , so the prototype of the PostQuitMessage function is defined to return void.

4. Message Queuing Status Flags

You can call the Getqueuestatus function to query the status of the queue:

flags);

Parameter flags is a Peugeot that is connected to or a group of or, and can be used to test special positioning.
-qs_allevents: Input, Wm_timer, wm_paint,wm_hotkey or sent messages in the queue.
-Qs_allinput: Any message in the queue.
Qs_allpostmessage: The message sent (not the other listed messages) is in the queue.
Qs_hotkey: A Wm_hotkey message is in the queue. Qs_input: The input message is in the queue.
Qs_key: A wm_keyup wm_keydown,wm_syskeyup or Wm_syskeydown message in the queue.
Qs_mouse:wm_mousemove messages or mouse key messages (Wm_buttonup Wm_rbuttondown, etc.) in the message queue.
Qs_mousebutton: mouse button messages (Wm_lbuttonup,wm_rbuttondown, etc.) are in the message queue.
The Qs_mousemove:wm_mousemove message is in the message queue.
The Qs_faint:wm_paint message is in the message queue.
Qs_postmessage: The message sent (not the other listed messages) is in the queue.
Qs_sendmessage: Messages sent by other threads or applications are in the message queue.
QS_TIEMR: A WM_TIEMR message is in the message queue.
When this function is called, the parameter flags the message type to be checked to tell the function that, when returned, the current message type is returned in a well-deserved high byte, and the low byte indicates the queue that has been added to.

5. Extracting messages

You can extract a message from a message queue by using the GetMessage function. The GetMessage function is prototyped as follows:

GetMessage(LPMSG lpMsg,HWND hWnd,UINT wMsgFilterMin,UINT wMsgFilterMax);

Parameter description:
Lpmsg: A pointer to the MSG structure that receives message information from the thread's message queue.
HWnd: The handle of the window that gets its message. When its value is NULL, GetMessage retrieves the message for any window that belongs to the calling thread, and the thread message is sent to the calling thread via PostThreadMessage.
Wmsgfiltermin: To get the minimum value of the message, the pass-through is set to 0.
Wmsgfiltermax: To get the maximum value of the message, if both Wmsgfiltermin and Wmsgfiltermax are 0, all messages are accepted.
GetMessage Returns TRUE if there is a message and the message is not wm_quit; the condition of returning FALSE is that there is a message and the message is Wm_quit waits for a message when there is no message, and the CPU is of course low-blocking.
For an in-depth analysis of the message, you can read this blog. This is also recommended to see

When extracting a message, the system must check the status of the queue and give the message extraction process line:

(1) If the QS_SENDMESSAGE flag bit is set, the system sends a message to the appropriate window procedure. The GetMessage function does not return after the window procedure finishes processing the message, waiting for other messages to be processed.
(2) If the message is re-queued, the GetMessage function populates the MSG structure and returns, at which time the message loop usually calls the DispatchMessage function, allowing the window procedure to process the message.
(3) If the QS_QIUT flag is set, the GetMessage function returns a WM_QIUT message and resets Qs_qiut Peugeot.
(4) If the message is again virtual input queue, the GetMessage function returns the hardware input message.
(5) If Qs_timer Peugeot is set, the GetMessage function returns a WM_TIMER message.
(6) Although called Message Queuing, messages in the queue are not always FIFO. For example, if there is wm_qiut in the message queue, the Wm_qiut will be removed first, causing the program to crash. WM_PAINT and Wm_timer are removed only when there is no other message. If there are multiple wm_paint or wm_timer, they may be merged into one.

Finally, let's summarize the process with a simple diagram:

When designing a Windows application based on a message-driven mechanism, the basic process of the program is left to Windows to handle. Windows has a certain initiative, wholeheartedly for the "received message to find the corresponding processing function" to provide services, and users only need to focus on their own things, such as writing their own window functions.

Windows Message Queuing Learning Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.