private void ProjectTreeView_DragEnter(object sender, DragEventArgs e) { try { object MyData = e.Data.GetData(typeof(TreeNode)); //如果节点有数据,拖放目标允许移动 if (MyData != null) { e.Effect = DragDropEffects.Move; } else { e.Effect = DragDropEffects.None; } TreeView MyTreeView = (TreeView)sender; TreeNode MyNode = MyTreeView.GetNodeAt(ProjectTreeView.PointToClient(new Point(e.X, e.Y))); //if (MyNode != null) //{ //改变进入节点的背景色 // MyNode.BackColor = Color.Blue; // //保存此节点,进入下一个时还原背景色 // MyOldNode = MyNode; //} } catch (SystemException ex) { return; } }
private void ProjectTreeView_DragDrop(object sender, DragEventArgs e) { TreeNode MyNode = (TreeNode)e.Data.GetData(typeof(TreeNode)); TreeView MyTreeView = (TreeView)sender; //得到当前鼠标进入的节点 TreeNode MyTargetNode = MyTreeView.GetNodeAt(ProjectTreeView.PointToClient(new Point(e.X, e.Y))); if (MyTargetNode != null) { TreeNode MyTargetParent = MyTargetNode.Parent; //删除拖放的节点 if (MyNode == MyTargetNode) { return; } MyNode.Remove(); //添加到目标节点 MyTargetNode.Nodes.Add(MyNode); //MyTargetNode.BackColor = Color.White; MyTreeView.SelectedNode = MyTargetNode; } }