Delphi's attribute is a very distinctive feature, this feature has been well inherited in C #. For a class, the property is a special interface for efficient modification and access to the data in the class. In VCL, there is also a feature of the properties of Published, which is to set the properties in the interface design process. Type of the property, characters, Boolean, enumeration, TSTRINGS, collections, class objects or interface types: TenumTest = (ETNONE, ETTESTINT, ETTestString); TTESTCONTROL = Class (TCUSTOMCONTROL) Private FINT: Integer; FenumTest: TenumTest; fcaption: string;
Function GetInt: Integer; FUNTION GETENUMTEST: TENUMTEST; FUNCTION CAPTION: STRING
procedure SetCatpion (AValue: String); procedure SetInt (AValue: Integer); procedure SetEnumTest (AValue: String); published property IntProp: Integer read GetInt write SetInt; property EnumTest: TEnumTest read GetEnumTest write SetEnumTest; property Caption: String read FCaption write SetCaption; End; Read and Write instructions Specify an effective way to access properties, and access to the property can be implemented in these methods.
The most important thing to write VCL controls is that attributes are written, the property editor is also between it. The property editor generally has a number, string, collection, class, color, font type editor, Delphi built-in basic editor declares that all attribute editors with TPROPERTYEDITOR must inherit this or his derived TordinalProperty is a few ordered base class property editor, as applicable TIntegerProperty TCharProperty TEnumPropertyTIntegerProperty int property editor to edit the character attribute applies TCharProperty TEnumProperty apply edit enumerated type properties TBoolProperty apply edit Boolean property TFloatProperty apply edit float edit string attribute applies TStringProperty Property TCLASSPROPERTY Applicable Edit Object Type TComponentProperty Use Edit Reference A Component Properties TSETPROPERTY Applicable Edit Collection Type The above can view DesignETORS.PAS unit (Delphi7) For some basic data types Sometimes, you do not need to create a property editor, because the data type itself also data Check, when you have a special check, you can send your own properties editor from the above basic editor.
Dialog Box Property Editor's Writing Defines a dialog interface that is editing the character type property, put a MEMO on the interface, a Label, and the three button code Unit UStringeditor;
Interface
Uses Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, Buttons, Designintf, DesignediTors;
type // genus dialog window class TMyStringEditDlg = class (TForm) Memo: TMemo; OkButton: TBitBtn; CancelButton: TBitBtn; Label1: TLabel; BitBtn1: TBitBtn; procedure MemoKeyDown (Sender: TObject; var Key: Word; Shift: TShiftState ); procedure FormDestroy (Sender: TObject); procedure FormShow (Sender: TObject); procedure UpdateStatus (Sender: TObject); procedure BitBtn1Click (Sender: TObject); private {Private declarations} FModified: Boolean; protected function getLines: TStrings; procedure SetLines (const Value: TStrings); function GetLinesControl: TWinControl; public {public declarations} property Lines: TStrings read getLines write SetLines; end; // properties editor dialog TMyStringProperty = class (TStringProperty) protected function EditDialog: TMyStringEditDlg; virtual; Public procedure edit; override; function GetAttributes: tpropertyAttributes; override;
Var mystringeditdlg: TMYSTRINGEDITDLG;
Implementation {$ r * .dfm} const //v1.73 19/08/2000) Copyrightstr: pchar = 'String Editor V1.0 (12/06/2003)' # 13 # 13 'Created by Donson Wan '# 13 # 13 ' Compiled in ' {$ IFDEF VER80}' Delphi 1.0 '{$ ENDIF} {$ IFDEF VER90}' Delphi 2.0 '{$ ENDIF} {$ IFDEF VER100}' Delphi 3.0 '{$ ENDIF } {$ IFDEF VER120} 'Delphi 4.0' {$ ENDIF} {$ IFDEF VER130} 'Delphi 5.0' {$ ENDIF} {$ IFDEF VER140} 'Delphi 6.0' {$ ENDIF} {$ IFDEF VER150} 'Delphi 7.0' { $ ENDIF} {$ IFDEF VER93} 'C Builder 1.0' {$ ENDIF} {$ IFDEF VER110} 'C Builder 3.0' {$ ENDIF} {$ IFDEF VER125} 'C Builder 4.0' {$ ENDIF }; var StoredWidth, StoredHeight, StoredLeft, Storedtop: Integer;
Sign; need
Function TMYSTRINGEDITDLG.GETLINSCONTROL: TWINCONTROL; Begin
END;
Procedure TMYSTRINGEDITDLG.MEMOKEYDOWN (Sender: Tobject; var key: word; shift: tshiftstate); begin if key = vk_escape dam / c;
Procedure TMYSTRINGEDITDLG.SETLINES (Const Value: Tstrings); Begin Inherited; Memo.lines.Assign (Value);
Procedure TMYSTRINGEDITDLG.FORMDESTROY (Sender: Tobject); Begin StoredWidth: = Width; Storedheight: = Height; Storedleft: = Left; StoredTop: = Top; End;
procedure TMyStringEditDlg.FormShow (Sender: TObject); begin if StoredWidth <> 0 then Width: = StoredWidth; if StoredHeight <> 0 then Height: = StoredHeight; if StoredLeft <> 0 then Left: = StoredLeft else Left: = (Screen. Width - Width) div 2; if StoredTop <> 0 then Top: = StoredTop else Top: = (screen.Height - Height) div 2; end; procedure TMyStringEditDlg.UpdateStatus (Sender: TObject); begin if Sender = Memo then FModified : = True; end; {TMYSTRINGPROPERTY}
procedure TMyStringProperty.Edit; begin inherited; with EditDialog do try Lines.Text: = GetValue; FModified: = False; UpdateStatus (nil); // ActiveControl: = GetLinesControl; case ShowModal of mrOk: begin SetValue (Lines.Text); / /Designer.modified; end; end;
Function TMYSTRINGPROPERTY.EDITDIALOG: TMYSTRINGEDITDLG; Begin Result: = TMYSTRINGEDITDLG.CREATE (Application);
Function TMYSTRINGPROPERTY.GETATTRIBUTES: TPROPERTYATTRIBUTES; Begin Result: = Inherited GetAttributes [Padialog] - [PASUBPROPERTIES]; END;
Procedure TMYSTRINGEDITDLG.BITBTN1CLICK (Sender: TOBJECT); Begin Messagedlg (StrPyrightstr), Mtinformation, [Mbok], 0);
END;
End.
You can use the above editor to register to the TTestControl.caption property Registration Property Editor's declaration as registerPropertyEditor (PropertyType: ptypeinfo; componentclass: tclass; const materialTyName: string; editorclass: tpropertyEditorClass;
RegisterPropertyEditor (TypeInfo (String), TTestControl, 'CAPTION', TMYSTRINGPROPERTY; if the registration is successful, the parameter 1 is TypeInfo (String) is the acquisition attribute runtime property type information, the parameter 2 is the specified registered control object, if not specified controls Objects are registered to Delphi all controls, so that you pull a label on the interface, you can't write multi-wire text, now you can lose, now you can lose in the pop-up attribute editor, you can arbitrarily :) Parameters 3 is Specifying a registration attribute name parameter 4 is the property editor class name you need to register. You can also add a category categoryName to your registered property. We specify the above CAPTION property as a MycateGory type property, registration example,
Next: RegisterPropertiesInsIncategory ('Mycategory', TTestControl, ['CAPTION']);