2

我有一个包含大量事件处理程序的设计和运行时组件。我暂时称它为 TNewComp。我在 TForm 上创建了一个 TNewComp 的实例,并在设计时通过属性编辑器用特定代码填充事件存根,并意识到我希望能够创建使用当前事件处理程序代码集的 TNewcomp 的新实例。

为此,我现在调用 TNewComp 的构造函数,然后“手动”为每个新实例的事件处理程序分配相应的事件存根代码,该存根代码驻留在包含在设计时创建的 TNewComp 实例的表单上。因此,如果我在名为 TNewForm 的表单上将一个 TNewComp 实例分配给名为 FNewComp 的变量,那么对于每个事件处理程序,我会执行以下操作:

FNewComp.onSomething = TNewform.onSomething
(... repeat for each event handler belonging to TNewComp ...)

这很好用,但是很麻烦而且更糟糕的是,如果我向 TNewComp 添加一个新的事件处理程序,我必须记住更新我的“newTComp()”函数以进行事件处理程序分配。为我动态创建新实例的每个独特组件类型冲洗并重复此过程。

有没有办法自动化这个过程,也许使用属性检查或其他一些 Delphi 6 内省技术?

——罗施勒

4

3 回答 3

4

我使用了以下代码。创建时要小心Dest owner,最安全的方法是传递Nil,以后自己释放组件。

implementation uses typinfo;

procedure CopyComponent(Source, Dest: TComponent);
var
  Stream: TMemoryStream;
  TypeData : PTypeData;
  PropList: PPropList;
  i, APropCount: integer;
begin
  Stream:=TMemoryStream.Create;
  try
    Stream.WriteComponent(Source);
    Stream.Position:=0;
    Stream.ReadComponent(Dest);
  finally
    Stream.Free;
  end;

  TypeData := GetTypeData(Source.ClassInfo);
  if (TypeData <> nil) then
  begin
    GetMem(PropList, SizeOf(PPropInfo)*TypeData^.PropCount);
    try
      APropCount:=GetPropList(Source.ClassInfo, [tkMethod], PropList);
      for i:=0 to APropCount-1 do
        SetMethodProp(Dest, PropList[i], GetMethodProp(Source, PropList[i]))
    finally
      FreeMem(PropList);
    end;
  end;
end;
于 2011-07-06T19:40:55.063 回答
2

一种选择是将“正确设置的组件”保存到流中,然后将该流加载到新的、动态创建的组件中,就好像它是由 Delphi IDE/运行时完成的一样。

另一种选择是使用 RTTI,即 TypInfo 单元。在那里,您有一个功能GetPropList,可以让您查询可用事件(TypeKind tkMethod),然后您可以使用GetMethodProp并将SetMethodProp事件处理程序从一个组件复制到另一个组件。

于 2011-07-06T18:45:20.973 回答
1

我将 Maksee 的解决方案调整为以下内容:

function CopyComponent(Source: TComponent; Owner: TComponent = nil): TComponent;
var
    Stream: TMemoryStream;
    TypeData : PTypeData;
    PropList: PPropList;
    i, APropCount: integer;
begin
    if not Assigned(Source) then
        raise Exception.Create('(CopyComponent) The Source component is not assigned.');

    Result := TComponent.Create(Owner);

    Stream := TMemoryStream.Create;

    try
        Stream.WriteComponent(Source);
        Stream.Position := 0;
        Stream.ReadComponent(Result);
    finally
        Stream.Free;
    end; // try()

    // Get the type data for the Source component.
    TypeData := GetTypeData(Source.ClassInfo);

    if (TypeData <> nil) then
    begin
        // Get the property information for the source component.
        GetMem(PropList, SizeOf(PPropInfo) * TypeData^.PropCount);

        try
            // Get the properties count.
            APropCount := GetPropList(Source.ClassInfo, [tkMethod], PropList);

            // Assign the source property methods to the destination.
            for i := 0 to APropCount - 1 do
                SetMethodProp(Result, PropList[i], GetMethodProp(Source, PropList[i]))

        finally
            // Free the property information object.
            FreeMem(PropList);
        end; // try()
    end; // if (TypeData <> nil) then
end;

这样函数就会返回一个新组件,而不是传入现有的组件引用(Maksee 版本中的 Dest 参数)。如果有人可以看到此变体将导致的缺陷或问题,请发表评论。

于 2011-10-15T18:57:43.957 回答