获取鼠标悬停控件

发布于 2024-09-10 07:48:22 字数 112 浏览 3 评论 0原文

我正在开发 C# .NET 应用程序。我的应用程序使用 TablePanelLayout 作为容器。它包含很多子控件(Label、TextBox、Button...)。当鼠标移到控件上时,如何获取该控件的名称?

I am working on a C# .NET application. My application uses TablePanelLayout as a container. It contains a lot of child controls (Label, TextBox, Button...). When the mouse moves over a control, how can I get the name of that control?

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

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

发布评论

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

评论(3

梅窗月明清似水 2024-09-17 07:48:22

Control.GetChildAtPoint 方法(点)Control.GetChildAtPoint 方法(Point、GetChildAtPointSkip) 执行您需要的操作。

但根据您的情况,您也可以执行以下操作:
对于面板中的每个子项,为子项的鼠标悬停事件添加一个侦听器,并在该侦听器中检查 sender 参数。

Control.GetChildAtPoint Method (Point) and Control.GetChildAtPoint Method (Point, GetChildAtPointSkip) do what you need.

But in your case you may also do the following:
for each child in your panel add a listener to the child's mouseover event and in that listener check the sender parameter.

清醇 2024-09-17 07:48:22

你可以做这样的事情,使用 jquery 来获取鼠标悬停时的功能。

$('#outer').mouseover(function() {
//这里获取控制权
});

you can do something like this, use jquery to get the function on mouse over.

$('#outer').mouseover(function() {
//get the control here
});

独木成林 2024-09-17 07:48:22

我必须做类似的事情来获取用户单击的控件的名称。您的案例已将鼠标悬停,但相同的方法可能会起作用。我最终使用:

Application.AddMessageFilter(new MyMessageFilter());

在程序启动时。 MyMessageFilter 的基础知识看起来像这样。您必须适应鼠标移动。

class MyMessageFilter : IMessageFilter
{
    public bool PreFilterMessage(ref Message msg)
    {
        try
        {
            const int WM_LBUTTONUP = 0x0202;
            const int WM_RBUTTONUP = 0x0205;
            const int WM_CHAR = 0x0102;
            const int WM_SYSCHAR = 0x0106;
            const int WM_KEYDOWN = 0x0100;
            const int WM_SYSKEYDOWN = 0x0104;
            const int WM_KEYUP = 0x0101;
            const int WM_SYSKEYUP = 0x0105;

            //Debug.WriteLine("MSG " + msg.Msg.ToString("D4") + " 0x" + msg.Msg.ToString("X4"));

            switch (msg.Msg)
            {
                case WM_LBUTTONUP:
                case WM_RBUTTONUP:
                    {
                        Point screenPos = Cursor.Position;
                        Form activeForm = Form.ActiveForm;

                        if (activeForm != null)
                        {
                            Point clientPos = activeForm.PointToClient(screenPos);

                            RecordMouseUp(clientPos.X, clientPos.Y, GetFullControlName(msg.HWnd));
                        }
                    }
            }
        }
    }

    private string GetFullControlName(IntPtr hwnd)
    {
        Control control = Control.FromHandle(hwnd);
        return control.Name; // May need to iterate up parent controls to get a full path.
    }      

}

I had to do something similar to get the name of the control the user clicked on. Your case is mouse over, but the same approach will probably work. I ended up using:

Application.AddMessageFilter(new MyMessageFilter());

at program startup. The basics of MyMessageFilter looks something like this. You'll have to adapt for mouse moves.

class MyMessageFilter : IMessageFilter
{
    public bool PreFilterMessage(ref Message msg)
    {
        try
        {
            const int WM_LBUTTONUP = 0x0202;
            const int WM_RBUTTONUP = 0x0205;
            const int WM_CHAR = 0x0102;
            const int WM_SYSCHAR = 0x0106;
            const int WM_KEYDOWN = 0x0100;
            const int WM_SYSKEYDOWN = 0x0104;
            const int WM_KEYUP = 0x0101;
            const int WM_SYSKEYUP = 0x0105;

            //Debug.WriteLine("MSG " + msg.Msg.ToString("D4") + " 0x" + msg.Msg.ToString("X4"));

            switch (msg.Msg)
            {
                case WM_LBUTTONUP:
                case WM_RBUTTONUP:
                    {
                        Point screenPos = Cursor.Position;
                        Form activeForm = Form.ActiveForm;

                        if (activeForm != null)
                        {
                            Point clientPos = activeForm.PointToClient(screenPos);

                            RecordMouseUp(clientPos.X, clientPos.Y, GetFullControlName(msg.HWnd));
                        }
                    }
            }
        }
    }

    private string GetFullControlName(IntPtr hwnd)
    {
        Control control = Control.FromHandle(hwnd);
        return control.Name; // May need to iterate up parent controls to get a full path.
    }      

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