Мне нужна помощь о том, как реализовать класс, который может быть показан в объекте инспектора - PullRequest
0 голосов
/ 03 ноября 2010

у меня есть

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

, и я хотел, чтобы эта функция была показана в Object Insepector для редактирования настроек.

я пытаюсь добавить

property DispPitch: TDispPitch read FDispPitch write FDispPitch. like 

DispPitch может бытьпоказано, но я не могу видеть его свойства.как LineSize, LineColor и т. д.

Ответы [ 2 ]

6 голосов
/ 03 ноября 2010

Вы должны получить свой класс от TPersistent или его потомка, чтобы сделать его доступным в Инспекторе объектов:

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

Из документации Delphi:

TPersistent является предком для всех объекты, которые имеют назначение и потоковые возможности.

3 голосов
/ 04 ноября 2010

Класс должен быть производным от TPersistent и должен реализовывать метод Assign () (или AssignTo ()), а также предоставлять событие OnChange, чтобы содержащийся класс мог реагировать на изменения, например:

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;

Тогда вы используете это так:

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;
...