Windows message mechanism-Summary & understanding

Source: Internet
Author: User

 

Windows is a message-driven system.

1. A message is a struct type record, which generally includes: hwnd-window handle, uint-message constant identifier, wparam-32 bit message-specific additional information, lparam32 bit message-specific additional information, DWORD-message creation time, tpoint-message creation time mouse Coordinate

2. Message Queue. in windows, a message queue can be maintained for all applications. The application must obtain messages from the message queue and then assign them to a window. (Does the application take the initiative to get it from the Message Queue ?) (Can there be multiple message queues ?) (Each thread has a message queue)

3. message loop: The application retrieves a message from the message queue, distributes it to an appropriate window, and then retrieves the next message from the message queue, and then assign it to the appropriate window for one operation. (Does the application actively participate in the cycle to find messages? Is it equivalent to a while (true) infinite loop ?) (Each application has its own message loop to get its own message in the loop. When there is no message, the message loop gives control to the system)
Message loops are the root cause for the persistence of applications. If the application exits cyclically, the application ends.

4. in the window process, each window has a window process to receive messages sent to the window. Its task is to get the message and then respond to it. The Window Process is a callback function; after processing a message, it usually returns a value to Windows. (An application can have multiple windows) (a callback function is a function in the program called by a Windows or external module)

There are five steps for a message to be responded by a window from generation to generation:

1. An event occurred in the system. (How is a datareceive event triggered ?)

2. Windows translates the event into a message and puts it in the message queue.

3. The application receives the message from the message queue and stores it in the tmsg record.

4. The window process in which the application sends messages to an appropriate window. (Is the window process a system function)

5. The Window Process responds to the message and processes it.

Step 3 and 4 constitute the message loop of the application. Message loops are often the core of Windows applications, because message loops allow an application to respond to external events. The task of message loop is to retrieve messages from the message queue and pass the messages to the appropriate window. If there are no messages in the message queue, Windows allows other applications to process their messages.

The biggest feature of Windows is its graphical operation interface, which is based on its message processing mechanism.

Windows message control center is like a heart, and messages are like blood.

Windows message control center is generally a three-tier structure with the Windows Kernel at the top. The Windows Kernel maintains a message queue. The second-level control center is generally the application object of various Windows applications. It obtains the messages under its jurisdiction from the message queue and then processes them. Some messages are directly processed, some of them also need to be sent to the next level form (window) or control ). The third-level control center is a Windows form object. Each form has a default form process, which processes various received messages.

Form: includes a window and a control with a handle. control refers to a control. The control itself may be a window or not. application is an application, the Windows message mechanism may not be used by applications.

1. Standard Windows messages, which start with WM.

2. notification messages are messages for standard Windows controls. Including buttons, ComboBox, Textbox, listview, Treeview, toolbar, and menu. Each type of message starts with a different string.

3. Custom messages. programmers can also customize messages.

Only a control with a handle can receive, forward, and draw messages. Controls with handles are essentially a window. They can exist independently and can be used as containers of other controls. controls without handles, such as labels, cannot exist independently, it can only be used as a child control of the window control. It cannot draw itself, but can only be drawn by the parent form.

The handle is essentially a 32-bit value automatically maintained by the system. This value is unique at any time of the operating system. However, after the form indicated by the handle is released, the handle is released, which may be used by other forms. That is to say, the value of the handle is dynamic, and it is only a unique identifier. The operating system uses the handle to identify and find the objects it represents.

However, not all handles are form handles. in windows, there are many other types of handles, such as the HDC handle, paint brush handle, and paint brush handle, application handle (hinstance. Such a handle cannot receive messages. No matter which handle is used, it is the unique identifier of an object in the system. This article only discusses the form handle.

Get a message from a message queue: You can use the peekmessage or getmessage function to obtain a message from a Windows message queue. Message Queues stored in windows are grouped by threads, that is, each thread has its own message queue.

To send a message to a specified form, use the following two functions: sendmessage and postmessage. The difference between the two functions is that the postmessage function only adds a message to the thread message queue. if the message is successfully added, true is returned. Otherwise, false is returned, whether the message is processed, or whether the message is processed, I don't know. Sendmessage is somewhat different. Instead of adding messages to the queue, sendmessage directly translates messages and calls Message Processing (this is the case when the thread sends messages to itself ), it is not returned until the message processing is complete. Therefore, if we want to send a message to be executed immediately, we should call sendmessage.

The form process is actually a callback function. The so-called callback function is actually a function called by a Windows operating system or an external program. Callback functions generally have a specified parameter format, which is passed to the caller in address mode. The window is called by the Windows operating system. When creating a window, you need to input the callback function address when allocating the form handle. Why can't we see this callback function during programming? This is because our programming tool has generated a default form process for us. The only thing we need to do in this process is to judge different message types and then make different processing. For example, an event can be generated for keyboard or mouse input.

Events are essentially message encapsulation and are a useful tool provided by the IDE programming environment to simplify programming. This encapsulation is implemented in the form process. Each ide encapsulates many Windows messages, such:
OnActivate-WM_ACTIVATE
OnClick-WM_XBUTTONDOWN
OnCreate-WM_CREATE
OnDblClick-WM_XBUTTONDBLCLICK
OnKeyDown-WM_KEYDOWN
OnKeyPress-WM_CHAR
OnKeyUp-WIN_KEYUP
OnPaint-WM_PAINT
OnResize-WM_SIZE
OnTimer-WM_TIMER

 

 

 

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.