如何为TreeView实现NodeMouseOut事件模拟?

时间:2015-10-29 17:12:36

标签: c# .net winforms treeview mouseout

我希望在用户鼠标hovers节点时显示一个特殊的弹出控件,并在鼠标离开节点时隐藏它。

第一部分我们有NodeMouseHover事件,但是如何处理隐藏程序?

1 个答案:

答案 0 :(得分:1)

可以使用计时器测试鼠标位置:

public class TreeViewEx : TreeView {
  public event TreeViewEventHandler NodeMouseLeave;

  private System.Windows.Forms.Timer timer;
  private TreeNode mouseNode = null;

  public TreeViewEx() {
    if (System.ComponentModel.LicenseManager.UsageMode == LicenseUsageMode.Runtime) {
      timer = new System.Windows.Forms.Timer();
      timer.Tick += timer_Tick;
      timer.Enabled = true;
    }
  }

  protected void OnNoseMouseLeave(TreeViewEventArgs e) {
    if (this.NodeMouseLeave != null) {
      this.NodeMouseLeave(this, e);
    }
  }

  void timer_Tick(object sender, EventArgs e) {
    if (this.ClientRectangle.Contains(this.PointToClient(MousePosition))) {
      TreeNode testNode = this.GetNodeAt(this.PointToClient(MousePosition));

      if (testNode == null) {
        if (mouseNode != null) {
          OnNoseMouseLeave(new TreeViewEventArgs(mouseNode));
          mouseNode = null;
        }
      } else {
        if (mouseNode != null && !testNode.Equals(mouseNode)) {
          OnNoseMouseLeave(new TreeViewEventArgs(mouseNode));
        }
        mouseNode = testNode;
      }
    } else {
      if (mouseNode != null) {
        OnNoseMouseLeave(new TreeViewEventArgs(mouseNode));
        mouseNode = null;
      }
    }
  }
}