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;
复制树(含子节点)
最新推荐文章于 2021-06-09 21:33:01 发布