2010-09-16 13 views
7

Por ejemplo, como Fuente. ¿Alguien puede dar un ejemplo muy simple? Tal vez sólo una propiedad con dos sub-propiedades¿Cómo codigo una propiedad con sub-propiedades?


Editar: quiero decir que cuando miro a la fuente en el inspector de objetos que tiene un pequeño signo más que puedo hacer clic para establecer el nombre del tipo de letra "Times New Roman", tamaño de letra "10", etc. Sorrry si uso los términos incorrectos, eso es lo que menciono por "sub-propiedades".

Respuesta

15

Todo lo que tiene que hacer es crear una nueva propiedad publicada que apunte a un tipo que tenga propiedades publicadas.

este código de verificación

type 
    TCustomType = class(TPersistent) //this type has 3 published properties 
    private 
     FColor : TColor; 
     FHeight: Integer; 
     FWidth : Integer; 
    public 
     procedure Assign(Source: TPersistent); override; 
    published 
     property Color: TColor read FColor write FColor; 
     property Height: Integer read FHeight write FHeight; 
     property Width : Integer read FWidth write FWidth; 
    end; 

    TMyControl = class(TWinControl) 
    private 
     FMyProp : TCustomType; 
     FColor1 : TColor; 
     FColor2 : TColor; 
     procedure SetMyProp(AValue: TCustomType); 
    public 
     constructor Create(AOwner: TComponent); override; 
     destructor Destroy; override; 
    published 
     property MyProp : TCustomType read FMyProp write SetMyProp; //now this property has 3 "sub-properties" (this term does not exist in delphi) 
     property Color1 : TColor read FColor1 write FColor1; 
     property Color2 : TColor read FColor2 write FColor2; 
    end; 

    procedure TCustomType.Assign(Source: TPersistent); 
    var 
    Src: TCustomType; 
    begin 
    if Source is TCustomType then 
    begin 
     Src := TCustomType(Source); 
     FColor := Src.Color; 
     Height := Src.Height; 
     FWidth := Src.Width; 
    end else 
     inherited; 
    end; 

    constructor TMyControl.Create(AOwner: TComponent); 
    begin 
    inherited; 
    FMyProp := TCustomType.Create; 
    end; 

    destructor TMyControl.Destroy; 
    begin 
    FMyProp.Free; 
    inherited; 
    end; 

    procedure TMyControl.SetMyProp(AValue: TCustomType); 
    begin 
    FMyProp.Assign(AValue); 
    end; 
+6

El definidor de la propiedad TMyControl.MyProp no debería estar escribiendo a FMyProp directamente. Eso filtrará el objeto FMyProp original y tomará posesión del objeto de la persona que llama. En su lugar, necesita utilizar un método setter que llame a FMyProp.Assign(), y TCustomType necesita implementar Assign(). –

1

¿Qué quiere decir, sub-propiedades? No hay tal cosa en Delphi.

mejor usted se refiere composición de objetos, donde un objeto contiene una referencia a otro objeto, por ejemplo -

interface 

type 
TObj1 = class 
private 
    FFont: TFont; 
    property Font: TFont read FFont; 
end; 

... 

implementation 

var 
    MyObj: TObj1; 
begin 
    MyObj1 := TObj1.Create; 
    MyObj1.Font.Name := 'Arial'; 
+0

(+1 gracias por responder) Quiero decir que cuando miro Font en el inspector de objetos, aparece un pequeño símbolo de eptl, que puedo hacer clic para establecer el nombre del fonts "times new roman", el tamaño de fuente "10", etc. Sorrry if Uso los términos incorrectos, eso es lo que menciono por "subpropiedades". – Mawg

Cuestiones relacionadas