日期:2014-05-17 浏览次数:20961 次
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.treeView1.SelectedNode = treeView1.GetNodeAt(e.Location);
this.treeView1.DoDragDrop(this.treeView1.SelectedNode, DragDropEffects.All);
}
}
private void treeView1_DragEnter(object sender, DragEventArgs e)
{
if ((e.Data.GetDataPresent(typeof(TreeNode))))
{
e.Effect = DragDropEffects.All;
}
}
private void treeView1_DragDrop(object sender, DragEventArgs e)
{
//因为这里的x,y坐标是全局的,所以要转换一下。
Point newLocation = treeView1.PointToClient(new Point(e.X, e.Y));
TreeNode targetNode = this.treeView1.GetNodeAt(newLocation);
TreeNode tn = e.Data.GetData(typeof(TreeNode)) as TreeNode;
if (targetNode == tn)
return;
tn.Parent.Nodes.Remove(tn);
targetNode.Nodes.Add(tn);
}