C# 是否可以检测面板c中特定坐标处是否有控件#

C# 是否可以检测面板c中特定坐标处是否有控件#,c#,winforms,user-controls,C#,Winforms,User Controls,我在winforms应用程序中有一个面板,我想知道是否有可能在坐标处看到UserControl,例如x:200,y:200 我不仅想检测左上角为x:200,y:200的用户控件,还想检测一个用户控件,比如说,它位于x:150,y:150,W:100,H:100 我用谷歌快速搜索了一下,但什么也找不到 [编辑] 总而言之: 在黑色面板中,我希望能够检测到蓝色坐标处是否有用户控件(显示为红色),如图2所示,而不仅仅是图1所示 步骤1:对当前具有可见==true的所有窗口(或控件)进行迭代: 步骤2

我在winforms应用程序中有一个面板,我想知道是否有可能在坐标处看到UserControl,例如x:200,y:200

我不仅想检测左上角为x:200,y:200的用户控件,还想检测一个用户控件,比如说,它位于x:150,y:150,W:100,H:100

我用谷歌快速搜索了一下,但什么也找不到

[编辑] 总而言之:

黑色面板中,我希望能够检测到蓝色坐标处是否有用户控件(显示为红色),如图2所示,而不仅仅是图1所示


步骤1:对当前具有
可见==true的所有窗口(或控件)进行迭代:

步骤2:对于每个
控件
,使用
c.ClientRectangle
属性获取其相对于其父控件的边界框

步骤3:使用
c.RectangleToScreen(…)
方法将边界框转换为屏幕坐标


第4步:使用
r.Contains(Point pt)
方法测试鼠标是否在
矩形内。

第1步:对当前具有
可见==true的所有窗口(或控件)进行迭代:

步骤2:对于每个
控件
,使用
c.ClientRectangle
属性获取其相对于其父控件的边界框

步骤3:使用
c.RectangleToScreen(…)
方法将边界框转换为屏幕坐标

步骤4:使用
r.Contains(Point pt)
方法测试鼠标是否位于
矩形内。

您可以使用该方法,如下所示:

Control parent = your_panel;
Point pt = your_point;
// Uncomment if your point is in screen coordinates
// pt = parent.PointToClient(pt);
Control child = parent.GetChildAtPoint(pt);
if (child != null)
{
    // There is some control, but it might not be the one you are searching for.

    // Uncomment if you are searching for a direct child of your panel
    // while (child.Parent != parent) child = child.Parent;

    // Use other criterias. For instance:
    var userControl = child as UserControl;
    if (userControl != null)
    {
        // There you go.
    }
}
您可以使用这样的方法:

Control parent = your_panel;
Point pt = your_point;
// Uncomment if your point is in screen coordinates
// pt = parent.PointToClient(pt);
Control child = parent.GetChildAtPoint(pt);
if (child != null)
{
    // There is some control, but it might not be the one you are searching for.

    // Uncomment if you are searching for a direct child of your panel
    // while (child.Parent != parent) child = child.Parent;

    // Use other criterias. For instance:
    var userControl = child as UserControl;
    if (userControl != null)
    {
        // There you go.
    }
}

我不明白你的问题。如果您想知道某个特定点是否包含在
UserControl
的边界中,为什么不能调用该方法呢?您在这里遇到的具体问题是什么?请提供清楚地说明您迄今为止所做的尝试,并详细说明哪些不适合您。@PeterDuniho我已试图让事情变得更清楚。到目前为止,我找不到关于这个话题的任何东西,所以我无法展示我的尝试。我不理解你的问题。如果您想知道某个特定点是否包含在
UserControl
的边界中,为什么不能调用该方法呢?您在这里遇到的具体问题是什么?请提供清楚地说明您迄今为止所做的尝试,并详细说明哪些不适合您。@PeterDuniho我已试图让事情变得更清楚。我无法展示我所做的尝试,因为我在这个话题上找不到任何东西。