Message process mechanism in Delphi
Delphi is a brand new Windows programming development tool provided by Borland. Since it uses elastic and reused object-oriented Pascal (Object-Oriented Pascal), there is a powerful database engine (BDE), fast code The compiler, but also provides many excellent components. It is favored by the broad masses of programming people. In numerous programming languages (such as VB, PowerBuilder, PowerPoint, etc.). One of the Delphi is stronger than other programming languages (such as VB4.0) The place is to customize messages in Delphi and can process messages directly. This is essential for users who wish to write their own components, or want to intercept. Filter messages are essential. Because writing components are generally Processing the corresponding message. The message processing mechanism in Delphi is described below. One. Delphi in Delphi VCL Delphi Each VCL (such as TButton, Tedit et al.) Has an intrinsic message processing mechanism. The basic point is that the component class receives some messages and sends them to Appropriate processing method, if there is no specific processing method, call the default message handling handle. Where MainWndProc is a static method defined in the TwinControl class and cannot be overridden. It does not directly handle messages, but will handle the WndProc method and provide an exception processing module for the WndProc method.
The mainwndproc method is declared as follows: Procedure mainwndproc (var message: tMessage); WNDPROC is a virtual method defined in the TControl class, which is called the Dispatch method for messages. The WndProc method is declared as follows: Procedure WndProc (var message: tMessage) The display is defined in the TOBJECT root class, which is as follows: procedure tobject.dispatch (var message); the message parameter passed to Dispatch must be a record type, and the first entry in this record must be A Cardinal Type Domain (Field) that contains the message number of the message to be assigned. For example: Type tMessage = Record Msg: cardinal; wparam: word; lparam: longint; result: longint; end; and Dispatch method The handle method of this message is handled in the final segment of the message number calling component. If this component and its ancestral class do not have the handle of this message, the Dispatch method calls the defaulthandler method. DEFAULTHANDLER method is defined in Tobject Virtual method, the declaration is as follows: Procedure defaultHandler (VAR message); Virtual; The defaultLer method in the Tobject class is just a simple return without any processing of the message. We can use the overload of this virtual method, in the subclass Implementation of the default processing of the message. For the components in the VCL, its DefaultHandler method launches the Windows API function DEFWindowProc to process the message. Er. Delphi Interest handler in Delphi users can customize the message and message handle. The definition of the message handle handle has the following principles: Message handler method must be a process and can only pass a TMESSAGE variable parameter. Method declaration There is a message command, then one after the message label (integer constant) between 0 and 32767. Message handle method does not need to explicitly indicate a message handling handle with the override command, and it is generally declared Component's Protected or Private area. In the message processing handle, it is generally the processing of the user's own handles, and finally use the inherited command to call the processed handle of this message (in some cases may be in the case). The name and parameter type of the handle of this message are unclear, and the call command inherited can avoid this trouble, and if there is no handle of this message in the ancestral class, inherited will automatically call the defaulthandler method. (Of course, if you want to block the off This message does not have inherited commands).
Message handle method declaration is: Procedure: TMESSAGE; Message MsgType; the same user can define its own message, user-defined message should start from WM_USER. Custom message and message handle handle example: const my_paint = WM_USER 1; type Tmypaint = record msgid: cardinal; msize: word; mcolor: longint; msgresult: longint; end; type Tmycontrol = class (TCustomControl) protected procedure change (var message: Tmypaint); message my_paint; ..... End; ... procedure: tmypaint; begin size: = message.msize; {Set TMYBUTTON size attribute} color: = message.mcolor; {setting TMYBUTTON color attribute} {do Something else } inherited; {{TCUSTOMCONTROL processing} END; III. Filter message filtering message is also known as a message trap. Under a certain situation, users can need to screen some messages. Or intercepting some messages to proceed. It can be seen from the above description that the filtering message generally has three ways: (1). Virtual method WndProc. (2). Written a message processing handle for a message. (3). DEFHANDALER inherited In these, the message is processed. The method commonly used in it is the method (2), which has been introduced in the previous section, and the method (1) is similar to the method (3), which is only a simple to introduce a next method (1). The general process of heavy-duty virtual method WndProc is as follows: Procedure TMYObject.WndProc (var message: tMessage); begin {... Judging whether this message is processed ..} Inherited WndProc (Message); {Unpreproving message Method handling} END; It can be seen that the advantage of processing messages in the WndProc method is that the message over the range can be filtered throughout the range, without having to specify a handle for each message. In fact, in fact the TControl component uses it to filter and process All mouse messages (from WM_Mousefirst to WM_Mouselast, as follows: The following code). It also uses it or prevents certain messages from being sent to the handle.