用于在TreeView之间调整树节点位置
//开始拖动树节点 private void treeView1_ItemDrag(object sender, ItemDragEventArgs e) ...{ if (e.Button == MouseButtons .Left ) ...{ DoDragDrop(e.Item, DragDropEffects.Move); } } //检验拖放的数据是否适用于目标控件,即是否为TreeNode对象 private void treeView1_DragEnter(object sender, DragEventArgs e) ...{ if (e.Data .GetDataPresent ("System.Windows.Forms.TreeNode")) ...{ e.Effect = DragDropEffects.Move; } else ...{ e.Effect = DragDropEffects.None; } } //拖放操作 private void treeView1_DragDrop(object sender, DragEventArgs e) ...{ //获得拖放中的节点 TreeNode moveNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode"); //根据鼠标坐标确定要移动到的目标节点 Point pt; TreeNode targeNode; pt = ((TreeView)(sender)).PointToClient(new Point(e.X,e.Y)); targeNode = treeView1.GetNodeAt(pt); //如果目标节点无子节点则添加为同级节点,反之添加到下级节点的未端 if (targeNode.Nodes.Count == 0) ...{ targeNode.Parent.Nodes.Insert(targeNode.Index, (TreeNode)moveNode.Clone()); } else ...{ targeNode.Nodes.Insert(targeNode.Nodes .Count, (TreeNode)moveNode.Clone()); } //展开目标节点,便于显示拖放效果 targeNode.Expand(); //移除拖放的节点 moveNode.Remove(); }