代码之家  ›  专栏  ›  技术社区  ›  Fayyaz

如何创建组属性[重复]

  •  0
  • Fayyaz  · 技术社区  · 11 年前

    我想创建一组属性(可扩展属性),我定义记录类型并将我的属性类型设置为记录。但该属性不会出现在对象检查器中,但在运行时我可以访问该属性。

      type
      GageProperty = Record
        MaxValue:Real;
        Color1:TColor;
        Color2:TColor;
        DividerLength:Integer;
        DownLimit:Real;
        FloatingPoint:Integer;
        Frame:Boolean;
        GageFont:TFont;
        GradiantStyle:GradStyle;
        Height:Integer;
        Width:Integer;
        Left:Integer;
        MinValue:Real;
        NeedleColor:Tcolor;
        Sector1Color:TColor;
        Sector2Color:TColor;
        Sector3Color:TColor;
        SignalFont:TFont;
        SignalNmae:String;
        Step:Integer;
        SubStep:Integer;
        Thickness:Integer;
        Top:Integer;
        UpLimit:Real;
        ValueUnit:String;
      End;
      TGasTurbine = class(TPanel)
      private
        { Private declarations }
        FGageProp:GageProperty;
        Procedure SetGageProp(Const Value:GageProperty);
      published
        { Published declarations }
        Property GageProp: GageProperty Read FGageProp Write SetGageProp;
    

    我该怎么办? 请帮帮我

    1 回复  |  直到 11 年前
        1
  •  1
  •   NGLN Selim Serhat Çelik    11 年前

    要使结构化类型可流化并在设计器中设置,该类型必须从 TPersistent :

    type
      TGage = class(TPersistent)
      public
        MaxValue: Real;
        Color1: TColor;
        Color2: TColor;
        procedure Assign(Source: TPersistent); override;
      end;
    
      TGasTurbine = class(TPanel)
      private
        FGage: TGage;
        procedure SetGage(Value: TGage);
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
      published
        property Gage: TGage read FGage write SetGage;
      end;    
    
    procedure TGage.Assign(Source: TPersistent);
    begin
      if Source is TGage then
      begin
        MaxValue := TGage(Source).MaxValue;
        Color1 := TGage(Source).Color1;
        Color2 := TGage(Source).Color2;
      end
      else
        inherited Assign(Source);
    end;
    
    constructor TGasTurbine.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      FGage := TGage.Create;
    end;
    
    destructor TGasTurbine.Destroy;
    begin
      FGage.Free;
      inherited Destroy;
    end;
    
    procedure TGasTurbine.SetGage(Value: TGage);
    begin
      FGage.Assign(Value);
    end;