TreeView(树杈视图)

==================================================


AllowDrop                         是否允许拖拽
ImageList
                         ImageList控件(Images属性)
SelectedImageIndex
        被选中的树杈的图标

ImageList是以Node为主的


==================================================


ItemDrag
->DoDragDrop ->初始化拖曳操作
DragEnter
->当您将鼠标指针移至任何一个控件的上方时,便会引发该控件的DragEnter 事件
DragEnter ->GetDataPresent ->检测所拖曳的数据格式是否适用于目标控件
DragDrop ->如果用户在一个有效的置放目标上放开鼠标按键,将会引发目标控件的DragDrop 事件

==================================================

//添加树杈

TreeNode tn1 = new TreeNode("站点1");
treeView1.Nodes.Add(tn1);
tn1.ImageIndex = 0;
TreeNode tn1_1=new TreeNode("站点1-1");
tn1.Nodes.Add(tn1_1);
tn1_1.ImageIndex = 1;

//节点拖动
private void treeView1_ItemDrag (object sender, ItemDragEventArgs e)
{
            TreeNode tn=(TreeNode)e.Item;
            if (e.Button == MouseButtons.Left && tn != null && tn.Parent != null)
            {
                DoDragDrop (tn, DragDropEffects.Move);
            }
}

private void treeView1_DragEnter(object sender, DragEventArgs e)
{
            if (e.Data. GetDataPresent ("System.Windows.Forms.TreeNode"))
            {
                e.Effect = DragDropEffects.Move;
            }
}

private void treeView1_DragDrop(object sender, DragEventArgs e)
{
            if (e.Data.GetDataPresent ("System.Windows.Forms.TreeNode"))
            {
                TreeNode dragedTreeNode = (TreeNode)e.Data.GetData ("System.Windows.Forms.TreeNode");
                Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
                TreeNode targetTreeNode =((TreeView)sender).GetNodeAt(pt);
                targetTreeNode .Nodes.Add((TreeNode)dragedTreeNode .Clone());
                dragedTreeNode.Remove();
                ((TreeView)sender).ExpandAll();
            }
}

你可能感兴趣的:(TreeView(树杈视图))