简体   繁体   English

找到.NET用户控件的中心

[英]Find the center of a .NET User Control

I'd like to display an image in the center of my user control, but I'm drawing a blank on actually "finding" the center of the control! 我想在我的用户控件的中心显示一个图像,但我实际上“找到”控件的中心了!

I feel like this should be something very simple but I just can't get my head around it. 我觉得这应该是非常简单的事情,但我无法理解它。

Ideally I'd like to end up with X & Y coordinates which can be added to an instance of Point in order to display at that point. 理想情况下,我希望最终得到X和Y坐标,可以将其添加到Point的实例中,以便在该点显示。

You can just use ctrl.Width / 2, ctrl.Height / 2 . 你可以使用ctrl.Width / 2, ctrl.Height / 2
You may want to add ctrl.Left and ctrl.Top . 您可能想要添加ctrl.Leftctrl.Top

如果图像应放在中心,请使用以下代码:(伪,未经测试)

image.Location = new Point((control.Width / 2) - (image.Width /2),(control.Height / 2) - (image.Height / 2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM