1
2 unit Unit1;
3
4 interface
5
6 uses
7 Windows, Messages, SysUtils, Variants, Classes, Controls, Forms,
8 Dialogs, StdCtrls, ComCtrls;
9
10 type
11 TForm1 = class(TForm)
12 TreeView1: TTreeView;
13 Label1: TLabel;
14 procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
15 procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
16 State: TDragState; var Accept: Boolean);
17 private
18 { Private declarations }
19 procedure MoveNode(TargetNode, SourceNode : TTreeNode);
20 public
21 { Public declarations }
22 end;
23
24 var
25 Form1: TForm1;
26
27 implementation
28
29 {$R *.dfm}
30
31 //
32 // Procedure which will move node and its subnodes
33 //
34 procedure TForm1.MoveNode(TargetNode, SourceNode : TTreeNode);
35 var
36 nodeTmp : TTreeNode;
37 i : Integer;
38 begin
39 with TreeView1 do
40 begin
41 nodeTmp := Items.AddChild(TargetNode,SourceNode.Text);
42 for i := 0 to SourceNode.Count -1 do
43 begin
44 MoveNode(nodeTmp,SourceNode.Item[i]);
45 end;
46 end;
47 end;
48
49
50 procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
51 var
52 TargetNode, SourceNode : TTreeNode;
53 begin
54 with TreeView1 do
55 begin
56 TargetNode := GetNodeAt(X,Y); // Get target node
57 SourceNode := Selected;
58 if (TargetNode = nil) then
59 begin
60 EndDrag(False);
61 Exit;
62 end;
63 MoveNode(TargetNode,SourceNode);
64 SourceNode.Free;
65 end;
66 end;
67
68 procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
69 State: TDragState; var Accept: Boolean);
70 begin
71 if (Sender = TreeView1) then // If TRUE than accept the draged item
72 begin
73 Accept := True;
74 end;
75 end;
76
77 end.
|