C# 获取控件在窗体上的位置

编程入门 行业动态 更新时间:2024-10-12 12:33:47
本文介绍了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?

推荐答案

我通常将 PointToScreen 和 PointToClient 结合起来:

I usually combine PointToScreen and PointToClient:

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

更多推荐

C# 获取控件在窗体上的位置

本文发布于:2023-10-08 01:11:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471072.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   控件   位置

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!