19th Chapter-delphi Custom Parts Development (II.) (2)

Source: Internet
Author: User
Tags constructor

⑸ Default Property value

When declaring a property, you can selectively declare a property's default value. The default value for a part property is the set of property values in the part constructor. For example, when you select a part from Component palette to place it in a form, Delphi creates the part by calling the part constructor and determines the part property initial value.

Delphi uses a declarative default value to determine whether the attribute value exists in the DFM file. If you do not describe the default value, Delphi will always save the property value. The way to declare a default value is to add a default directive after the property declaration, followed by the default value.

When you re declaring a property, you can describe an attribute that has no default value. If an inherited property already has one, the method to set up an attribute with no default value is to add the nodefault instruction after the property declaration. If you are declaring a property for the first time, there is no need to add the NoDefault directive, since there is no default directive that means so.

The following example is a procedure named IsTrue that sets the default value True for a Boolean type property:

Type

Tsamplecomponent=class (tcomponent)

Private

Fisatrue:boolean;

Pubilic

Constructor Create (aowner:tcomponent); Overvide;

Published

Property Istrue:boolean Read Fistrue write fistrue default True;

End

Constructor Tsamplecomponent.create (aowner:tcomponent);

Begin

Inherited Create (Aowner);

Fistvue: = True; {Set default value}

End

5. Write the Property editor

Object Inspector provides the default editor for all types of attributes, and Delphi also supports designing your own editor for properties by writing and registering the property editor. You can register an editor that is designed specifically for the properties of a custom part, and you can design properties for all types. The following five steps are required to write the property editor:

Inherit a property editor object

Edit the property as text

Edit attributes as a whole

Description Editor Properties

Registering the Property editor

⑴ Inheritance Property Editor Object

Several property editors are defined in the DSGNINTF Library unit. They are all inherited from the tpropertyeditor. When you create the property editor, you can inherit from Tpropertyeditor directly or from any of the property editors in the table.

Table 19.4 Types of property editors

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Type-edited properties

─────────────────────────────────────

Tordinalproperty all ordered attributes (integers, characters, enumerations)

Tintegerproperty all integers, including child boundary types

Tcharproperty character type or subset of characters

Tenumproperty Any enumeration type

Tfloatproperty all floating point numbers

Tstringproperty strings, including fixed-length strings

Independent elements in the Tsetelementproperty collection

Tsetelementproperty all collections, not directly editing the collection type, but expanding into a column

Collection element Properties

Tclassproperty objects, displaying object names, and allowing the expansion of object properties

Tmethodpropevty method pointer, mainly refers to the event

Tcomponentproperty a part in the same form, the user cannot edit the properties of the part.

But it can point to compatible parts.

Tcolorproperty part color, display color constants, or display hexadecimal numbers

Tfontnameproperty Font Name

Tfontproperty font, allowing expanded font properties or pop-up Fonts dialog box

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The following is the definition of tfloatpropertyeditor:

Type

Tfloatproperty=class (Tpropertyeditor)

Public

function Allequal:boolean; Override

function getvalue:string; Override

Procedure SetValue (Const value:string); Override

End

⑵ Edit attributes like text

All properties need to display their values as text in the Object Inspector window. The property Editor object provides a virtual method for converting between text representation and actual values. These virtual methods are GetValue and SetValue, and your property editor can also inherit a series of methods for reading and writing different types of values. See table below:

Table 19.5 methods for reading and writing property values

━━━━━━━━━━━━━━━━━━━━━━━━━━

Property type ' Get ' method ' Set ' method

──────────────────────────

Floating-point number Getfloatvalue Setfloatvallue

Method Pointer Getmethodvalue Setmehodvalue

Ordered type Getordvalue setordvalue

String Getstrvalue Setstrvalue

━━━━━━━━━━━━━━━━━━━━━━━━━━

When the GetValue method is overridden, a "get" method is invoked, and a "Set" method is invoked when the SetValue method is overwritten.

The GetValue method of the property editor returns a string to represent the current property value. GetValue returns "Unknown" by default.

The SetValue of the property editor receives the parameters of the Object Inspector window string, converts them to the appropriate type, and sets the property value.

The following are examples of Tintegerproperty's GetValue and SetValue:

function Tintegerproperty getvalue:string;

Begin

Result: = IntToStr (Getordvalue);

End

Proceduve Tintegerpropertysetvalue (Const value:string);

Var

L:longint;

Begin

L: = Strtoint (Value); {Convert string to math}

With Gettypedata (getproptype) ^ do

if (L < MinValue) or (L > MaxValue) Then

Raise epropertyerror.create (Fmtloadstr (Soutofrange,

[Minvalue,maxvalue]));

Setordvalue (L);

End

⑶ to edit attributes as a whole

Delphi supports providing users with a dialog box to visually edit properties. This situation is often used for editing object type properties. A typical example is the Font property, which allows the user to find a Font dialog box to select the properties of the fonts.

Provides the overall Properties Edit dialog box, which overrides the edit method for the property editing object. The Edit method also uses the "Get" and "Set" methods.

The color property used in most parts uses the standard Windows Color dialog box as the property editor. The following is the Tcolorproperty edit method

Procedure Tcolorproperty.edit

Var

Colordialog:tcolordialog;

Begin

ColorDialog: = tcolordialog.create (application); {Create Editor}

Try

Colordialog.color: = Getordvalue; {Using an existing value}

If Colordialog.execute Then

Setordvalue (Colordialog.color);

Finally

Colordialog.free;

End

End

⑷ Description Editor Properties

Property editors must tell the Object inspector window how to use the appropriate display tool. For example, the Object inspector window needs to know whether a property has child attributes or if it can display a list of possible values. The properties that describe the editor typically override the GetAttributes method of the property editor.

GetAttributes returns a collection of tpropertyattributes types. The collection includes any or all of the values in the table:

Table 19.6 Property Editor feature Flags

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Related methods of flag meaning

──────────────────────────────

The Pavaluelist editor can give a set of enumerated values GetValues

The Pasubpropertie property has child attributes Getpropertises

Padialog Editor can display Edit dialog box editor

Pamultiselect when the user chooses more than one part

Property should be able to display N/A

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The Color property is flexible and allows you to select them in a variety of ways in the Object Inspector window. Either type, or select the Set editor from the list. Therefore, the Tcolorproperty GetAttributes method contains a number of attributes in the return value.

function TColorProperty.GetAttributes:TProrertyAttributes;

Begin

Result: = [Pamultiselect, Padialog, pavaluelist];

End

⑸ Registration Property Editor

Once you have created the property editor, you must register in Delphi. When registering the property editor, you want to associate with a property.

Call the Registerpropertyeditor procedure to register the property editor. This procedure accepts four parameters:

A pointer to the type information of the property to edit. This is always obtained by calling the TypeInfo function, such as TypeInfo (Tmycomponent)

The type of part applied by the editor, if the parameter is nil the editor applies to all properties of the given type

Property name, which is available only if the previous parameter describes the part

Types of properties that use the property editor

The following refers to the process of registering a standard part:

Procedure Register;

Begin

Registerpropertyeditor (TypeInfo (tcomponent), nil, Tcomponentproperty,

Registerpropertyeditor (TypeInfo (tcomponentname), Tcomponent,

' Name ', (componentnamepropety);

Registerpropertyeditor (TypeInfo (Tmenuitem), Tmenu, ', Tmenuitemproperty ');

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.