Basic methods of writing controls in Delphi
As a Delphi programmer, if you want to further improve your programming level, you must master how to write controls. This article will introduce some basic methods and patterns of writing controls for beginners through a simple example.
This example control is called TLeiLabel, and it adds two practical functions on the basis of TLabel: one is to make the text have a three-dimensional shape, and the other is to make the text have a hyperlink attribute. Let us implement these functions step by step.
1. Make the text have a three-dimensional shape
First define the type T3DEffect and attribute Style3D as follows:
T3DEffect=(Normal, Raised, Lowered, Shadowed);
PRoperty Style3D:T3DEffect read FStyle3D write SetStyle3D default Normal;
Then define the variable in private: "FStyle3D:T3DEffect;" and set the SetStyle3D() method as follows. This is also the general format for writing methods:
procedure TLeiLabel.SetStyle3D(Value: T3DEffect);
begin
if FStyle3D <> value then
begin
FStyle3D := value;
invalidate; //Indicates that the control will be redrawn
end;
end;
In addition, for text with shadows, the offsets of the shadow, ShadeXOffSet and ShadeYOffSet, must be defined:
property ShadowXOffSet: integer read FXOffSet write SetFXOffSet default 5;
property ShadowYOffSet: integer read FYOffSet write SetFYOffSet default -5;
The writing methods SetFXOffSet() and SetFYOffSet() are similar to SetStyle3D() above.
To redraw a control, you generally need to overload the Paint method. Here we are just redrawing the text. We only need to overload the DoDrawText method.
The declaration of DoDrawText is placed in Protected:
procedure DoDrawText(var Rect: TRect; Flags: Longint); override;
Here DoDrawText() draws different text according to four types (normal, raised, recessed and shadow).
2. Make text have hyperlink attributes
Define an attribute URL to represent the URL or email address to be linked.
Property URL: String read FURL write SetURL;
Write method SetURL as follows:
procedure TLeiLabel.SetURL(Value: String);
Begin
if FURL <> Value then FURL := Value ;
if FURL <> ' then
Cursor := crHandPoint;
end;
When clicking this Label, you need to open a browser or a mail sending and receiving tool, which requires overloading 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('@',FURL) <> 0 Then
s := 'mailto:' + FURL
else
s := 'http://' + FURL;
ShellExecute(application.Handle, 'open', PChar(s), NIL, NIL, SW_SHOWNORMAL);
end;
For general hyperlinks, the color of the text changes when the mouse is moved into it. To this end, add the attribute HoverColor, which indicates the color of the text when the mouse moves in.
Property HoverColor : TColor Read FHoverColor Write SetHoverColor default clRed;
Also define two procedures that receive Windows messages CM_MOUSEENTER and CM_MOUSELEAVE (mouse in and out):
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 text color
Font.Color := FHoverColor;
//Change text color
End;
end;
Procedure TLeiLabel.CMMouseLeave (Var Message: TMessage);
begin
Font.Color:=FFontColor;
//Restore the original color of the text
end;
In order to set the default value of the attribute, we also need to overload the constructor Create(). Note that the constructor of the ancestor class must first be called when overloading the constructor. The program for overloading the Create() construct is as follows:
constructor TLeiLabel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
//The constructor of the ancestor class must be called first
FStyle3D := Normal;
FXOffSet := 5;
FYOffSet := -5;
FHoverColor := clRed;
end;
Finally, an icon must be added to this control. We can use the Image Editor in Delphi to create a 24×24 Bitmap bitmap and save it as a DCR file. The file name must be the same as the Pas file name of the control. The bitmap name must be the same as the control name and all capitalized.