获取Mouse Over上的控件

时间:2010-07-19 13:20:50

标签: c# winforms mouseover

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

3 个答案:

答案 0 :(得分:1)

Control.GetChildAtPoint Method (Point)Control.GetChildAtPoint Method (Point, GetChildAtPointSkip)做你需要的。

但在您的情况下,您可能还会执行以下操作: 对于面板中的每个子项,为子项的mouseover事件添加一个侦听器,并在该侦听器中检查sender参数。

答案 1 :(得分:0)

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

$('#outer')。mouseover(function(){   //在这里获得控制权 });

答案 2 :(得分:0)

我必须做类似的事情来获取用户点击的控件的名称。你的情况是鼠标悬停,但同样的方法可能会有效。我最终使用了:

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

}