Understanding Windows message Loop mechanism

Source: Internet
Author: User

Understanding message loops and the entire messaging mechanism is important for Windows programming. If you don't understand the whole process of message processing, there are a lot of confusing things to do in Windows programming.

What are messages (message)

Each message is an integer value, and if you look up a file (looking at the header file to learn about the API is a good habit and a common practice) you can find some of the following macro definitions:

#define Wm_initdialog 0x0110
#define Wm_command 0x0111

#define Wm_lbuttondown 0x0201
//...


In Windows communication, there are at least some basic Windows communication, with almost all of the messages being used. If you want the window or control (in essence, the control to be a special window) to perform what action, you should send a message to it, and if another window wants you to do anything, it can send a message to you. If an event, such as tapping the keyboard, moving the mouse, clicking the button, etc., the system sends the message to the window, and if you are one of those windows, you will receive the message to perform the appropriate action.

There are two parameters for each Windows message, wparam and lparam. The initial wparam was 16 (Win16), and the lparam was 32-bit. In Win32, two parameters are 32 bits. Not all messages are using these two parameters, and each message uses them differently. If the WM_CLOSE message ignores the above two parameters, and if the WM_COMMAND message uses the above two parameters, WParam contains the "two" value, HiWord (WParam) is the notification information (if available), LoWord (WParam) The id,lparam of the control or menu that is sending the message is the HWND (window handle) of the control that sent the message, and if the value is NULL, the message is not sent by the control.

HiWord () and LoWord () are Windows-defined macros that take out a 32-bit integer value of high and low characters, respectively. In Win32, a "word" is a 16-bit integer, and a DWORD (Double WORD) is a 32-bit integral type.

You can send messages with PostMessage () or SendMessage (). PostMessage () returns a message immediately after it is placed in the message queue, that is, when PostMessage () is called and the function execution is returned, it is likely that the message has not been processed. SendMessage () sends the message directly to the window until the message processing is complete. If you want to close a window, you can send it a wm_close message, like PostMessage (hwnd, WM_CLOSE, 0, 0); The effect is the same as clicking the (Close) button in the upper-right corner of the window. Note that both the wparam and lparam values here are 0, as mentioned earlier, the WM_CLOSE message ignores the above two parameters.

dialog box (Dialogs)

If you use a dialog box to communicate with the control, you need to send a message to the control. You can either use the GetDlgItem () function to get a handle to the control based on the ID of the control, and then call the SendMessage () function to send the message, or use SendDlgItemMessage () to combine the above steps. Passing in the ID of a window handle and child control can take a handle to the child control and use this handle to send a message. APIs like SendDlgItemMessage () such as GetDlgItemText () can operate on all windows, not just dialogs.

What is Message Queuing (Messages queue)

Suppose a scenario: the system is processing the WM_PAINT message, just at this point when the user taps some keys on the keyboard, what happens? Should the system interrupt the drawing operation and then handle the keystroke message or the message that the key should be discarded? Obviously these are unreasonable, so we introduced Message Queuing, when a message is sent, a message is added to the message queue, and when a message is processed, it is removed from the message queue. This ensures that the message is not lost and that when you are processing a message, other incoming messages can be added to the message queue until they are processed.

What is a message loop (Messages loop)

while (GetMessage ( &msg, NULL, 00) 0)
{
    translatemessage ( &msg);
    dispatchmessage (& MSG);
}
/span>


The above code executes in the following procedure:
1. Message loop call GetMessage () Finds messages from the message queue for processing, and if Message Queuing is empty, the program stops executing and waits (program blocking).
2. When an event occurs that causes a message to be added to the message queue (for example, the system registers a mouse click event), GetMessage () returns a positive value indicating that a message needs to be processed and that the message has been populated into the incoming MSG parameter; 0 is returned when the Wm_quit message is passed in If a negative return value indicates an error has occurred.
3. Remove the message (in the MSG variable) and pass it to the TranslateMessage () function, which does some extra processing: Converts the Virtual key value information to character information. This step is actually optional, but there are some places that need to use this step.
4. After the above steps have been executed, pass the message to the DispatchMessage () function. The DispatchMessage () function distributes the message to the target window of the message, and finds the target window procedure function, passing the window handle, message, WParam, lparam, and so on to the window procedure function and calling the function.
5. In the window procedure function, examine the message and other parameters that you can use to implement the action you want. If you do not want to handle some special messages, you should always call the DefWindowProc () function, and the system will handle these messages in the default manner (generally, do not take any action).
6. Once a message processing is complete, the window procedure function returns, and the DispatchMessage () function returns, continuing to loop through the next message. The

message loop is a very important concept for Windows programming. Window procedure functions are not automatically called by the system, but are called indirectly by the developer by calling DispatchMessage (). If you wish, you can call the GetWindowLong () function through the window handle to find the window procedure function directly called to achieve the purpose of message processing.

while (GetMessage ( &msg, NULL, 00) 0)
{
    wndproc Fwndproc =     fwndproc (Msg.hwnd, Msg.message, Msg.wparam, Msg.lparam);
}

I try to write the code above, it does work, but there are a variety of problems, such as unicode/ansi encoding conversion, timer callback and so on, and so the code is not suitable, and it is likely to cause a lot of interruptions to the normal operation of many programs. So this code is just a test here, and it must not be written in a real project.

Notice here that we use GetWindowLong () to get the window procedure function of the window. Why don't we just call the WndProc () function? The message loop handles the messages for all windows in the program, including controls like buttons, list boxes, and so on that have their own window procedure functions, so we want to ensure that the correct window procedure function is called. Although sometimes several Windows call the same window procedure function, the first parameter of the function (the handle of the window) is often used to tell the window that the procedure function is the message that the window sends.

The code can see that most of the time the program is processing the message loop. The window will continue to process the sent message, but what if you want to quit the program? Because we are using a while () loop, if GetMessage () returns False (that is, 0) exits the loop, the program can execute to the end of WinMain (), the program exits: This is exactly what the PostQuitMessage () function does, The function adds the Wm_quit message to the end of the message queue, GetMessage () takes the Wm_quit message from the message queue, populates the MSG structure, returns a positive number, but 0. At the same time, the value of the member wparam of the structure MSG is set to the value you pass to the PostQuitMessage () function parameter, and you can choose to ignore it or make it the return value of the WinMain () function, which is the exit code for the process.

Note: If an error occurs, the GetMessage () function returns-1. You should keep this in mind, and your program will probably go wrong. Although GetMessage () returns a value bit bool type, it can return a value other than true or FALSE, because BOOL is defined as a uint (unsigned int). The following program seems to work correctly, but sometimes it does not work properly.

While(GetMessage (&MSG, NULL,0,0
while (GetMessage ( &msg, NULL, 0, 0!= 0)

while< Span style= "color: #000000;" > (GetMessage (&msg, NULL, 00) ==


The above code is all wrong! Some programs you will see will use the first way, in this way you must ensure that getmessage () always executes successfully, otherwise the following code should be used:

while(GetMessage (&Msg, NULL, 0, 0) > 0)


I hope you have a good understanding of the Windows message loop, and if not, slowly, you will gradually understand the use of the process.

English Original: http://winprog.org/tutorial/message_loop.html

Understanding Windows message Loop mechanism

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.