using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void treeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
{
e.Cancel = true;
}
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
e.Cancel = true;
}
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)//左键拖动
{
if (e.Button == MouseButtons.Left)
{
DoDragDrop(e.Item, DragDropEffects.Move);
}
}
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");
if (moveNode.Parent == null)
{
Point pt;
TreeNode targeNode;
pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y));
targeNode = this.treeView1.GetNodeAt(pt);
if (targeNode == null)
{
return;
}
targeNode = GetParentNode(targeNode);
TreeNode NewMoveNode = (TreeNode)moveNode.Clone();
int index = targeNode.Index;
if (moveNode.Index < targeNode.Index)
{
index = targeNode.Index + 1;
}
treeView1.Nodes.Insert(index, NewMoveNode);
treeView1.SelectedNode = treeView1.Nodes[index];
this.treeView1.BeforeCollapse -= new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeCollapse);
this.treeView1.BeforeExpand -= new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
moveNode.Remove();
NewMoveNode.Expand();
NewMoveNode.EnsureVisible();
this.treeView1.BeforeCollapse += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeCollapse);
this.treeView1.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
}
}
private TreeNode GetParentNode(TreeNode treeNode)
{
if (treeNode.Parent != null)
{
return GetParentNode(treeNode.Parent);
}
return treeNode;
}
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
private void treeView1_DragOver(object sender, DragEventArgs e)
{
const Single scrollRegion = 20;
//Point pt = treeView1.PointToClient(Cursor.Position);
Point pt = treeView1.PointToClient(new Point(e.X, e.Y));
if ((pt.Y + scrollRegion) > treeView1.Height)
{
SendMessage(treeView1.Handle, (int)277, (int)1, 0);
}
else if (pt.Y < 0)
{
SendMessage(treeView1.Handle, (int)277, (int)0, 0);
}
}
private void Form1_Load(object sender, EventArgs e)
{
treeView1.ShowPlusMinus = false;
treeView1.ShowRootLines = false;
treeView1.Scrollable = true;
var aa = new TreeNode("A");
aa.Nodes.Add(new TreeNode("A1"));
aa.Nodes.Add(new TreeNode("A2"));
aa.Nodes.Add(new TreeNode("A3"));
var aa1 = new TreeNode("B");
aa1.Nodes.Add(new TreeNode("B1"));
aa1.Nodes.Add(new TreeNode("B2"));
aa1.Nodes.Add(new TreeNode("B3"));
var aa2 = new TreeNode("C");
aa2.Nodes.Add(new TreeNode("C1"));
aa2.Nodes.Add(new TreeNode("C2"));
aa2.Nodes.Add(new TreeNode("C3"));
var aa3 = new TreeNode("D");
aa3.Nodes.Add(new TreeNode("D1"));
aa3.Nodes.Add(new TreeNode("D2"));
aa3.Nodes.Add(new TreeNode("D3"));
treeView1.Nodes.Add(aa);
treeView1.Nodes.Add(aa1);
treeView1.Nodes.Add(aa2);
treeView1.Nodes.Add(aa3);
treeView1.ExpandAll();
treeView1.Refresh();
this.treeView1.BeforeCollapse += new TreeViewCancelEventHandler(this.treeView1_BeforeCollapse);
this.treeView1.BeforeExpand += new TreeViewCancelEventHandler(this.treeView1_BeforeExpand);
this.treeView1.ItemDrag += new ItemDragEventHandler(treeView1_ItemDrag);
this.treeView1.DragEnter += new DragEventHandler(treeView1_DragEnter);
this.treeView1.DragDrop += new DragEventHandler(treeView1_DragDrop);
this.treeView1.DragOver += new DragEventHandler(treeView1_DragOver);
this.treeView1.Nodes[0].EnsureVisible();
}
}
}