我需要在另一个控件的事件中找到鼠标下的控件。我可以从 GetTopLevel
开始,然后使用 GetChildAtPoint
进行迭代,但是有没有更快的方法?
最佳答案
这段代码没有多大意义,但它确实避免了遍历控件集合:
[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;
}
关于c# - 有没有快速的方法来获得鼠标下的控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/586479/