Brief introduction to VCL messaging
In addition to encapsulating Windows messages, VCL has some of its own customs. Windows's message begins with WM_, you can refer to Windows SDK or MSDN in Delphi Help, you can also see it in the message.pas file. Some internal messages of VCL are generally starting with cm_ cn_ cb_, in the controls.pas file It can also be seen in the specific use of Delphi information.
Below, I will introduce a brief introduction to the internal message of VCL.
Each VCL component in Delphi has an intrinsic message processing mechanism. The basic principle is that the component class receives some messages and sends them to the appropriate processing method. If there is no specific processing method, the default message handles handle is called. Several methods for processing messages are:
MainWndProc is a static method defined in the TwinControl class and cannot be overridden. It does not directly process messages, but is handled by WndProc method and provide an exception handler for the WndProc method. The MAINWNDPROC method is as follows:
Procedure MainwndProc (Var Message: TMessage);
WndProc is a virtual method defined in the TControl class, which calls the Dispatch method for messages, and the WndProc method is as follows:
Procedure WndProc (Var Message: tMessage); Virtual;
The Dispatch method is defined in the TOBJECT root class, which is as follows:
Procedure TOBJECT.DISPATCH (VAR Message);
The message parameter passed to the Dispatch must be a record type, and the first entry in this record must be a Cardinal type domain (Field), which contains the message number of the message to be assigned, such as the TMESSAGE type (see DELPHI), and the Dispatch method handles the handle method of this message based on the last transcript of the message number calling component. If this component and its ancestors do not have the handle of this message, the Dispatch method will call the DEFAULTHANDLER method. The DefaultHandler method is the virtual method defined in TOBJECT, which is as follows:
Procedure defaulthandler (var message); virtual;
The DEFAULTHANDLER method in the TOBJECT class is just a simple return without any processing for messages. We can implement the default processing of messages in subclasses by overloading this virtual method. For components in the VCL, Its defaulthandler method will start the Windows API function DEFWINDOWPROC to process the message.
TMYEDIT in the above as an example.
TMYEDIT = Class (TEDIT)
Private
...
protected
Rewinding the statement of message event messages
Procedure cmexit (var message: tcmexit); message cm_exit ;?
public
...
ENDL
The statement of the message is as follows, the message processing method must be a process, and there must be a message after the method. In the back add message definition, it is not necessary to explicitly indicate the overload with the Override command, and generally declare the Protected or Private area of the component.
Of course, you can write your own message, usually add a constant from WM_USER as your message. You can trigger your message via SendMessage and PostMessage. All events of VCL are triggered by messages, so that you can also add yourself. Component event. By the way, the difference between SendMessage and PostMessage: The former one is returned, the last one is not necessary to return. If your change in the component is the result of the triggered processing of a message, you The declaration of SendMessage. SendMessage must be used as follows
Function SendMessage (hwnd: hwnd; msg: uint; wparam: wparam; lparam: lparam): LRESULT; stdcall; The first parameter is the component handle, the second is the message constant, the back is some other parameter information, mouse, keyboard, etc. Wait. Here we don't separately define custom messages, use cm_exit to try an example:
SendMessage (MyEdit.hanle, cm_exit, 0, 0);
This will trigger CMEXIT is the focus of the control exits the OneXIT event.
Regarding the VCL message, I said so much. I didn't understand it. I didn't say it clearly. The Windows API does not say, many of the information can be checked, MSDN is very complete, I only know a few commonly used .
?