Windows message mechanism (1): Message Overview

Source: Internet
Author: User
Event drive)

Events generate messages, and applications generate messages.

The operations on the DOS system are easy to understand, and a command is input to execute a series of actions. enter a command to input a function name and call it (sometimes a parameter is required ). in Windows, there are some graphic operation interfaces. what will happen after you click a mouse in the image area? First of all, you guess whether it will be done by clicking on the icon, that is, calling a function to do some operations. Actually, this is true, but there are still some complicated processes in the middle.

When we use the mouse or keyboard to perform some operations, we call these operations an event ). the mouse and keyboard are hardware operations. We know that applications will not deal with hardware directly, but the operating system will deal with hardware, and then the application will only deal with the operating system.
When the operating system captures those events, a corresponding message is generated ). in addition, the program can directly generate messages in the code. we know that a lot of operations will occur constantly, and a lot of messages will be generated, but we will process them one by one. so there is a message queue.

 

Message Queue)

System message queue, thread Message Queue

When learning the data structure of the queue, we know that it is a first-in-first-out linear structure.

1) system message queue)

All messages generated by hardware events are captured by the operating system and saved in the system message queue. Then, the operating system sends these messages to the corresponding thread message queue.

2) thread Message Queue (thread-Specific Message Queue)

We didn't see any threads on the surface when creating a window. In fact, the background operation is to create a UI thread first, and then the thread creates window-related content. each UI thread has a thread message queue.

When we explicitly create a thread, if there are interface-related tasks used in the thread (check its message queue or create a window ). we also use this thread as the UI thread and add a thread message queue to it.

In addition to receiving messages sent by the system message queue, the thread message queue also accepts messages generated by the program, such as messages sent through postmessage. (Note: messages sent through sendmessage are not sent to the thread message queue, but directly sent to the window for processing)

 

Queued and non-queued messages

The messages in the system message queue and thread message queue are called queuing messages. As the name suggests, messages in the queue must be queued for processing. messages generated like sendmessage are directly sent to the window without queuing, so they are called non-queuing messages.

 

Message Definition

After talking about the message for a long time, you may ask whether the message is too weak. What is it like? Actually, it is a struct.

Typedef struct tagmsg
{
Hwnd;
// The Window handle for receiving the message. It determines the process of sending a message to the Message Queue of the middle thread or directly to the window.
Uint message;
// Message constant identifier, such as wm_create
Wparam;
// The specific additional information of a 32-bit message. The exact meaning depends on the message value.
Lparam;
// The specific additional information of a 32-bit message. The exact meaning depends on the message value.
DWORD time;
// Message creation time
Point pt;
// The cursor/cursor position in the screen coordinate system when the message is created
}
MSG

Where

Wparam contains the keyboard and mouse information. For example, if wparam & mk_lbutton is left-clicked and wparam & mk_shift is clicked, the shift key is pressed. If lparam is pressed, the screen coordinate information loword (lparam) is included) // y axis

 

The callback function is defined as lresult callback wndproc (hwnd, uint message, wparam, lparam)

The function parameter is exactly the first four member variables of MSG.

 

 

 

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.