[Reprint] Windows message transmission mechanism in Delphi

Source: Internet
Author: User

[Reprint] Windows message transmission mechanism in Delphi

In: http://blog.csdn.net/newfang/archive/2006/06/15/800780.aspx
The blog has been on for a long time and has not been written at all. Now that you have already sat down, write something and adjust your mood!

Develop Windows-based applications Program To understand the composition of the Windows message system.

The message itself is passed to the application as a record, which contains the Message Type and other information! This record type is called tmsg. It is declared in the Windows Unit and will not be listed here (even typing is very hard: P)

In Win32, some pre-defined message constants often start with WM to represent a specific message. All Windows messages are defined in the message unit of Delphi. If you are interested, open the message Unit to study it!

Windows message system consists of three parts: (1) Message Queue. Windows can maintain a message queue for all applications, and the application must obtain messages from the message queue, then it is assigned to a window. (2) message loop. Through this loop mechanism, the application retrieves a message from the message queue, distributes it to an appropriate window, and proceeds in sequence. (3) Window Process. Each window has a window process to receive messages sent to the window by windows, the task of the window process is to get a message and respond to it (the Window Process is a callback function. After processing a message, a return value is usually given to Windows ).

From the generation of a message to the response of a message by a window, perform the following steps: (1) an event occurs in the system. (2) Windows translates the event into a message and places it in the message queue. (3) The application receives the message from the message queue and stores it in the tmsg record. (4) The application sends messages to an appropriate window. (5) The Window responds to the message and processes it.

The following describes the VCL message system processing principles of Delphi:

Each VCL component in Delphi has an internal message processing mechanism. When a form is created or a component is added, VCL has registered a message receiving routine mainwndproc, this routine is inherent in every form and component. Mainwndproc is a static method defined in the twincontrol class and cannot be overloaded. It does not directly process messages, but is handed over to wndproc for processing and exception protection. If an exception occurs, the application. handleexception method is called to handle the exception. You can view the twincontrol mainwndproc implementation by yourself. Code I will not post it here!

Wndproc is a virtual method in the tcontrol class, which means it can be overwritten and provides custom message processing routines (in fact, the tcontrol component uses it to filter and process all mouse messages (from wm_mousefirst to wm_mouselast ). Wndproc calls the dispatch method for message distribution. For specific code, you can view the tcontrol wndproc code implementation.

The dispatch method is defined in the tobject root class. The parameter passed to it must be a record type, and the first vertex in the record must be a cardinal field. The dispatch method calls the handle method for processing the message in the last inheritance class of the component based on the message number. If neither the component nor its ancestor class corresponds to the processing handle of the message, the dispatch method calls the defaulthandler method. The dispatch method is a virtual method defined in the tobject class, it is a simple response and does not process any messages. We can overload this virtual method and implement default message processing in the subclass (oh ...... My God, why is it dropped ~~).

This is how Delphi processes Windows messages!

Send message:

Delphi mainly sends messages in three ways. Perform () is used for all tcontrol derived objects. Sendmessage () and postmessage (). I'm sure I'm familiar with this. I will not talk about it any more! If you need to send messages across processes, you need to use registerwindowmessage (), which ensures that each application uses a consistent message sequence number. For more information, see help! An object derived from twincontrol can call broadcast () to broadcast a message to its child component. This technology can be used when the same message needs to be sent to a group of parts! If you want to use sendmessage () or postmessage () to broadcast messages, you only need to set the first parameter -- target object handle -- To hwnd_broadcast, which means to send messages to the main window of all applications!

Message Filtering: There are three methods for Delphi message filtering. The virtual method wndproc inherited by the overload component is used, the message processing handle is written for a message, and the virtual method defaulthandler inherited by the overload component is used. The second method is commonly used!

Related Article

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.