<> news , Message queuing
<>1. news
stay windows In the process , The news is from MSG Structure .MSG The definition of structure is as follows :
typedef struct tagMSG { HWND hwnd; UINT message; WPARAM wParam; LPARAM lParam;
DWORD time; POINT pt; }MSG;
The meaning of each member variable in the structure is as follows :
hwnd : Represents the window to which the message belongs . stay Windows In the process , use HWND Type to identify the window .
message : Variable specifies the identifier of the message . stay Windows
in , The message is represented by a numeric value , Different messages correspond to different values . However, the numerical value is not easy to remember , therefore Windows Define the value of the message as WM_XXX macro (WM
yes Window Message Abbreviation for ) The form of ,XXX
The upper case of the English spelling of a message . for example , The message of the left mouse button is WM_LBUTTONDOWN, The key press message is yes WM_KEYDOWN, The character message is WM_CHAR
wait . In the program , We usually use it WM_XXX Macro to use the message .
wParam and lParam Additional information used to specify the message . such as , When we receive a character message
Waiting ,message The value of the member variable is WM_CHAR, But what are the characters that the user enters , It's up to you wParam and
lParam To illustrate .wParam,lParam The information represented varies with the message .
time and pt It represents the time when the message is delivered to the message queue and the current position of the mouse .
<>2. Message queuing
every last WindowS After the application starts executing , The system will create a message queue for the program , This message queue is mainly used to store the messages of the window created by the program .
for example , When you draw in the window , Press the left mouse button , here , The operating system will sense the event , The operating system then wraps the event as a message , To the message queue of the application , Wait for the application to get the message , Then deal with it . The application then continuously fetches messages from the message queue through a message loop , And respond .
In fact, in the above process , The operating system gives the application ‘ send message ’. So called ‘ send message ’, In fact, the operating system calls a function in the application that is specifically responsible for processing messages , This function is called window procedure .
Take another example , Like you're right word The minimization operation is performed , This operation is not word Document operations , Instead, the minimal operation is captured by the operating system , Then the minimized message is sent to the word Message queuing for applications , then word The program gets the minimized message from the message queue through a loop . then word Distribute the message to the operating system , Then the operating system senses the message , The callback function is then called .
Here is a flow chart of a message call :
actually windows Maintains a system message queue , And for each GUI Threads maintain a separate thread message queue . In order to avoid non-compliance GUI The cost of creating thread message queue , When all threads are created and initialized , No message queues are created . Only when the thread calls for the first time GDI Function time , The system creates a message queue for the thread . So those who are not GUI Thread has no message queue .
Whenever the user moves the mouse , When you click a button or keyboard , The device driver for the mouse or keyboard converts the input into a message , And put the message in the system message queue . Delete windows Check your message queue , If the message queue is not empty , One message at a time is retrieved and deleted , The target window of the message is then determined , Then put the message into the thread message queue of the thread that created the window . The thread's message queue receives all mouse and keyboard messages from the window created by the thread . The thread then removes the information from the queue , And tell the system to send them to the corresponding window message processing function .
There are always some spurts on the way to study , There's always some disgusting mouth like shit that will bite you , There's no need to pay attention , On the contrary, we should study hard , Fight back with technology .
Technology