开发者

Get controls on Mouse Over

开发者 https://www.devze.com 2023-01-07 17:41 出处:网络
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 ca

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 开发者_JAVA百科get the name of that control?


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.


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

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


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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消