【问题标题】:How to get control under mouse cursor? [duplicate]如何在鼠标光标下获得控制? [复制]
【发布时间】:2010-03-09 17:28:36
【问题描述】:

我有几个按钮的表单,我想知道现在光标下的按钮是什么。

附:也许它是重复的,但我找不到这个问题的答案。

【问题讨论】:

    标签: c# winforms mouse


    【解决方案1】:

    也许GetChildAtPointPointToClient 是大多数人的第一个想法。我也是先用的。但是,GetChildAtPoint 不适用于不可见或重叠的控件。这是一个运行良好的代码,它可以管理这些情况。

    using System.Drawing;
    using System.Windows.Forms;
    
    public static Control FindControlAtPoint(Control container, Point pos)
    {
        Control child;
        foreach (Control c in container.Controls)
        {
            if (c.Visible && c.Bounds.Contains(pos))
            {
                child = FindControlAtPoint(c, new Point(pos.X - c.Left, pos.Y - c.Top));
                if (child == null) return c;
                else return child;
            }
        }
        return null;
    }
    
    public static Control FindControlAtCursor(Form form)
    {
        Point pos = Cursor.Position;
        if (form.Bounds.Contains(pos))
            return FindControlAtPoint(form, form.PointToClient(pos));
        return null;
    }
    

    这会给你光标下的控制权。

    【讨论】:

    • 您可以添加GetChildAtPointSkip.Invisible 作为第二个参数...忽略不可见的控件。
    【解决方案2】:

    看看GetChildAtPoint。如果控件包含在容器中,您将不得不做一些额外的工作,请参阅Control.PointToClient

    【讨论】:

    • +1 用于 Control.PointToClient
    【解决方案3】:

    您可以通过多种方式做到这一点:

    1. 收听表单控件的MouseEnter 事件。 “sender”参数会告诉你是哪个控件引发了事件。

    2. 使用System.Windows.Forms.Cursor.Location 获取光标位置,并使用Form.PointToClient() 将其映射到表单的坐标。然后您可以将该点传递给Form.GetChildAtPoint() 以找到该点下的控件。

    安德鲁

    【讨论】:

      【解决方案4】:
      // This getYoungestChildUnderMouse(Control) method will recursively navigate a       
      // control tree and return the deepest non-container control found under the cursor.
      // It will return null if there is no control under the mouse (the mouse is off the
      // form, or in an empty area of the form).
      // For example, this statement would output the name of the control under the mouse
      // pointer (assuming it is in some method of Windows.Form class):
      // 
      // Console.Writeline(ControlNavigatorHelper.getYoungestChildUnderMouseControl(this).Name);
      
      
          public class ControlNavigationHelper
          {
              public static Control getYoungestChildUnderMouse(Control topControl)
              {
                  return ControlNavigationHelper.getYoungestChildAtDesktopPoint(topControl, System.Windows.Forms.Cursor.Position);
              }
      
              private static Control getYoungestChildAtDesktopPoint(Control topControl, System.Drawing.Point desktopPoint)
              {
                  Control foundControl = topControl.GetChildAtPoint(topControl.PointToClient(desktopPoint));
                  if ((foundControl != null) && (foundControl.HasChildren))
                      return getYoungestChildAtDesktopPoint(foundControl, desktopPoint);
                  else
                      return foundControl;
              }
          }
      

      【讨论】:

        【解决方案5】:

        如何在每个按钮中定义一个鼠标悬停事件 它将发送者按钮分配给按钮类型的公共变量

        【讨论】:

        • 其实我不是按钮,是第三方插件;)
        猜你喜欢
        • 1970-01-01
        • 2011-05-04
        • 1970-01-01
        • 2019-03-22
        • 1970-01-01
        • 1970-01-01
        • 2021-11-11
        • 1970-01-01
        • 2011-03-14
        相关资源
        最近更新 更多