有没有一种快速的方法来获得鼠标下的控制?

我需要在另一个控件的事件中找到鼠标下的控件。 我可以从GetTopLevel开始并使用GetChildAtPoint进行迭代,但有更快的方法吗?

这段代码没有多大意义,但它确实避免遍历Controls集合:

 [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern IntPtr WindowFromPoint(Point pnt); private void Form1_MouseMove(object sender, MouseEventArgs e) { IntPtr hWnd = WindowFromPoint(Control.MousePosition); if (hWnd != IntPtr.Zero) { Control ctl = Control.FromHandle(hWnd); if (ctl != null) label1.Text = ctl.Name; } } private void button1_Click(object sender, EventArgs e) { // Need to capture to see mouse move messages... this.Capture = true; } 

未经测试并脱离我的头顶(也许很慢……):

 Control GetControlUnderMouse() { foreach ( Control c in this.Controls ) { if ( c.Bounds.Contains(this.PointToClient(MousePosition)) ) { return c; } } } 

或者看上LINQ:

 return Controls.Where(c => c.Bounds.Contains(PointToClient(MousePosition))).FirstOrDefault(); 

不过,我不确定这会有多可靠。