I need help on how to implement class that can be

2019-01-27 10:37发布

i have

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

and i wanted this feature shown in Object Insepector to edit the settings.

i try adding

property DispPitch: TDispPitch read FDispPitch write FDispPitch. like 

the DispPitch can be shown but i cannot see its properties. like LineSize, LineColor etc.

2条回答
Summer. ? 凉城
2楼-- · 2019-01-27 10:45

The class needs to derive from TPersistent, and should implement the Assign() (or AssignTo()) method, as well as expose an OnChange event so the containing class can react to changes, eg:

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;

Then you use it like this:

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;
查看更多
来,给爷笑一个
3楼-- · 2019-01-27 10:53

You must derive your class from TPersistent, or a descendant, in order to make it available in the Object Inspector:

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

From Delphi Documentation:

TPersistent is the ancestor for all objects that have assignment and streaming capabilities.

查看更多
登录 后发表回答