如何控制鼠标光标?

发布于 2024-08-24 06:39:04 字数 67 浏览 0 评论 0 原文

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

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

I have form with few buttons and I want to know what button is under cursor now.

P.S. Maybe it's duplicate, but I can't find answer to this question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

公布 2024-08-31 06:39:04

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

Have a look at GetChildAtPoint. You will have to do some extra work if the controls are contained in a container, see Control.PointToClient.

牛↙奶布丁 2024-08-31 06:39:04

也许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;
}

这将使您可以在光标下方进行控制。

Maybe GetChildAtPoint and PointToClient is the first idea for most people. I also used it first. But, GetChildAtPoint doesn't work properly with invisible or overlapped controls. Here's a well-working code and it manages those situations.

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;
}

This will give you the control right under the cursor.

悲喜皆因你 2024-08-31 06:39:04
// 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;
        }
    }
// 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;
        }
    }
奶气 2024-08-31 06:39:04

您可以通过多种方式完成此操作:

  1. 监听表单控件的 MouseEnter 事件。 “sender”参数将告诉您哪个控件引发了该事件。

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

安德鲁

You could do it a number of ways:

  1. Listen to the MouseEnter event of your form's controls. The "sender" parameter will tell you what control raised the event.

  2. Obtain the cursor position using System.Windows.Forms.Cursor.Location and map it to your form's coordinates using Form.PointToClient(). You can then pass the point to Form.GetChildAtPoint() to find the control under that point.

Andrew

喵星人汪星人 2024-08-31 06:39:04

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

What about defining an on-Mouse-over event in each button
which assigns the sender button to a public variable of a button type

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文