首页  编辑  

复制,移动TreeView节点

Tags: /超级猛料/VCL/TreeView/   Date Created:
procedure AddSubTree(DestTree: TTreeView; SourceNode, DestNode: TTreeNode;
  AddState: Boolean);
var
  TempNode, TempNode1: 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;

procedure MoveSubTreeView(SourceTreeView, DestTreeView: TTreeView;
  Keep: Boolean);
var
  TempLevel, TempIndex: integer;
  I, J: integer;
  TempNode: array of TTreeNode;
  TempNode1: TTreeNode;
  Yes_No: Boolean;
  OldOnChanging, OldOnChanging1: TTVChangingEvent;
  OldOnChange, OldOnChange1: TTVChangedEvent;
  IsSelected: TTreeNode;
begin
  OldOnChanging := DestTreeView.OnChanging;
  DestTreeView.OnChanging := Nil;
  OldOnChange := DestTreeView.OnChange;
  DestTreeView.OnChange := Nil;
  OldOnChanging1 := SourceTreeView.OnChanging;
  SourceTreeView.OnChanging := Nil;
  OldOnChange1 := SourceTreeView.OnChange;
  SourceTreeView.OnChange := Nil;
  IsSelected := SourceTreeView.Selected;
  TempLevel := SourceTreeView.Selected.Level;
  setlength(TempNode, TempLevel + 1);
  Yes_No := False;
  TempNode1 := Nil;
  TempIndex := FindNode(DestTreeView, SourceTreeView.Selected.Text);
  if TempIndex > -1 then
  begin
    DestTreeView.Items[TempIndex].Delete;
  end;
  with SourceTreeView do
  begin
    TempNode[TempLevel] := Selected;
    if TempLevel > 0 then
      for I := TempLevel - 1 downto 0 do
      begin
        TempNode[I] := TempNode[I + 1].Parent;
      end;
  end;
  with DestTreeView.Items do
  begin
    if Count > 0 then
    begin
      for I := TempLevel downto 0 do
        for J := Count - 1 downto 0 do
        begin
          if (TempNode[I].Text = DestTreeView.Items[J].Text) and
            (TempNode1 = Nil) then
          begin
            if I = TempLevel then
              Yes_No := True;
            TempNode1 := DestTreeView.Items[J];
          end;
        end;
    end;
    if TempNode1 = Nil then
      for I := 0 to TempLevel - 1 do
      begin
        TempNode1 := AddChild(TempNode1, TempNode[I].Text);
      end
    else
      for I := TempNode1.Level + 1 to TempLevel - 1 do
      begin
        TempNode1 := AddChild(TempNode1, TempNode[I].Text);
      end;
  end;

  AddSubTree(DestTreeView, IsSelected, TempNode1, False); // Yes_No);
  if (Not(Keep)) then
  begin
    if (IsSelected.Parent <> Nil) and (IsSelected.Parent.Count < 1) then
      IsSelected.Parent.Delete
    else
      IsSelected.Delete;
  end;
  DestTreeView.SortType := stData;
  DestTreeView.OnChanging := OldOnChanging;
  DestTreeView.OnChange := OldOnChange;
  SourceTreeView.OnChanging := OldOnChanging1;
  SourceTreeView.OnChange := OldOnChange1;
end;