调整窗口大小后保持winform控件居中 [英] Keeping winform control centered after window resize

查看:31
本文介绍了调整窗口大小后保持winform控件居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Visual Studio 2008、windows 窗体、C#、.NET 2.0 ...

Using Visual Studio 2008, windows forms, C#, .NET 2.0 ...

是否有一种无需代码的方法来让控件(在我的例子中恰好是一个 PictureBox)在窗口大小调整时保持居中?换句话说,使用一些属性设置的组合而不是手动编写代码来保持元素居中.

Is there a code-less way to get a control (it happens to be a PictureBox in my case) to stay centered as the window is resized? In other words, using some combination of property settings rather than writing code by hand to keep the element centered.

例如,在窗体窗口的中央有一个按钮,当用户调整窗口大小时,该按钮将根据其默认锚点(顶部、左侧)保持固定不动.无论窗口如何调整大小,我都希望它保持居中.

For example, there's a button in the center of a windows forms window, when the user resizes the window, the button will stay fixed in place based on its default anchoring (top,left). What I'd like it to do is to stay centered, regardless of how the window is resized.

我知道我可以监视窗口大小调整事件,然后让表单上的所有元素重新定位以保持在相同的相对位置(基于我编写的代码让它们这样做).我希望有一些属性设置的组合来实现相同的效果.

I'm aware that I could watch for a window resize event and then have all elements on the form reposition themselves to stay in their same relative positions (based on code I would write to have them do that). I'm hoping that there is some combination of property settings to acheive the same effect.

正如其他帖子中所建议的那样,我已经尝试使用 TableLayout 控件,但就将元素居中的特定解决方案而言,我可以在正确的方向上使用推力.

As suggested in other posts, I've tried using the TableLayout control, but could use a shove in the right direction in terms of the specific solution to centering an element.

推荐答案

将您的控件置于设计器中并将其 Anchor 属性设置为 None.

Center your control in the designer and set its Anchor property to None.

这篇关于调整窗口大小后保持winform控件居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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