怎么才可以实现treeview结点的拖动呢?(200分)

  • 主题发起人 主题发起人 deee
  • 开始时间 开始时间
D

deee

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么可以用拖动的方法来实现treeview结点的移动呢?
谢谢各位大虾的帮忙!
 
记住将treeview的dragmode设置为dmAutomatic

procedure TForm.TreeViewDragOver事件中谁便写点什么。

procedure TForm.TreeViewDragDrop(Sender, Source: TObject; X,
Y: Integer);
var
VL_Node, VL_tmpNode: TTreeNode;
VL_I: integer;
begin
if (TreeView.Selected <> nil) and (TreeView.Selected.level > 0) then
begin
VL_Node := TreeView.GetNodeAt(X, Y);
if VL_Node <> nil then
begin
if TreeView.Selected.Parent = VL_Node then Exit;
if TreeView.Selected = VL_Node then Exit;
if TreeView.Selected.Level < VL_Node.Level then
begin
VL_tmpNode := VL_Node;
for VL_I := 0 to VL_Node.Level - TreeView.Selected.Level - 1 do
begin
VL_tmpNode := VL_tmpNode.Parent;
end;
if TreeView.Selected = VL_tmpNode then Exit;
end;

if Application.MessageBox('是否确认要移动该节点?', '确认信息', mb_YESNO or mb_ICONQUESTION) <> IDYES then Exit;
TreeView.Selected.MoveTo(VL_Node, naADDChild);
end;
end;
end;
 
以下是一段代码,可作参考:

//鼠标按下时执行的语句,在MouseDown事件中加入下列语句
begin
//判断左键按下并且鼠标点在一个结点上开始实现拖拽
if(Button=mbLeft)and(htOnItem in Treeview1.GetHitTestInfoAt(X,Y)) then Treeview1.BeginDrag(False);
end;

//鼠标拖动执行语句,在DragOver事件中加入下列语句
var
Node:TTreeNode;
begin
if Source=Treeview1 then begin
Node:= Treeview1.GetNodeAt(X,Y);{取当前结点}
if Node<>nil then Accept:=true;{当前结点不为空才能实现拖拽,accept:=true}
end;
end;

//鼠标释放时执行的语句,在DragDrop事件中加入下列语句
var
TempNode:TTreeNode;
AttachMode:TNodeAttachMode;
begin
if Treeview1.Selected = nil then Exit;
AttachMode:=naAddChild;{设置结点移动模式,设移动结点为子结点}

//注意这里有bug,当移动结点时,如果目标结点没有子结点,则加入的新的子结点会失败,所以先在当前目标结点的下面加入一个临时子结点,移动完毕后,再将临时结点删除
Treeview1.Items.BeginUpdate;
try
TempNode:=Treeview1.Items.AddChild(Treeview1.DropTarget,'Temp');
try{移动选中的结点到目标结点}
Treeview1.Selected.MoveTo(Treeview1.DropTarget,AttachMode);
finally
TempNode.Free;{不要忘了释放临时结点}
end;
finally
Treeview1.Items.EndUpdate;
end;
end;
 
我有一端代码,但是没有完全成功:
procedure TTreeDragForm.CopyNodeUnder(TreeView:TTreeview;SourceNode,TargetNode:TTreeNode);
var
NewNode:TTreeNode;
i:integer;
begin//增加一个新的节点
NewNode:=Treeview.Items.AddChildFirst(TargetNode,'');
NewNode.Assign(SourceNode);
for i:=SourceNode.Count-1 downto 1 do
CopyNodeUnder(TreeView,SourceNode.Item,NewNode);
Treeview.Items.Delete(SourceNode);
end;

procedure TTreeDragForm.TreeView1DragOver(Sender, Source: TObject; X,
Y: Integer; State: TDragState; var Accept: Boolean);
var
TargetNode,SourceNode:TTreeNode;
begin
TargetNode:=Treeview1.GetNodeAt(x,y);
//获得当前所在位置的节点
if (Source = Sender) and (TargetNode<>nil ) then
begin
Accept:=True;
SourceNode:=Treeview1.Selected ;
While (TargetNode.Parent<>nil) and
(TargetNode<>SourceNode) do
begin
TargetNode:=TargetNode.Parent ;
if TargetNode=SourceNode then Accept:=False;
end;//如果到达节点是该节点的下级,则不能移动!!
end
Else Accept:=False;
end;

procedure TTreeDragForm.TreeView1DragDrop(Sender, Source: TObject; X,
Y: Integer);
var
TargetNode,SourceNode:TTreeNode;
begin
TargetNode:=Treeview1.GetNodeAt(x,y);
Label1.Caption:=TargetNode.Text;
if TargetNode<>Nil then
begin
SourceNode:=TreeView1.Selected ; //获得
Label2.Caption:=TargetNode.Text;
TreeView1.Items.BeginUpdate ;//暂时禁止刷新
Try
CopyNodeUnder(TreeView1,SourceNode,TargetNode);
TargetNode.Expand(False);
Treeview1.Selected :=TargetNode;
finally
TreeView1.Items.EndUpdate ;
end;
end;
end;
 
多人接受答案了。
 

Similar threads

回复
0
查看
830
不得闲
回复
0
查看
885
不得闲
D
回复
0
查看
822
DelphiTeacher的专栏
D
后退
顶部