我需要有关如何实现可以在对象Inspector中显示的类的帮助

时间:2010-11-03 07:32:34

标签: delphi class object properties custom-component

我有

...
  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 个答案:

答案 0 :(得分:6)

您必须从TPersistent或后代派生您的课程,以便在对象检查器中使用它:

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

来自Delphi文档:

  

TPersistent是所有人的祖先   具有赋值和的对象   流媒体功能。

答案 1 :(得分:3)

该类需要派生自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;