gpt4 book ai didi

Delphi:在属性编辑器中浏览组件

转载 作者:行者123 更新时间:2023-12-02 14:40:04 28 4
gpt4 key购买 nike

当属性是任何类的简单组件时,IDE 的属性编辑器能够下拉所有项目表单中所有兼容组件的列表。

我想做一些等效的任务,但要根据属性的可接受的组件类进行一些过滤;这些类的共同祖先只是TComponent,并且它们具有自定义接口(interface)。

目前,我有一个工作属性编辑器,它使用 paValueList 属性,并在 GetValues 过程中基于检查支持的接口(interface)进行一些过滤,但它仅限于当前形式:-(.

如何像IDE一样浏览所有表单?

最佳答案

I want to do some equivalent task, but with some filtering based on acceptable component classes for the property; these classes common ancestor is only TComponent and they have custom interfaces.

如果您仅过滤 1 个接口(interface),则应更改相关属性以接受该接口(interface)类型而不是 TComponent,然后使用接口(interface)属性的默认属性编辑器 (TInterfaceProperty )将自动为您过滤组件:

property MyProperty: IMyInterface read ... write ...;

Currently I have a working property editor that uses a paValueList attribute and some filtering in the GetValues procedure, based on checking the supported interfaces, but it is limited to the current form :-(.

How to browse all the forms like the IDE does?

要在自定义属性编辑器中手动过滤组件,您需要执行与默认组件属性编辑器(TComponentProperty)相同的操作来获取兼容的组件,然后您可以过滤它们根据需要进一步。

在内部,TComponentProperty.GetValues() 只需调用 Designer.GetComponentNames(),并将其传递给正在处理的属性类型的 PTypeData编辑:

procedure TComponentProperty.GetValues(Proc: TGetStrProc);
begin
Designer.GetComponentNames(GetTypeData(GetPropType), Proc);
end;

因此,如果您的属性接受 TComponent (因为这是您想要的组件的唯一共同祖先):

property MyProperty: TComponent read ... write ...;

然后,在这种情况下,GetPropType() 将返回 TypeInfo(TComponent)

GetComponentNames()(其实现在 IDE 中,在 VCL 源代码中不可用)枚举拥有正在编辑的组件的 Root 组件(Form、DataModule 或 Frame),以及可在编辑的 Root 的 uses 子句中指定的其他单元中访问的所有链接的 Root 对象。这是有记录的行为:

DesignIntf.IDesigner60.GetComponentNames

Executes a callback for every component that can be assigned a property of a specified type.

Use GetComponentNames to call the procedure specified by the Proc parameter for every component that can be assigned a property that matches the TypeData parameter. For each component, Proc is called with its S parameter set to the name of the component. This parameter can be used to obtain a reference to the component by calling the GetComponent method.

Note: GetComponentNames calls Proc for components in units that are in the uses clause of the current root object's unit (Delphi) or included by that unit (C++), as well as the entity that is the value of Root.

因此,在您的 GetValues() 实现中,调用 Designer.GetComponentNames() 并为 TComponent 指定 PTypeData > 并让 IDE 枚举所有可用单元并为您提供每个组件名称的列表。然后,您可以循环访问该列表,调用 Designer.GetComponent() 来获取实际的 TComponent 对象,并查询它们以获取所需的接口(interface):

procedure TMyComponentProperty.GetValues(Proc: TGetStrProc);
var
Names: TStringList;
I: Integer;
begin
Names := TStringList.Create;
try
Designer.GetComponentNames(GetTypeData(TypInfo(TComponent)), Names.Append);
for I := 0 to Names.Count-1 do
begin
if Supports(Designer.GetComponent(Names[I]), IMyInterface) then
Proc(Names[I]);
end;
finally
Names.Free;
end;
end;

事实上,这与默认的 TInterfaceProperty.GetValues() 实现非常相似:

procedure TInterfaceProperty.ReceiveComponentNames(const S: string);
var
Temp: TComponent;
Intf: IInterface;
begin
Temp := Designer.GetComponent(S);
if Assigned(FGetValuesStrProc) and
Assigned(Temp) and
Supports(TObject(Temp), GetTypeData(GetPropType)^.Guid, Intf) then
FGetValuesStrProc(S);
end;

procedure TInterfaceProperty.GetValues(Proc: TGetStrProc);
begin
FGetValuesStrProc := Proc;
try
Designer.GetComponentNames(GetTypeData(TypeInfo(TComponent)), ReceiveComponentNames);
finally
FGetValuesStrProc := nil;
end;
end;

唯一的区别是,TInterfaceProperty 不会浪费内存将名称收集到临时 TStringList 中。它在枚举它们时实时过滤它们。

关于Delphi:在属性编辑器中浏览组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32525806/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com