我希望我的非视觉组件将其发布的属性放在不在Object Inspector顶层的类别下.
以下面的例子为例:

type
TMyComponent = class(TComponent)
protected
function GetSomeValue: string;
function GetSomeValueExt: string;
published
property SomeValue: string read GetSomeValue;
property SomeValueExt: string read GetSomeValueExt;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('My Component', [TMyComponent]);
end;
function TMyComponent.GetSomeValue: string;
begin
Result := 'test';
end;
function TMyComponent.GetSomeValueExt: string;
begin
Result := 'extended..';
end;
Run Code Online (Sandbox Code Playgroud)
如何使用名为MyProperties的类别下的SomeValue和SomeValueExt在Object Inspector中注册我的组件?
插图:

我的组件可能有很多已发布的属性,我宁愿它们在Object Inspector的自有级子类别下面,以使它远离公共属性,如Name和Tag.
谢谢 :)
Rob*_*edy 17
创建一个具有这些属性的类,然后为您的组件提供该类类型的单个属性.属性类应该是TPersistent后代:
type
TComponentProperties = class(TPersistent)
private
function GetSomeValue: string;
function GetSomeValueExt: string;
published
property SomeValue: string read GetSomeValue;
property SomeValueExt: string read GetSomeValueExt;
end;
TMyComponent = class(TComponent)
private
FProperties: TComponentProperties;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Properties: TComponentProperties read FProperties;
end;
Run Code Online (Sandbox Code Playgroud)
该组件拥有其属性对象,因此需要创建和销毁它:
constructor TMyComponent.Create;
begin
inherited;
FProperties := TComponentProperties.Create;
end;
destructor TMyComponent.Destroy;
begin
FProperties.Free;
inherited;
end;
Run Code Online (Sandbox Code Playgroud)
使用该代码,组件的属性现在应该列在"属性"项下.不过,这不是一个类别.类别完全是另类.类别不会重新安排组件; 它们只是更改了对象检查器中属性的显示方式.请注意,使用我的代码,TMyComponent不再具有任何SomeValue属性.相反,它只有一个属性Properties,并且该对象具有其他属性.考虑一下您是否真的希望组件的消费者必须访问它.
如果Properties属性不是只读的,那么它需要有一个属性设置器; 你不能直接写它FProperties.写这样:
procedure TMyComponent.SetProperties(const Value: TProperties);
begin
FProperties.Assign(Value);
end;
Run Code Online (Sandbox Code Playgroud)
这也要求你重写Assign方法来做正确的事情:
procedure TComponentProperties.Assign(Other: TPersistent);
begin
if Other is TComponentProperties then begin
SomeValue := TComponentProperties(Other).SomeValue;
SomeValueEx := TComponentProperties(Other).SomeValueEx;
end else
inherited;
end;
Run Code Online (Sandbox Code Playgroud)
我们还假设属性对象的属性也不是只读的.当属性对象的属性发生更改时,拥有对象可能想知道它,因此它应该有一个组件为其赋值的事件.当属性改变时,它们将触发事件.
| 归档时间: |
|
| 查看次数: |
2152 次 |
| 最近记录: |