1. Decide what to draw
Graphic parts usually have the ability to change their appearance, and the appearance of graphic control depends on the combination of some of its properties, such as gauge control's ability to determine its shape, direction, and graphically display its processes. Similarly, shape control should have the ability to decide which shapes to display.
Give shape control of this ability and add properties called shape. This requires the following three steps:
Declaring property types
declaring properties
Writing implementation Methods
⑴ declaring property types
When declaring a property of a user-defined type, you must first declare the property type. The most common custom type used for attributes is the enumeration type.
For shape control, you need to declare an enumeration that controls the ability to draw shapes, and here is the declaration of the enumeration type:
Type
Tsampleshapetype= (Sstrectangle, Sstsquare, Sstroundrect,
Sstroundsquare, Sstellipse, sstcircle);
Tsampleshape = Class (Tgraphiccontrol)
End
This allows you to declare a property with that type.
⑵ Declaration Properties
When declaring a property, it is often necessary to declare a private domain to hold the property value, and then describe the method of reading and writing the property value.
For Shape control, a field is declared to hold the current shape, and then a property is declared to read and write the domain value by means of a method.
Type
Tsampleshape=class (Tgrahpiccontrol)
Private
Fshape:tsampleshapetype;
Procedure Setshape (Value:tsampleshapetype);
Published
Property Shape:tsampleshapetype read Fshape write Setshape;
End
Now, there is only the Setshape implementation part left.
⑶ Writing Implementation method
Here is the implementation of the Setshape:
Procedure Tsampleshape.setshape (Value:tsampleshapetype);
Begin
If Fshape<>value Then
Begin
Fshape: = value;
Invalidate (True); {Force new shape to repaint}
End
End
2. Covering constructor and destructor
In order to change the default property values and initialize the objects owned by the part, you need to overwrite the inherited constructor and destructor methods.
The default size of the graphics control is the same, so you need to change the width and Height properties.
In this case, the size of shape control is initially set to 65 pixel points of the edge length.
⑴ add overlay in part declarations constructor
Type
Tsampleshape=class (Tgraphiccontrol)
Public
Constructor Create (aowner:tcomponent); Override
End
⑵ the attribute height and width with the new default value
Type
Tsampleshape=class (Tgrahiccontrol)
Published
Property Height default 65;
Property Width default 65;
End
⑶ a new constructor in the implementation part of the library unit
Constructor Tsampleshape.create (aowner:tcomponent);
Begin
Inherited Create (Aowner);
Width: = 65;
Height: = 65;
End