Window message mechanism

Source: Internet
Author: User

Windows is an operating system based on events and messages. To learn about Windows programming, you must understand the event and message mechanisms.

1. What is a message?

Generally speaking, it is a notification sent by the operating system to the application, which tells the application of an event. For example, if you click the mouse, change the window size, or press a key on the keyboard, Windows will send a message to the corresponding application.

Ii. Composition of messages

The message itself is transmitted to the application as a record. This record type is called tmsg. The tmsg record consists of a message name (uint), two parameters (wparam, lparam), and other information.

In the Windows unit of Delphi, it is declared as follows: Type tmsg = packed record hwnd: hwnd; // window handle message: uint; // message constant identifier wparam: wparam; // specific additional information of 32-bit messages lparam: lparam; // specific additional information of 32-bit messages time: DWORD; // the time when the message was created PT: tpoint; // The cursor position end when the message is created. The tpoint is defined as follows: tpoint = record X: integer; Y: integer; end;

The definition in C language is as follows: typedef struct MSG {hwnd; // window handle uint message; // message constant identifier wparam; // additional information of a 32-bit message lparam; // DWORD time for a 32-bit message; // tpoint PT when the message is created; // The mouse position when the message is created} tmsg; typedef struct tpoint {int X; int y;} tpoint; Iii. Meaning of each field in message record (tmsg)Hwnd: 32-bit window handle. A window handle is a 32-bit identifier allocated to each window to distinguish multiple windows in the system. The system uses a window handle to uniquely identify a window in the system. Message: a constant used to distinguish other messages. These constants can be pre-defined constants in Windows or user-defined constants. Wparam: 32-bit. It is usually a message-related constant, or a handle to a window or control. Lparam: 32-bit, usually a pointer to the data in the memory. Time: the time when the message was created. PT: The cursor position when the message is created. Iv. Composition of WINDOWS Message SystemWindows message system consists of three parts: 1) message queue. Windows can maintain a message queue for all applications. The application must obtain the message from the message queue and assign it to a window. 2) message loop. Through this loop mechanism, the application retrieves a message from the message queue and then assigns it to an appropriate window. Then, it retrieves the next message from the message queue and then assigns it to the appropriate window,. 3) window process. Each window has a window process to receive messages sent to the window. Its task is to obtain the message and then respond to it. A window is a callback function. After processing a message, it usually returns a value to Windows. Note that a callback function is a function in a program that is called by a Windows or external module. 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 obtains 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 second-level control center is generally the application objects of various Windows applications. The third-level control center is a Windows form object. Each form has a default form process, which processes various received messages. (Note: Windows refers to the Windows operating system; Windows refers to Windows; forms: including windows and controls with handles; control refers to controls, which may also be a window, or not; application is an application, and the application may not use the Windows message mechanism. Here we will discuss the applications with message loops) V. Message Classification

Windows OS includes the following messages:

1. Standard Windows messages:

This type of message starts with WM.

2. Notification Message

Notification messages are messages for standard Windows controls. These controls include buttons, ComboBox, Textbox, ListBox, listview, Treeview, and toolbar), menu, etc. Each type of message starts with a different string.

3. Custom messages

Programmers can also customize messages. 6. HandleNot every control can receive, forward, and draw itself. Only a control with a handle can do this. 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. So why does the handle make the window so unique? It is actually because of the message. With a handle, the form can receive messages, so it knows when to draw itself, draw the child control, and know when to click the part of the window, and then proceed accordingly. A handle is like a person's ID card. With it, You can engage in various social activities. Otherwise, you will either be a hacker invisible to the society or be behind others, prove your existence by others. VII. message transmission1. Get a message from the message queue: You can use the peekmessage or getmessage function to get a message from the Windows message queue. Message Queues stored in windows are grouped by threads, that is, each thread has its own message queue. 2. 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. Another point is that the message sent by sendmessage will not be added to the Message Queue (error: the thread sends messages to other threads and appends them to the Message Queue sent by other threads, even if the two threads are in the same process), the message sent by sendmessage cannot be obtained through peekmessage or getmessage. In addition, some messages won't succeed with postmessage, such as wm_settext. Therefore, not all messages can use postmessage. There are other message sending API functions, such as postthreadmessage, sendmessagecallback, sendmessagetimeout, and sendpolicymessage.

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.