private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
TreeNode tn = e.Item as TreeNode;
if ((e.Button == MouseButtons.Left) && (tn != null) && (tn.Parent != null)) //根节点不允许拖放操作。
{
this.treeView1.DoDragDrop(tn, DragDropEffects.Copy | DragDropEffects.Move | DragDropEffects.Link);
}
}
private void treeView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void treeView1_DragDrop(object sender, DragEventArgs e)
{
TreeNode NewNode;
if (e.Data.GetDataPresent(typeof(TreeNode)))
{
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);
NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
DestinationNode.Nodes.Add((TreeNode)NewNode.Clone());
DestinationNode.Expand();
//删除已经移动的节点
NewNode.Remove();
}
}