GetChildAtPoint如何工作? [英] How GetChildAtPoint works?

查看:234
本文介绍了GetChildAtPoint如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在(x,y)位置有control1.现在,我通过par = control1.Parent
获得其父控件. 接下来,我想从父级"par"重新获得control1.我使用方法"GetChildAtPoint",如control2 = par.GetChildAtPoint(new Point(x + 1,y + 1))
它应该工作并返回control2 = control1,但实际上它返回control2 = null.
我不知道它是如何工作的.请你帮助我好吗?我真的很想以相同的方式编写一些代码(从父控件中重新获得子控件,而该父控件已使用方法Parent从原来的子控件中获取了.)
非常感谢!

I have control1 at location (x,y). Now I get its parent control by par = control1.Parent
Next, I want to regain my control1 from the parent "par". I use the method "GetChildAtPoint" like as control2 = par.GetChildAtPoint(new Point(x+1, y+1))
It should work and return control2 = control1, but in fact it returns control2=null.
I don''t know how it works. Could you please help me? I really want to write some codes doing the same way (regaining the child control from the parent control which has been taken from the original child control with the method Parent).
Thank you so much!

推荐答案

这里是一个解释:

http://msdn.microsoft.com/en-us/library/a6zktd23.aspx [ ^ ]

我能想到它不会在哪里返回控件的唯一原因是该控件已禁用或不可见,但是我不知道,因为我自己还没有实际尝试过.
Here''s an explanation:

http://msdn.microsoft.com/en-us/library/a6zktd23.aspx[^]

The only reason I can think of where it wouldn''t return the control is that the control is disabled or not visible, but I don''t know since I haven''t actually tried it myself.


我认为(没有看到您的代码)您使用的位置错误.
我尝试了您似乎正在描述的代码,它的工作原理是:
I think (without seeing your code) that you are using the wrong location.
I tried the code you seem to be describing, and it works:
private void button1_Click(object sender, EventArgs e)
    {
    Button b = sender as Button;
    Control f = b.Parent;
    Control c = f.GetChildAtPoint(new Point(b.Left + 1, b.Top + 1));
    Control d = f.GetChildAtPoint(new Point(b.Location.X + 1, b.Location.Y + 1));
    if (b == c && c == d)
        {
        MessageBox.Show("Same");
        }
    }

正如我所期望的那样,b == c == d.
您是从子顶部和左侧"(或X和Y)还是表格构造点的?

As I would expect, b == c == d.
Did you construct the point from the Child Top and Left (or X and Y) or the form?


我检查了它:即使您不加1,它也可以正常工作进行协调.可能的问题:1)您应该在父坐标系中使用(x, y); 2)如果未显示用户界面,它将无法正常工作;例如,如果从表单构造函数中调用该代码将不起作用.

考虑一下,例如:

I checked it up: it works correctly, even if you don''t add 1 to coordinated. Possible problems: 1) you should take (x, y) in the coordinate system of parent; 2) it won''t work if the UI is not shown; for example, the code will not work if called from the form constructor.

Consider this, for example:

public Form1() {
    InitializeComponent();
    Control ctl;
    ctl = this.GetChildAtPoint(label1.Location); //will return null
    this.Shown += (sender, eventArgs) => {
        ctl = this.GetChildAtPoint(label1.Location); //will return label1; 
    };
}



—SA



—SA


这篇关于GetChildAtPoint如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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