delphi – 如何在Object Inspector中对组件的属性进行分组?
我希望我的非视觉组件将其发布的属性置于不在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; 如何使用名为MyProperties的类别下的SomeValue和SomeValueExt在Object Inspector中注册我的组件? 插图: 我的组件可能有很多已发布的属性,我宁愿它们在Object Inspector的自有级别子类下,以使其远离公共属性,如Name和Tag. 谢谢 :) 解决方法
创建一个具有这些属性的类,然后为您的组件提供该类类型的单个属性.属性类应该是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; 该组件拥有其属性对象,因此需要创建和销毁它: constructor TMyComponent.Create; begin inherited; FProperties := TComponentProperties.Create; end; destructor TMyComponent.Destroy; begin FProperties.Free; inherited; end; 使用该代码,组件的属性现在应该在“属性”项下列出.不过,这不是一个类别.类别完全是另类.类别不会重新安排组件;它们只是更改了对象检查器中属性的显示方式.请注意,使用我的代码,TMyComponent不再具有任何SomeValue属性.相反,它只有一个属性Properties,该对象具有其他属性.考虑一下您是否真的希望组件的消费者必须访问它. 如果Properties属性不是只读的,那么它需要有一个属性设置器;你不能直接写到FProperties.写这样: procedure TMyComponent.SetProperties(const Value: TProperties); begin FProperties.Assign(Value); end; 这还要求您重写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; 我们还假设属性对象的属性也不是只读的.当属性对象的属性发生更改时,拥有对象可能想知道它,因此它应该有一个组件为其赋值的事件.当属性改变时,它们将触发事件. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |