在 C# 中调整窗口大小时如何将对象保持在原位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12972874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:09:30  来源:igfitidea点击:

how to keep objects in place when window is resized in C#

c#winformsresizewindow

提问by PHP Developer

How can I keep the objects of my window (buttons, labels, etc) in center when the window is resized?

调整窗口大小时,如何将窗口的对象(按钮、标签等)保持在中心?

Currently, I have 3 buttons in a Windows Form. When I maximize the window, the buttons stay at the top left section of the window. I want them to be in the center as they were when the window was not maximized.

目前,我在 Windows 窗体中有 3 个按钮。当我最大化窗口时,按钮停留在窗口的左上角。我希望它们像窗口未最大化时一样位于中心。

Any help?

有什么帮助吗?

回答by SirDuckduck

You should set the Anchor properties of the object to none,

您应该将对象的 Anchor 属性设置为none

This will keep it in the middle.

这将保持在中间。

回答by LightStriker

If you are using the visual designer of Visual Studio (And you have no reason not to), the property of your control you seek to manage how they are placed inside a form is "Anchor". By default, when you create a new control, it is set to "Top-Left", which mean they would stay in a fixed position to the top left of your form. You can change that to anchor them to anything.

如果您正在使用 Visual Studio 的可视化设计器(并且您没有理由不这样做),那么您寻求管理它们如何放置在表单中的控件的属性是“锚点”。默认情况下,当您创建一个新控件时,它被设置为“左上角”,这意味着它们将保持在表单左上角的固定位置。您可以更改它以将它们锚定到任何东西。

You can also disable the anchors and control their position by overriding the Resize method of the form.

您还可以通过覆盖表单的 Resize 方法来禁用锚点并控制它们的位置。

回答by Dan Puzey

Set the Anchorproperty of your controls correctly. By default your control is anchored to Top,Left. If you clear this property (anchor to nothing, essentially), your button will remain centered.

Anchor正确设置控件的属性。默认情况下,您的控件锚定到Top,Left. 如果您清除此属性(本质上锚定为空),您的按钮将保持居中。

(It may seem like you want to anchor to all four sides, but in reality what this will do is resize your button to fill the form!)

(看起来您想锚定到所有四个边,但实际上这样做是调整按钮的大小以填充表单!)

回答by Grzegorz W

To keep your layout fixed and in the middle do this:

要保持布局固定并在中间,请执行以下操作:

  1. On your Formadd TableLayoutPanel.
  2. Set it's Dockproperty to Fill.
  3. Create 3 rows and 3 columns.
  4. Edit rows and columns - set 50% for first and last column and row.
  5. Set fixed size for middle row and column.
  6. Place Panelor anything else you like in 2nd row and 2nd column. It will always be in the middle.
  1. 在您Form添加TableLayoutPanel.
  2. 将其Dock属性设置为Fill.
  3. 创建 3 行 3 列。
  4. 编辑行和列 - 为第一和最后一列和行设置 50%。
  5. 为中间行和列设置固定大小。
  6. Panel在第 2 行和第 2 列中放置您喜欢的任何内容。它永远在中间。