Message process mechanism in Delphi

zhaozj2021-02-08  225

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. Delivery of the message in Delphi VCL

Each VCL (Visual Component Library) component (such as TButton, Tedit, etc.) has an intrinsic message processing mechanism. The basic point is that the component class receives some messages and sends them to the appropriate processing method, if not specific The method of processing, then call the default message handling handle.

MAINWNDPROC is a static method defining in the TwinControl class and cannot be reloaded. It does not directly process information, but is handled by the WndProc method, and provides a different constant treatment module 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. It is called the Dispatch method to enter the message, 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); message parameters passing to Dispatch must be a record type, and the first entry in this record must be a Cardinal type domain (Field), which contains messages to be assigned Number. For example:

Type

TMessage = Record

Msg: cardinal;

WPARAM: WORD;

LPARAM: longint;.

Result: longint;

END;

The Dispatch method handles the handle method of this message according to the last transcript of the message number calling component. If this component does not correspond to the handle of this message, the Dispatch method will call the DEFAULTHANDLER method. DEFAULTHANDLER method is The virtual method defined in TOBJECT 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

Windows API Function DefWindowProc proactive.

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:

1. Message handle method must be a process and can only pass a TMESSAGE type variable parameter. 2. Method declaration has a message command, then taken a message mark (integer constant) between 0 and 32767. 3. Message handle method does not need to explicitly indicate a message handling handle with an override command, and it is generally declared in the PROTECTED or Private area of ​​the component. 4. In the message handling handle, it is generally the user's own message. Processing, finally call the processing handle of this message in the ancestral class with the inherited command (in some cases may be reversed). Since the name and parameter type of the handle of the message may be unclear, the call command inherited can be Avoid this trouble, and if there is no handle corresponding to this message in the ancestral class, 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 their own messages, and user-defined messages should be started from WM_USER.

Customized messaging and yet 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 Dimensions} Color: = message.mcolor; {Settings TMYBUTTON Color Properties}

{Do Something else}

Inherited; {交 交 TCUSTOMCONTROL processing}

END;

3. Filter message

Filter messages are also known as a message trap. Under a certain situation, users can need to screen some messages. Or intercepting some messages to proceed. From the above introduction, there are three types of paths that can be seen by filtering messages: (1). The virtual method of the reloading member is the virtual method WndProc. (2). Needle

Message preparation message handles. (3). The virtual method of the reunion of the reloading member defhandler is processed in it. 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 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 that you can filter the entire range, without having to specify a handle for each message, in fact, in fact, using it to filter and process all mouse messages (

From WM_MouseFirst to WM_MouseLast, as shown below code). It is also possible to use it to block some messages to be sent to the proceed 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, and cannot be entered by the keyboard (a bit similar to the TLABEL component). We can filter out WM_SETFOCUS, WM_MOUSEMOVE messages in its WndProc method, Treatment to achieve the above

To seek, the source is as follows:

Unit myedit;

Interface

Uses

Windows, Messages, Sysutils, Classes, Graphics,

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 as Crarrow, not 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 known, master the method and timing of various messages (if necessary, by means of various tools, such as Winsight32, SPY, etc.), and combine the OOP language characteristics We may have high quality components. This is the case that the reader is not disconnected in practice.

Nanjing University of Science and Technology Self-control Department 96 Ma Yong

转载请注明原文地址:https://www.9cbs.com/read-1284.html

New Post(0)