zoukankan      html  css  js  c++  java
  • TreeView拖动

    namespace TreeViewConfig
    {
    public partial class FormMain : Form
    {
    const int scrollRegion = 25;

    [DllImport("user32.dll")]

    private static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);


    public FormMain()
    {
    InitializeComponent();

    }

    private void btnConnToDB_Click(object sender, EventArgs e)
    {
    treeViewMain.Nodes.Clear();
    tableStation.Clear();

    treeViewMain.Nodes.Add("牵引");
    treeViewMain.Nodes.Add("电力");


    //连接数据库
    SqlConnection connection = new SqlConnection();
    connection.ConnectionString = "server=" + textBoxServer.Text + ";Initial Catalog=" + textBoxDB.Text + ";Integrated Security=false;uid=" + textBoxUser.Text + ";pwd=" + textBoxPwd.Text;

    SqlDataAdapter command = new SqlDataAdapter("SELECT distinct name, StID, StCode, type FROM stationConfig order by StCode", connection);
    command.Fill(dsStation, "StationConfig");

    connection.Close();

    tableStation = dsStation.Tables["StationConfig"];

    IEnumerable<DataRow> query =
    from name in tableStation.AsEnumerable()
    select name;

    foreach (DataRow dr in query)
    {
    if (dr.Field<byte>("type") <= 3)
    {
    treeViewMain.Nodes[0].Nodes.Add(string.Format("[{0}] {1}",
    (dr.Field<Int16>("StCode")).ToString("000"),
    dr.Field<string>("name")));
    }
    else
    {
    treeViewMain.Nodes[1].Nodes.Add(string.Format("[{0}] {1}",
    (dr.Field<Int16>("StCode")).ToString("000"),
    dr.Field<string>("name")));
    }
    }
    }

    private void treeViewMain_DragDrop(object sender, DragEventArgs e)
    {
    //得到拖放中的节点
    TreeNode moveNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");

    //根据鼠标坐标确定要移动到的目标节点
    TreeNode targetNode = new TreeNode();
    Point pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y));

    targetNode = this.treeViewMain.GetNodeAt(pt);

    //如果目标为自己,返回
    if (targetNode.Text == moveNode.Text)
    {
    return;
    }

    //如果目标包含在自己的节点树中,返回
    if (moveNode.Nodes.Contains(targetNode))
    {
    return;
    }
    else if (targetNode.FullPath.Contains(moveNode.FullPath))
    {
    return;
    }

    //如果目标为父节点,返回
    if (targetNode.Parent == moveNode)
    {
    return;
    }

    //添加节点
    TreeNode newmoveNode = (TreeNode)moveNode.Clone();

    targetNode.Nodes.Insert(targetNode.Nodes.Count, newmoveNode);

    //更新当前拖动的节点选择
    treeViewMain.SelectedNode = newmoveNode;

    targetNode.Expand();

    //移除拖放的节点
    moveNode.Remove();
    }

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

    private void treeViewMain_ItemDrag(object sender, ItemDragEventArgs e)
    {
    if (e.Button == MouseButtons.Left)
    {
    //((TreeNode)e.Item).Parent.Collapse();
    DoDragDrop(e.Item, DragDropEffects.Move);
    }
    }

    private void treeViewMain_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
    treeViewMain.SelectedNode = e.Node;
    contextMenuStripTree.Show(treeViewMain, e.X, e.Y);
    }
    }

    private void AddNodeToolStripMenuItem_Click(object sender, EventArgs e)
    {
    AddNodeForm addnodeform = new AddNodeForm();
    addnodeform.Location = new Point(treeViewMain.Location.X + 25, addNodeFormY);
    addnodeform.StartPosition = FormStartPosition.Manual;
    addnodeform.ShowDialog(this);

    if (addnodeform.NewNodeName != "")
    {
    treeViewMain.SelectedNode.Nodes.Insert(treeViewMain.SelectedNode.Nodes.Count, addnodeform.NewNodeName);
    treeViewMain.SelectedNode.Expand();
    }
    addnodeform.Close();
    }

    private void DelNodeToolStripMenuItem_Click(object sender, EventArgs e)
    {
    treeViewMain.SelectedNode.Remove();
    }

    private void treeViewMain_MouseMove(object sender, MouseEventArgs e)
    {
    addNodeFormY = e.Y + 45;
    mouseY = e.Y;

    label1.Text = string.Format("X:{0} Y:{1} treeViewMain.VScrollPos:{2} treeViewMain.Height:{3} mouseY:{4}", e.X, e.Y, treeViewMain.VScrollPos, treeViewMain.Height, mouseY);
    }

    private void treeViewMain_DragOver(object sender, DragEventArgs e)
    {
    //拖动过程中高亮鼠标滑过的节点
    TreeViewHitTestInfo hti = this.treeViewMain.HitTest(this.treeViewMain.PointToClient(new Point(e.X, e.Y)));
    this.treeViewMain.SelectedNode = hti.Node;

    //拖动过程中鼠标达到上下边界时滚动条自动调整
    if (e.Y > (treeViewMain.Height + scrollRegion*8))
    {
    //向下滚动
    SendMessage(treeViewMain.Handle, (int)277, (int)1, 0);
    }
    else if (e.Y < (treeViewMain.Top + scrollRegion))
    {
    //向上滚动
    SendMessage(treeViewMain.Handle, (int)277, (int)0, 0);
    }

    }
    }
    }
  • 相关阅读:
    ESFramework Demo -- 动态组及群聊Demo(附源码)
    反射整理学习
    JavaScript 每周导读
    SQLSERVER 中的 with锁级别
    代码细节重构:请对我的代码指手划脚
    SQLServer查询死锁语句
    模块加载系统 v16
    数据结构之排序算法C#实现
    浅谈操作系统对内存的管理
    如何编写可维护的面向对象JavaScript代码
  • 原文地址:https://www.cnblogs.com/jiewei915/p/2318951.html
Copyright © 2011-2022 走看看