Through examples to see the entire process of developing the VCL component (four)

zhaozj2021-02-16  55

(Connected to above)

Fourth, component attribute editor and component editor:

Through the effort above our components, our components seem to be more perfect, but we also ignore some important details and some interesting things, this one we will study two very useful component features:

When we develop component core functions, we have set up two properties BeGintime and Waketime, they are all characterized properties, but they want to indicate the time type, so it is very likely to make component user error editing properties. And causing the transformation string to time error (of course, this is just the explanation of the article, we deliberately set it to string type), although by browsing the original code, you know that we have also made some code level anti-wrong processing, make it When you enter an error, the property is automatically turned to '00: 00: 00 ', however this is still very unfriend of the component user, so we need to customize the editor for these two attributes, and our editor will pop up a window. There is a TDATETIMEPICKER inside to select time. There are many examples in Delphi, such as everyone knows the Lines property, when you click on the omitted number of omnisions to edit LINES, this greatly reduces the possibility of component user models .

After customizing the property editor, we will join a functural element-component editor for the component itself, which is often in Delphi, such as some components, when you double-click it, it will not enter the code to write Status, but eject its own editor. Although our components don't seem to need this feature, in order to demonstrate it, we also consider it, we have written a copyright information and a dialog box to our components, and a component user doubles it when the information (Of course, this is just a species). The two features mentioned above are only written in the new component package, so you can set this component package to Designtime Only in the new component package, and set this component package to Designtime ONLY. For convenience, we will directly put them directly Write together with the unit of the component. Note: Some classes and methods that appear below require the reference unit Designeditors (Delphi7) or DSgnintf (Delphi5), as mentioned earlier, they all belong to the Open Tools API of Delphi, so if you don't have this write unit, please follow the foregoing method Install them.

First, write the property editor, because Begintime and Waketime are string types, we must inherit from the default string property editor class TStringProperty and override it (here only introduces a few important methods, in fact all The attribute editor is inherited from TPropertyEditor, but we don't have to directly inherit this base class). One of the important methods is getAttribute, and he will return some value representing the editor function, which will be explained in the code of the code (if your property editor also needs a drop-down list, you also need another important method GetValues ​​Specifically Check Delphi Help) In addition to enable the Property Editor to pop-up, we need to overwrite the Edit method. In order to be able to design a visualization, we can create a normal engineering, copy the form of the form to our component unit after design, and copy the DFM file of the form to our component package directory, and Add the compiler switch {$ r * .dfm} in the code. The following is a class declaration of the form, this form does not have any code to be written:

TTIMEEDITFRM = Class (TFORM)

DateTimePicker1: tdatetimepicker;

Button1: tbutton; button2: tbutton;

Private

{Private Declarations}

public

{Public declarations}

END;

The following is the code of the property editor:

TclockProperty = Class (TstringProperty)

public

Function GetAttributes: tpropertyAttributes; Override;

Procedure edit; Override;

END;

Implementation part:

Procedure TclockProperty.edit;

VAR

TimeEditfrm: TTIMEEDITFRM;

Begin

TimeEditFRM: = TTIMEEDITFRM.CREATE (Application);

Try

TimeEditFrm.datetimePicker1.Time: = startIME (getValue);

if TimeEditfrm.ShowModal = MROK THEN

SetValue (TimeEditFrm.DatetimePicker1.Time);

// getValue and setValue are the base class of TStringProperty, and he directly reads and sets the value of strings.

Finally

TimeEditfrm.Free;

END;

END;

Function TclockProperty.getattributes: tpropertyAttribute;

Begin

Result: = [Padialog, PamultiTISELECT];

// Padialog indicates a dialog box that will display a dialog box, and the PamulitisElect allows multiple component selection properties.

/ / In addition to this, if you want the property editor to display the drop-down list, you also need PAVALUELIST to see the help.

END;

Finally, we use the RegisterPropertyEditor method to register attribute editor:

PROCEDURE register;

Begin

......

RegisterPropertyEditor (TypeInfo (String), Tclock, 'Begintime', TclockProperty;

RegisterPropertyEditor (TypeInfo (String), Tclock, 'Waketime', TclockProperty;

END;

After recompiling the update, we can test it, you can see the component editor works very well:

Next we come to implement the component editor:

Component Editor needs to inherit tComponEnteditor and overwrite some important methods, getverbcount returns the number of custom menus, getverb adds text for each custom menu, and EXECUTEVERB adds events for each menu item, and edit is the default for components. Operation specifies events (that is, double-click the component when design), the following is the code:

TclockEditor = Class (TcomponiteTeditor)

public

Function GETVERBCOUNT: Integer; Override;

Function GETVERB (INDEX: Integer): String; Override;

Procedure Executeverb (Index: Integer); OVERRIDE;

Procedure edit; Override;

END;

Implementation part:

Procedure tclockeditor.edit;

Begin

Executeverb (1); / / default display

END;

Procedure TclockEditor.executeverb (Index: Integer); Begin

Case Index of

// The first menu that displays the name does not display anything.

1: ShowMessage ('hk.barton@2003 ');

END;

END;

Function Tclockeditor.getverb (Index: Integer): String;

Begin

Case Index of

0: Result: = 'hk.barton';

1: Result: = 'About Clock';

END;

END;

Function TclockEditor.getverbcount: Integer;

Begin

Result: = 2; // We show two menus, one my name, one about

END;

The same last we registered component editor:

PROCEDURE register;

Begin

......

RegisterComponiteTereditor (Tclock, TclockEditor);

END;

You can also see the test situation of the component editor:

The article is written here. Although there are so many, this is still a small part of the content in the development of component development, this article is just a role of tiles, I hope some of the friends who are going to enter the component. In order to facilitate you to read this article, if you want this component to be developed this article, please contact me:

E-mail: hk.barton@sohu.com HEKPHI@hotmail.com QQ: 6813489

(Full text)

references:

Marco Cantu "Mastering Delphi"

转载请注明原文地址:https://www.9cbs.com/read-27352.html

New Post(0)