C# 获取控件在窗体上的位置 [英] C# Get a control's position on a form

查看:63
本文介绍了C# 获取控件在窗体上的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当控件可能位于其他控件(如面板)内时,有没有办法在表单中检索控件的位置?

Is there any way to retrieve a control's position in a form, when the control may be inside other controls (like Panels)?

控件的 Left 和 Top 属性只给我它在父控件中的位置,但是如果我的控件位于五个嵌套面板内,并且我需要它在表单上的位置怎么办?

The control's Left and Top properties gives me only it's position within it's parent control, but what if my control is inside five nested panels, and I need it's position on the form?

快速示例:

按钮 btnA 位于面板 pnlB 内的坐标 (10,10).
面板 pnlB 位于窗体 frmC 内的坐标 (15,15) 上.

The button btnA is located on coordinates (10,10) inside the panel pnlB.
The panel pnlB is located on coordinates (15,15) inside the form frmC.

我想要 btnA 在 frmC 上的位置,即 (25,25).

I want btnA's location on frmC, which is (25,25).

我可以得到这个位置吗?

Can I get this location?

推荐答案

我通常将 PointToScreenPointToClient 结合起来:

I usually combine PointToScreen and PointToClient:

Point locationOnForm = control.FindForm().PointToClient(
    control.Parent.PointToScreen(control.Location));

这篇关于C# 获取控件在窗体上的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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