3. Publish Pen and Brush
 
By default, a canvas has a thin, black pen, and solid white brush that enables users to change the properties of canvas when using shape control, and must be able to provide these objects at design time, and then use these objects when drawing. Such an attached pen or brush is called a owned object.
 
The following three steps are required to manage owned objects:
 
Declaring an Object field
 
Declaring access Properties
 
Initializing the Owned object
 
⑴ declare owned object fields
 
Each object you own must have a declaration of an object field that always points to the owned object when the part exists. Typically, a part creates it in constructor and undoes it in destructor.
 
The domain of the owned object is always defined as private, and you typically provide access properties if you want the user or other part to access the domain.
 
The following code declares the object fields for pen and brush:
 
Type
 
Tsampleshape=class (Tgraphiccontrol)
 
Private
 
Fpen:tpen;
 
Fbrush:tbrush;
 
End
 
⑵ Declaration Access Property
 
You can provide access to owned objects by declaring properties of the same type as the owned object. This gives the developer who uses the part the means to access the object at design time or at runtime.
 
The following provides a way to access pen and brush to Shape control
 
Type
 
Tsampleshape=class (Tgraphiccontrol)
 
Private
 
Procedure Setbrush (Value:tbrush);
 
Procedure Setpen (Value:tpen);
 
Published
 
Property Brush:tbrush read Fbrush write Setbrush;
 
Property Pen:tpen read Fpen write Setpen;
 
End
 
Then write the Setbrush and Setpen methods in the implementation section of the Library unit:
 
Procedure Tsampleshape.setbrush (Value:tbrush);
 
Begin
 
Fbrush.assign (Value);
 
End
 
Procedure Tsampleshape.setpen (Value:tpen);
 
Begin
 
Fpen.assign (Value);
 
End
 
⑶ Initialize owned Object
 
The new object that is added in the part must be established in the part constructor so that the user can interact with the object at run time. Accordingly, the destructor of a part must undo the owned object before undoing itself.
 
Because pen and brush objects are added to shape control, they are initialized in constructor, and they are undone in destructor.
 
① creates pen and brush in the constructor of Shape control
 
Constructor Tsampleshape.create (aowner:tcomponent);
 
Begin
 
Inherited Create (Aowner);
 
Width: = 65;
 
Height: = 65;
 
Fpen: = tpen.create;
 
Fbrush: = tbrush.create;
 
End
 
② overrides destructor in the declaration of a Part object
 
Type
 
Tsampleshape=class (Tgraphiccontrol)
 
Public
 
Construstor. Create (aowner:tcomponent); Override
 
Destructor.destroy; Override
 
End
 
③ a new destructor in the implementation section of the Library unit
 
destructor Tsampleshape.destroy;
 
Begin
 
Fpen.free;
 
Fbrush.free;
 
inherited destroy;
 
End