zoukankan      html  css  js  c++  java
  • TreeView中找鼠标指向的节点

    在使用TreeView的时候,某个TreeNode的右键菜单指定了,但右击的时候其选定的节点并不是鼠标指向的节点,此时右键菜单中的功能要找到操作的节点就不能是SelectedNode了,为这个问题上网也没找到答案,最后在MSDN里找到一个方法,利用GetNodeAt(Point p)的方法找节点,只要知道P,就可找到节点,最后实现的方法如下:通过以下代码找到节点,在MouseDown里把此节点设为SelectedNode,这样就可能在右键菜单里对SelectedNode操作了.注:Control为TreeView所在的Form.

            /// <summary>
            
    /// 得到TreeView里鼠标指向的节点,同时把该节点设置为当前选中的节点
            
    /// </summary>
            
    /// <param name="tv"></param>
            
    /// <returns></returns>

            public static TreeNode GetMouseNode(TreeView tv, Control control)
            
    {
                Point pt 
    = control.PointToScreen(tv.Location);
                Point p 
    = new Point(Control.MousePosition.X - pt.X, Control.MousePosition.Y - pt.Y);
                TreeNode tn 
    = tv.GetNodeAt(p);
                
    return tn;
            }

  • 相关阅读:
    LintCode "Maximum Gap"
    LintCode "Wood Cut"
    LintCode "Expression Evaluation"
    LintCode "Find Peak Element II"
    LintCode "Remove Node in Binary Search Tree"
    LintCode "Delete Digits"
    LintCode "Binary Representation"
    LeetCode "Game of Life"
    LintCode "Coins in a Line"
    LintCode "Word Break"
  • 原文地址:https://www.cnblogs.com/yvesliao/p/886422.html
Copyright © 2011-2022 走看看