This is another interview question. When I come back, I will summarize it.
The biggest feature of Windows is its graphical operation interface, which is based on its message processing mechanism.
I. There is a system message queue in windows. For every running Windows application, the system creates an application queue-"message queue"-to store messages in various Windows that may be created ". There is a piece of code called "message loop" in the application.
While (getmessage (& MSG, null, 0, 0 ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
The MSG variable is a MSG-type structure. The MSG type is defined in winuser. h as follows:
Typedef struct tagmsg
{
Hwnd;
Uint message;
Wparam;
Lparam;
DWORD time;
Point pt;
}
MSG, * PMSG;
The point data type is also a structure, which is defined in windef. h as follows:
Typedef struct tagpoint
{
Long X;
Long y;
}
Point, * Ppoint;
Translatemessage (& MSG); transmits the MSG structure to Windows for some keyboard conversion. (We will discuss this in depth in chapter 6 .)
Dispatchmessage (& MSG); returns the MSG structure to Windows. Then, Windows sends the message to the appropriate window message handler for processing. In other words, Windows calls the window message processing program. In hellowin, the window message handler is the wndproc function. After the message is processed, wndproc is passed back to Windows. In this case, Windows is still in the dispatchmessage call. After the dispatchmessage call is processed, Windows returns to the hellowin program and starts the message loop from the next getmessage call.
Differences between sendmessage () and postmessage:
Both are used to send messages to applications. Postmessagex () directly adds a message to the Message Queue of the application, and exits without waiting for the program to return. sendmessage () is the opposite. After the application processes the message, it returns. I want to better reflect the relationship between the two functions:
What is the difference between the peekmessage function and getmessage function?
1. getmessage will not be returned until there is a suitable message, and peekmessage is just a glimpse of the message queue.
2. getmessage will delete the message from the queue, while peekmessage can set the last parameter wremovemsg to determine whether to keep the message in the queue.