是否可以检测面板内特定坐标处是否有控件 - c#
Is it possible to detect if there is a control at a specific coordinate within a panel - c#
我在 winforms 应用程序中有一个面板,我想知道是否可以查看坐标处是否有 UserControl,例如 x:200、y:200.
我不仅想检测左上角位于 x:200、y:200 的 UserControl,而且还检测位于 x:150、x:150 的 UserControl。 =35=] 和 W:100, H:100.
我快速地给了它 google 但我找不到任何东西。
[编辑]
总结:
在 Black 面板中,我希望能够检测 Red 中是否有 UserControl =24=]蓝色 坐标如图 2 所示而不仅仅是图 1.
第 1 步:遍历所有 windows(或控制)当前 Visible == true
:How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?
第 2 步:对于每个 Control
,使用 c.ClientRectangle
属性 获取其相对于其父级的边界框。
第 3 步:使用 c.RectangleToScreen(...)
方法将边界框转换为屏幕坐标。
第 4 步:使用 r.Contains(Point pt)
方法测试鼠标是否在 Rectangle
内。
你可以使用 Control.GetChildAtPoint Method (Point),像这样:
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.
}
}
我在 winforms 应用程序中有一个面板,我想知道是否可以查看坐标处是否有 UserControl,例如 x:200、y:200.
我不仅想检测左上角位于 x:200、y:200 的 UserControl,而且还检测位于 x:150、x:150 的 UserControl。 =35=] 和 W:100, H:100.
我快速地给了它 google 但我找不到任何东西。
[编辑] 总结:
在 Black 面板中,我希望能够检测 Red 中是否有 UserControl =24=]蓝色 坐标如图 2 所示而不仅仅是图 1.
第 1 步:遍历所有 windows(或控制)当前 Visible == true
:How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?
第 2 步:对于每个 Control
,使用 c.ClientRectangle
属性 获取其相对于其父级的边界框。
第 3 步:使用 c.RectangleToScreen(...)
方法将边界框转换为屏幕坐标。
第 4 步:使用 r.Contains(Point pt)
方法测试鼠标是否在 Rectangle
内。
你可以使用 Control.GetChildAtPoint Method (Point),像这样:
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.
}
}