Basic ways to write controls in Delphi

zhaozj2021-02-17  32

Basic ways to write controls in Delphi

As a Delphi programmer, it is necessary to further improve the programming level, you must master the preparation method of the control. This article will introduce some basic methods and patterns of the preparation of the preparation of the initiator through a simple example.

This example control is called TLEiLabel, which is to add two practical functions on the basis of TLabel: First, make the text with a stereo shape, and the other is to make the text have hyperlink attributes. Let's take a step by step to implement these features.

First, make the text have stereo shapes

First define type T3deffect and attribute Style3D as follows:

T3Deffect = (Normal, raised, lowered, shadowed);

Property Style3D: T3deffect Read FStyle3d Write SetStyle3d Default Normal

The variable is defined in private: "FStyle3D: T3deffect;" and sets the setStyle3D () method as follows, which is also a general format of writing methods:

Procedure Tleilabel.setStyle3D (Value: T3deffect);

Begin

IF fStyle3D <> Value Then

Begin

FStyle3d: = Value;

Invalidate; // means the control will be redrawn

END;

END;

In addition to the text of the shadow, the shadow is also defined Shadexoffset and ShadeyOffset:

Property ShadowxOffset: Integer Read FXOffset Write SetFxOffset Default 5;

Property Shadowyoffset: Integer Read Fyoffset Write SetFyoffset Default -5;

Write the method setFxOffset (), setFyOffset (), and SETSTYLE3D () above.

To redraw control, you generally have to overload the Paint method. Here is just heavy graphics, we only need to overload the DodrawText method.

DodrawText's statement is placed in protected:

Procedure DodrawText (Var Rect: Treat; Flags: longint); OVERRIDE;

Here DodrawText () draws different texts according to four types (normal, projections, recesses and shadows).

Second, make the text have hyperlink attributes

Define an attribute URL indicates the URL or Email address to be linked.

Property URL: String Read Furl Write SetURL;

Write the method seturl as follows:

Procedure tleilabel.seturl (value: string);

Begin

IF furl <> value kilure: = value;

IF furl <> '' THEN

Cursor: = crhandpoint;

END;

When you click this Label to open a browser or send and receive a mail tool, you have to overload the Click method.

Procedure Click; Override;

Procedure tleilabel.click;

Var s: string;

Begin

Inherited Click;

If FURL = '' Then EXIT;

IF LowerCase (Copy (FURL, 1, 7)) = 'http: //' Then

S: = FURL

Else IF POS ('@', furs) <> 0 THENS: = 'Mailto:' FURL

Else

S: = 'http://' furl;

Shellexecute (Application.handle, 'Open', PCHAR (S), NIL, NIL, SW_SHOWNORMAL);

END;

The general hyperlink is changed when the mouse is moved into the color. To this end, attribute HoverColor, indicating the color of the text when the mouse is moved.

Property HoverColor: Tcolor Read FhoverColor Write SthoverColor Default Clred;

Also defined two processes that receive Windows messages cm_mouseenter and cm_mouseeleave (mouse movement and removal):

Procedure CmmouseEnter (Var Message: TMESSAGE); Message Cm_MouseEnter;

Procedure CmmouseLeave (Var Message: TMESSAGE); Message Cm_MouseLeave;

Procedure tleilabel.cmmouseenter (Var message: tMessage);

Begin

IF enabled and visible and getparentform (self) .active then

Begin

FFONTCOLOR: = font.color;

// Save the color of the text

Font.color: = fhovercolor;

// change text color

END;

END;

Procedure Tleilabel.cmmouseLeave (Var Message: TMESSAGE);

Begin

Font.color: = ffontcolor;

// Restore the original color

END;

To set the default value of the properties, we also need to overload construct Create (), note, must first call the ancestry class. The procedure for heavy duty constructs CREATE () is as follows:

Constructor Tleilabel.create (Aowner: Tcomponent);

Begin

Inherited Create (Aowner);

// must first call the ancestral structure

FStyle3D: = Normal;

FXOFFSET: = 5;

Fyoffset: = -5;

FHOVERCOLOR: = CLRED;

END;

Finally, you must also add icon for this control. We can create a 24 × 24 Bitmap bitmap with Image Editor in Delphi, saved as a DCR file, the file name must be the same as the control's PAS file name, the bitmap name must be the same as the control name and all capitalize.

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

New Post(0)