2010-11-03 16 views
0

j'aibesoin d'aide sur la façon de mettre en œuvre la classe qui peut être montré dans l'objet Inspecteur

... 
    TDispPitch = class 
    private 
    iLineSize: Integer; 
    iLineColor: TColor; 
    bDisplayAccent: Boolean; 
    bVisible: Boolean; 
    published 
    property LineSize : Integer read iLineSize write iLineSize; 
    ...etc 
    end; 
... 

et je voulais cette fonction montre objet Insepector pour modifier les paramètres.

i essayer d'ajouter

property DispPitch: TDispPitch read FDispPitch write FDispPitch. like 

le DispPitch peut être affiché mais je ne peux pas voir ses propriétés. comme LineSize, LineColor etc.

Répondre

6

Vous devez tirer votre classe de TPersistent, ou un descendant, afin de le rendre disponible dans l'inspecteur d'objets:

TDispPitch = class(TPersistent) 
private 
    ... 
published 
    property ... 
    ... 
end; 

De Delphi Documentation:

TPersistent est l'ancêtre de tous les objets qui ont une affectation et des capacités de streaming .

+2

vous devez également remplacer la méthode 'TPersistent.Assign()', et alors la propriété de la classe contenant l'utilisation d'une méthode setter qui appelle Assign() insted de attribuer s son pointeur de champ directement. Voir mon autre réponse. –

3

La classe doit tirer de TPersistent, et devrait mettre en œuvre la méthode Assign() (ou AssignTo()), ainsi que d'exposer un événement OnChange de sorte que la classe contenant peut réagir aux changements, par exemple:

type 
    TDispPitch = class(TPersistent) 
    private 
    iLineSize: Integer; 
    iLineColor: TColor; 
    bDisplayAccent: Boolean; 
    bVisible: Boolean; 
    FOnChange: TNotifyEvent; 
    procedure Changed; 
    procedure SetLineSize(Value : Integer); 
    procedure SetLineColor(Value: TColor); 
    procedure SetDisplayAccent(Value: Boolean); 
    procedure SetVisible(Value: Boolean); 
    public 
    procedure Assign(Source: TPersistent); override; 
    property OnChange: TNotifyEvent read FOnChange write FOnChange; 
    published 
    property LineSize : Integer read iLineSize write SetLineSize; 
    property LineColor: TColor read iLineColor write SetLineColor; 
    property DisplayAccent: Boolean read bDisplayAccent write SetDisplayAccent; 
    property Visible: Boolean read bVisible write SetVisible; 
    end; 

procedure TDispPitch.Assign(Source: TPersistent); 
var 
    LSource: TDispPitch; 
begin 
    if Source is TDispPitch then 
    begin 
    LSource := TDispPitch(Source); 
    iLineSize := LSource.LineSize; 
    iLineColor := LSource.LineColor; 
    bDisplayAccent := LSource.DisplayAccent; 
    bVisible := LSource.Visible; 
    Changed; 
    end else 
    inherited; 
end; 

procedure TDispPitch.Changed; 
begin 
    if FOnChange <> nil then FOnChange(Self); 
end; 

procedure TDispPitch.SetLineSize(Value : Integer); 
begin 
    if iLineSize <> Value then 
    begin 
    iLineSize := Value; 
    Changed; 
    end; 
end; 

procedure TDispPitch.SetLineColor(Value: TColor); 
begin 
    if iLineColor <> Value then 
    begin 
    iLineColor := Value; 
    Changed; 
    end; 
end; 

procedure TDispPitch.SetDisplayAccent(Value: Boolean); 
begin 
    if bDisplayAccent <> Value then 
    begin 
    bDisplayAccent := Value; 
    Changed; 
    end; 
end; 

procedure TDispPitch.SetVisible(Value: Boolean); 
begin 
    if bVisible <> Value then 
    begin 
    bVisible := Value; 
    Changed; 
    end; 
end; 

ensuite, vous l'utiliser comme ceci:

type 
    TSomeOtherClass = class(...) 
    private 
    FDispPitch: TDispPitch; 
    procedure DispPitchChanged(Sender: TObject); 
    procedure SetDispPitch(Value: TDispPitch); 
    public 
    constructor Create; override; 
    destructor Destroy; override; 
    published 
    property DispPitch: TDispPitch read FDispPitch write SetDispPitch; 
    end; 

constructor TSomeOtherClass.Create; 
begin 
    inherited; 
    FDispPitch := TDispPitch.Create; 
end; 

destructor TSomeOtherClass.Destroy; 
begin 
    FDispPitch.Free; 
    inherited; 
end; 

procedure TSomeOtherClass.DispPitchChanged(Sender: TObject); 
begin 
    ... use new FDispPitch values as needed... 
end; 

procedure TSomeOtherClass.SetDispPitch(Value: TDispPitch); 
begin 
    FDispPitch.Assign(Value); 
end;