Boh*_*ohn 3 c# cursor-position winforms
我的表单层次结构是这样的:
Form -> TableLayoutOne -> TableLayoutTwo -> Panel -> ListBox
Run Code Online (Sandbox Code Playgroud)
在ListBox的MouseMove事件中,我有这样的代码:
Point cursosPosition2 = PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y));
Control crp = this.GetChildAtPoint(cursosPosition2);
if (crp != null)
MessageBox.Show(crp.Name);
Run Code Online (Sandbox Code Playgroud)
MessageBox向我显示"TableLayoutOne",但我希望它向我显示"ListBox".我的代码在哪里,我错了?谢谢.
该GetChildFromPoint()方法使用本机ChildWindowFromPointEx()方法,其文档说明:
确定属于指定父窗口的子窗口中的哪些(如果有)包含指定的点.该函数可以忽略不可见,禁用和透明的子窗口.搜索仅限于直接子窗口.没有搜查孙子孙女和更深的后代.
请注意粗体文本:方法无法获得您想要的效果.
从理论上讲,你可以调用GetChildFromPoint()返回的控件,直到你得到null:
Control crp = this.GetChildAtPoint(cursosPosition2);
Control lastCrp = crp;
while (crp != null)
{
lastCrp = crp;
crp = crp.GetChildAtPoint(cursorPosition2);
}
Run Code Online (Sandbox Code Playgroud)
然后你就知道lastCrp那个位置是那个位置最低的后代.