[Original] briefly describe the Delphi message mechanism!

Source: Internet
Author: User

Every window control with a handle (inherited from twincontrol) must register a window process (mainwndproc) in windwos when it is created ). This window is not called by the application itself, but by Windows !!! Therefore, the message processing code written in this process can be automatically called and run.
A typical window process contains a large case Branch to process different messages! For example:
Case message. MSG
Wm_lbuttondown:
Dosomething1...
Wm_close:
Dosomething2...
Wm_paint:
Dosomething3...
Wm_mousewheel:
Dosomething4...
Wm_size:
Dosomethingn...
End;
In this way, when we write code in the corresponding message, when this message occurs on this control, the corresponding code will be executed. (For example, if you write code in the preceding wm_lbuttondown message, When you click the left mouse button on the control, the code you wrote in wm_lbuttondown will be executed !)
  
========================================================== ========================================================== ======================================
The simplest statement is described above. When you click the left mouse button on a control, the detailed process is as follows:
  
Click the left mouse button on a control. Windows generates a wm_wm_lbuttondown message of the tmsg record type, which contains the handle of the message receiver in the tmsg record. Then, Windows sends the message to the corresponding application based on the handle in the tmsg record. If this application is made in Delphi, VCL will send the message to tapplication. in the message loop queue created by run, and then the application is called here. onmessage event. If there is no corresponding Message Processing Method in the onmessage event, tapplication is called. based on the handle of the message receiver in the tmsg record, dispatchmessage sends the message to the window registered by the handle in Windows (mainwndproc in VCL ). So far, this message is sent in windows ].
  
[Dispatch in Windows] can be expressed using the following flowchart:
  
Messages are generated in Windows (such as clicking the mouse) → tapplication. run → application. handlemessage → call tapplication. processmessages → call application. onmessage event (if any) → tapplication. dispatchmessage → call twincontrol. mainwndproc enters the VCL internal message dispatch!
  
The message dispatch process in vcl is as follows ]:
Twincontrol. mainwndproc directly sends the message to the tcontrol. windowproc attribute without any processing. The windowproc attribute is a method type pointer, so it calls the tcontrol. wndproc virtual method. The wndproc virtual method can process the following types of messages: wm_keyfirst, wm_mousefirst, wm_lbuttondblclk, success, fail, wm_mousemove, wm_lbuttondown, failed, wm_lbuttonup, cm_visiblechanged. These messages are processed by tcontrol. wndproc and will not be passed down. Since wndproc is a virtual method, it is overwritten multiple times in the Child classes after the tcontrol class (for example, you can override this virtual method again, most components obtain most message processing methods through inheritance. Other messages are passed down to tobject. Dispatch. The dispatch method first searches for the corresponding message processing method in this class, if it finds the message processing method. Otherwise, go up to the parent class and ancestor class step by step until the corresponding message method is found. If the tobject layer is found and the corresponding message processing method is not found, call the tobject. defaulthandler method. Basically, most of the useless messages will be discarded. So far, the message processing process has ended.
 
[Message dispatch process in vcl] can be expressed using the following flowchart:

Twincontrol. mainwndproc → tcontrol. windowproc property (a method type pointer) → tcontrol. wndproc (virtual method, which can be override by the user) → tobject. dispatch (search for the corresponding message method from the parent class starting from the subclass)

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.