GetChildAtPoint 方法返回错误的控件 [英] GetChildAtPoint method is returning the wrong control

查看:29
本文介绍了GetChildAtPoint 方法返回错误的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单层次结构是这样的:

表格 ->TableLayoutOne ->TableLayoutTwo ->面板 ->列表框

在 ListBox 的 MouseMove 事件中,我有这样的代码:

 Point cursosPosition2 = PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y));Control crp = this.GetChildAtPoint(cursosPosition2);如果(crp != 空)MessageBox.Show(crp.Name);

MessageBox 显示TableLayoutOne",但我希望它显示ListBox".我的代码哪里出错了?谢谢.

解决方案

GetChildFromPoint() 方法使用原生的 ChildWindowFromPointEx() 方法,其文档说明:><块引用>

确定哪些子窗口(如果有)属于指定的父窗口包含指定的点.该功能可以忽略不可见、禁用和透明的子窗口.搜索仅限于直接子窗口.孙子和更深的不搜索后代.

注意粗体文本:该方法无法得到您想要的.

理论上你可以在返回的控件上调用 GetChildFromPoint() 直到你得到 null :

Control crp = this.GetChildAtPoint(cursosPosition2);控制 lastCrp = crp;while (crp != null){lastCrp = crp;crp = crp.GetChildAtPoint(cursorPosition2);}

然后您就会知道 lastCrp 是该位置的最低后代.

My form hierarchy is something like this:

Form -> TableLayoutOne -> TableLayoutTwo -> Panel -> ListBox

In the MouseMove event of the ListBox, I have code like this:

    Point cursosPosition2 = PointToClient(new Point(Cursor.Position.X, Cursor.Position.Y));
    Control crp = this.GetChildAtPoint(cursosPosition2);
    if (crp != null)
        MessageBox.Show(crp.Name);

The MessageBox is showing me "TableLayoutOne", but I expect it to show me "ListBox". Where in my code am I going wrong? Thanks.

解决方案

The GetChildFromPoint() method uses the native ChildWindowFromPointEx() method, whose documentation states:

Determines which, if any, of the child windows belonging to the specified parent window contains the specified point. The function can ignore invisible, disabled, and transparent child windows. The search is restricted to immediate child windows. Grandchildren and deeper descendants are not searched.

Note the bolded text: the method can't get what you want.

In theory you could call GetChildFromPoint() on the returned control until you got null:

Control crp = this.GetChildAtPoint(cursosPosition2);
Control lastCrp = crp;

while (crp != null)
{
    lastCrp = crp;
    crp = crp.GetChildAtPoint(cursorPosition2);
}

And then you'd know that lastCrp was the lowest descendant at that position.

这篇关于GetChildAtPoint 方法返回错误的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆