代码之家  ›  专栏  ›  技术社区  ›  smok1

在Delphi5上创建TPanel的精确副本

  •  4
  • smok1  · 技术社区  · 17 年前

    我有一个TPanel pnlMain,其中根据用户操作、数据验证等创建了几个动态TPanel(pnlMain是它们的父项)。每个面板包含一个充满字符串的彩色网格。除了面板,还有一些开源的arrows组件和图片。一大堆东西。

    on this question ),但在打印之前,必须向用户提供一个新表单,其中包含pnlMain的副本。在这个表单上,用户必须做一些更改,添加一些组件,然后打印他定制的pnlMain副本。打印后,用户将关闭此表单,并使用原始pnlMain返回原始表单。正如你所猜测的,原始的pnlMain必须保持完整。

    3 回复  |  直到 9 年前
        1
  •  3
  •   Mohammed Nasman    17 年前

    procedure TForm1.btn1Click(Sender: TObject);
    
        function CloneComponent(AAncestor: TComponent): TComponent;
          var
            XMemoryStream: TMemoryStream;
            XTempName: string;
          begin
            Result:=nil;
            if not Assigned(AAncestor) then
              exit;
            XMemoryStream:=TMemoryStream.Create;
            try
              XTempName:=AAncestor.Name;
              AAncestor.Name:='clone_' + XTempName;
              XMemoryStream.WriteComponent(AAncestor);
              AAncestor.Name:=XTempName;
              XMemoryStream.Position:=0;
              Result:=TComponentClass(AAncestor.ClassType).Create(AAncestor.Owner);
              if AAncestor is TControl then TControl(Result).Parent:=TControl(AAncestor).Parent;
              XMemoryStream.ReadComponent(Result);
            finally
              XMemoryStream.Free;
            end;
          end;
    
        var
          aPanel: TPanel;
          Ctrl, Ctrl_: TComponent;
          i: integer;
        begin
          //handle the Control (here Panel1) itself first
          TComponent(aPanel) := CloneComponent(pnl1);
          with aPanel do
          begin
            Left := 400;
            Top := 80;
          end;
    
          //now handle the childcontrols
          for i:= 0 to pnl1.ControlCount-1 do begin
            Ctrl := TComponent(pnl1.Controls[i]);
            Ctrl_ := CloneComponent(Ctrl);
            TControl(Ctrl_).Parent := aPanel;
            TControl(Ctrl_).Left := TControl(Ctrl).Left;
            TControl(Ctrl_).top := TControl(Ctrl).top;
          end;
        end;
    

    代码来自 Delphi3000 文章

        2
  •  2
  •   Fabricio Araujo    17 年前

    太多的代码。。。ObjectBinaryToText和ObjectTextToBinary使用流媒体很好地完成了这项工作。 Delphi7有一个代码示例,不知道2009年(或2006年,从不费心看)还有它。

        3
  •  1
  •   Mason Wheeler    17 年前

    this article ,您将在其中找到指向某些帮助程序代码的链接,包括 CopyObject