Delphi is a new Windows programming development tool provided by Borland. Since it uses elastic and reused object-oriented Pascal (Object-Oriented Pascal) and has a powerful database engine (BDE) fast code compiler At the same time, there are many excellent components. It is favored by the broad masses of programming people. In numerous programming languages (such as VB Powerbuilder PowerPoint et al.). One of the Delphi is in other programming languages (such as VB4.0) is in Delphi. Customize messages can be customized directly. This is essential for those who wish to write their own components or want to intercept. Filter messages are essential. Because the write components generally process the corresponding messages. The message processing mechanism in Delphi is described below.
One. Delivery of the message in Delphi VCL
VCL (Visual Component Library) components in Delphi (such as TButton)
Tedit, etc.) has an inner job handling machine. The basic point is that the component class receives some messages and sends them to the appropriate prescription method.
If there is no specific timing method, the default information will be invoked. MAINWNDPROC is a static method defined in the TwinControl class, which cannot be reloaded (Override). It does not directly process information, but is handled by the WndProc method, and provides a different constant treatment module for the WndProc method. MAINWNDPROC method is as follows: Procedure mainwndproc (var message: tMessage); WNDPROC is a virtual method for defining in the TControl class
Distribution of the DISPATCH method
The WndProc method is as follows:
Procedure WndProc (Var Message: tMessage); Virtual;
The Dispatch method is defined in the TOBJECT root class.
It is as follows:
Procedure TOBJECT.DISPATCH (VAR Message); message parameters handle to Dispatch must be a record type
And the first entry in this record must be a Cardinal Type Domain (Field)
It contains the message number of the message to be assigned. Example: Type
TMessage = Record
Msg: cardinal;
WPARAM: WORD;
LPARAM: longint;.
Result: longint;
END;
The DISPATCH method will handle the handle method of this message according to the final resection of the message number calling member. If this part is, there is no handle of this message in the ancestral class.
The Dispatch method will call the defaulthandler method. DEFAULTHANDLER method is the virtual method for definition in TOBJECT
It is as follows:
Procedure defaulthandler (var message); virtual;
The DEFAULTHANDLER method in the TOBJECT class is just a manner that is now simply returned without the processing of messages. We can realize the default processing of messages in subclasses through heavy loads on this virtual method. For information on the VCL
Its DefaultHandler method will start the Windows API function DEFWINDOWPROC processes the message.
Two. Delphi's message handling handle
Households in Delphi can handle handle with a self-defined message and a message. The definition of the message handling handle has the following principles:
The message handler method must be a process and can only pass a TMESSAGE variable parameter. The method declares that there is a message mark (integer constant) between 0 and 32767 after the method declaration. Message handle method does not need Use the override command to explicitly indicate a message handle of the heavy load ancestors. In addition, it is generally declared in the PROTECTED or Private area of the component. In the message handle, it is generally the user's own handles the message to call the ancestral class with the inherited command. The handle of the message (in some cases may be reverse). Due to the name and parameter type of the handle of this message may not be clear because the ancestral class may be unclear, I will avoid this, if the ancestral class does not correspond to this message Processing the handle inherited will automatically call the defaulthandler method. (Of course, if you want to block this message, you don't have inherited commands). The message handling handle is:
Procedure mymsgmethod (var message: tMessage); Message MsgType;
Similar users can also define job-free messages to start from WM_USER. Customized message and message handling handle For 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 TMYCONTROL.CHANGE (VAR message: TMYPAINT);
Begin
Size: = message.msize; {Settings TMYBUTTON Dimenshen Attribute}
Color: = message.mcolor; {Settings TMYBUTTON color properties}
{Do Something else}
Inherited; {交 交 TCUSTOMCONTROL processing}
END;
3. Filter message
Filter messages are also known as the 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 that the method (2) has been introduced in the previous section, and the method (1) is similar to the method (3), which is only a simple introduction to the next method (1). The general procedure of the virtual method WNDPROC is as follows: Procedure TmyObject.WndProc (Var Message: TMessage);
Begin
{... Judging this message is whether it is ..}
Inherited WNDPROC (Message);
{Unprepared messages handled by the father from WndProc Method}
END; It can be seen that the advantage of processing messages in the WndProc method is to filter the entire range, without having to specify a handle for each message. In fact, in the TCONTROL component, it is to filter and process all mouse. The message (from WM_Mousefirst to WM_Mouselast, as shown below). It is also used to block some messages from being sent to the handle.
Procedure Tcontrol.WndProc (Var Message: TMessage);
Begin
IF (Message.msg> = WM_Mousefirst) and
(Message.msg <= WM_Mouselast)
THEN
IF Dragging the {Treatment Draging Event}
DragMousemsg (TwMMouse (Message))
Else
... {Treating his mouse message}
END;
Dispatch (Message);
{Otherwise, send a message}
END;
The next example is a simple self-defined component example:
The TMYEDIT class is a new class that is born from the TEDIT class. Its feature is that the focus cannot be obtained in the run, cannot be entered by the keyboard (a bit similar to the TLABEL component). We can filter out the WM_SETFOCUS WM_MOUSEMOVE message in its WndProc method and processes To reach the above requirements, the source procedures are as follows:
Unit myedit;
Interface
Uses
Windows
Messages
Sysutils
Classesgraphics
CONTROLS
Forms
Dialogs
STDCTRLS;
Type
TMYEDIT = Class (TEDIT)
Private {private declarations}
protected {protected declarations}
{Other Fields and Methods}
Procedure WndProc (var message: tMessage; OVERRIDE;
PUBLIC {public declarations}
Published {Published Declarations} END;
PROCEDURE register;
IMPLEMENTATION
PROCEDURE register;
Begin
RegisterComponents ('Samples' [TMYEDIT]);
END;
Procedure TmyEdit.wndProc (Var Message: TMESSAGE);
Begin
IF message.msg = wm_mousemove dam
Begin
Cursor: = Crarrow;
{Set the cursor for Crarrow instead of the default CrBeam cursor}
EXIT;
END;
If Message.msg = WM_SETFOCUS THEN EXIT;
{Shield off WM_SETFOCUS message
Do not allow TMYEDIT controls to get input focus}
Inherited WNDPROC (Message);
{Other news father WNDPROC processing}
END;
End.
You can use TMYEDIT to check its performance in Component Palette. As can be seen from the above, it can be seen that only the message processing mechanism in the Delphi VCL is clear, master the method and timing of processing various messages (if necessary, using various tools, such as Winsight32SPY, etc.), combined with the OOP language, we It is possible to compose high quality components. This is the case that the reader is not disconnected in practice.