複製樹(含子節點)

來源:互聯網
上載者:User

uses
  Windows, SysUtils, Classes,StdCtrls, ComCtrls;

const
 createToChild=1;
 createToAboveBrother=2;
 createToUnderBrother=3;

{ 複製樹(含子節點)
    輸入參數
      DestTree        複製目的地TTreeView控制項
      SourceNode      複製源
      DestNode        複製目的地
      CreateStyle     複製類型
                        createToChild          產生複製目的地的子節點
                        createToAboveBrother   產生複製目的地的上面兄弟節點
                        createToUnderBrother   產生複製目的地的下面兄弟節點
}
procedure CopyTree(DestTree:TTreeView;SourceNode,DestNode:TTreeNode;CreateStyle:Byte);

 procedure AddSubTree(DestTree:TTreeView;SourceNode,DestNode:TTreeNode;AddState:Boolean);
 var
   TempNode:TTreeNode;
   I:integer;
 begin
  TempNode:=DestNode;
  with DestTree do
  begin
   if Not(AddState) then TempNode:=Items.AddChild(DestNode,SourceNode.Text);
   if SourceNode.HasChildren then
   begin
    for I:=0 to SourceNode.Count-1 do
    begin
     if I>0 then
      TempNode:=Items.AddChild(TempNode.Parent,SourceNode.Item[I].Text)
     else
      TempNode:=Items.AddChild(TempNode,SourceNode.Item[I].Text);
      AddSubTree(DestTree,SourceNode.Item[I],TempNode,True);
     end;
    end;
   end;
  end;

var
 TempNode,TempNode2:TTreeNode;
begin
 if SourceNode=nil then Exit;

 if CreateStyle=createToChild then
   AddSubTree(DestTree,SourceNode,DestNode,False);

 if CreateStyle=createToAboveBrother then
 begin
   TempNode:=DestTree.Items.Insert(DestNode,SourceNode.Text);
   AddSubTree(DestTree,SourceNode,TempNode,True);
 end;

 if CreateStyle=createToUnderBrother then
 begin
   TempNode2:=DestNode.Parent;
   TempNode:=TempNode2.GetNextChild(DestNode);
   if TempNode<>nil then
     TempNode:=DestTree.Items.Insert(TempNode,SourceNode.Text)
   else
     TempNode:=DestTree.Items.AddChild(TempNode2,SourceNode.Text);
   AddSubTree(DestTree,SourceNode,TempNode,True);
 end;
end;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.