19th Chapter-delphi Custom Parts Development (IV.) (3)

Source: Internet
Author: User
Tags access properties constructor

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.