procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
Item : TTreeNode;
CursorPos: TPoint;
begin
if (Sender is TTreeView) and (Source is TTreeView) then begin
if (TTreeView(Source).Selected <> nil)//and(TTreeView(Sender).DropTarget <> nil)
and(TTreeView(Source).Selected <> TTreeView(Sender).DropTarget) then begin
TTreeView(Source).Items.Delete(TTreeView(Source).Selected);
end;
end;
end;
//최용일님의 재귀호출 사용
procedure TForm1.CopyChildNode(SourceTreeView, DestTreeView: TTreeView; Source, Dest: TTreeNode);
var
Index: Integer;
Item: TTreeNode;
begin
for Index := 0 to Source.Count - 1 do
begin
// Copies Node
Item := DestTreeView.Items.AddChild(Dest, '');
Item.Assign(Source[Index]);
// Copy Child Nodes
if Source.Item[Index].HasChildren then
CopyChildNode(SourceTreeView, DestTreeView, Source[Index], Item);
end;
// Deletes Node
//for Index := Source.Count - 1 to 0 do
// SourceTreeView.Items.Delete(Source[Index]);
if Source.Count > 0 then
SourceTreeView.Items.Delete(Source[0]);
end;
procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := (Sender is TTreeView) and (Source is TTreeView)
//and(TTreeView(Source).Selected <> nil)and(TTreeView(Sender).DropTarget <> nil);
end;
재귀호출은 최용일님것 사용하구요
Treeview1,Treeview2의 속성중 DragMode는 dmAutomatic으로한후
procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
Item : TTreeNode;
CursorPos: TPoint;
begin
if (Sender is TTreeView) and (Source is TTreeView) then begin
if (TTreeView(Source).Selected <> nil)//and(TTreeView(Sender).DropTarget <> nil)
and(TTreeView(Source).Selected <> TTreeView(Sender).DropTarget) then begin
CursorPos := TTreeView(Sender).ScreenToClient(Point(X,Y));
Item := TTreeView(Sender).GetNodeAt(X, Y);
//이동할 서브 노드 리스트(서로다른 트리뷰일때 MoveTo로할경우 문제생김)
//Item := TTreeView(Sender).Items.AddChild(TTreeView(Sender).DropTarget, '');
Item := TTreeView(Sender).Items.AddChild(Item, '');
Item.Assign(TTreeView(Source).Selected);
CopyChildNode(TTreeView(Source), TTreeView(Sender), TTreeView(Source).Selected, Item);
TTreeView(Source).Items.Delete(TTreeView(Source).Selected);
end;
end;
end;
//최용일님의 재귀호출 사용
procedure TForm1.CopyChildNode(SourceTreeView, DestTreeView: TTreeView; Source, Dest: TTreeNode);
var
Index: Integer;
Item: TTreeNode;
begin
for Index := 0 to Source.Count - 1 do
begin
// Copies Node
Item := DestTreeView.Items.AddChild(Dest, '');
Item.Assign(Source[Index]);
// Copy Child Nodes
if Source.Item[Index].HasChildren then
CopyChildNode(SourceTreeView, DestTreeView, Source[Index], Item);
end;
// Deletes Node
//for Index := Source.Count - 1 to 0 do
// SourceTreeView.Items.Delete(Source[Index]);
if Source.Count > 0 then
SourceTreeView.Items.Delete(Source[0]);
end;
procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := (Sender is TTreeView) and (Source is TTreeView)
//and(TTreeView(Source).Selected <> nil)and(TTreeView(Sender).DropTarget <> nil);
end;