Windows 窗体中的内容在最大化时移动 - C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/154270/
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-09-15 11:21:18  来源:igfitidea点击:

Stuff in Windows Form Move When Maximized - C#

c#windowsuser-interface

提问by AlbertEngelB

It's been a while since I've programmed a GUI program, so this may end up being super simple, but I can't find the solution anywhere online.

我已经有一段时间没有编写 GUI 程序了,所以这可能最终变得非常简单,但我无法在网上的任何地方找到解决方案。

Basically my problem is that when I maximize my program, all the things inside of the window (buttons, textboxes, etc.) stay in the same position in the window, which results in a large blank area near the bottom and right side.

基本上我的问题是当我最大化我的程序时,窗口内的所有东西(按钮、文本框等)都停留在窗口中的相同位置,这导致底部和右侧附近有一个大的空白区域。

Is there a way of making the the elements in the program to stretch to scale?

有没有办法让程序中的元素按比例拉伸?

回答by Brian Ensink

You want to check and properly set the Anchor and Dock properties on each control in the Form. The Anchor property on a control tells which sides of the form (top, bottom, left, right) the control is 'anchored' to. When the form is resized, the distance between the control and its anchors will stay the same. This lets you make a control stay in the bottom right corner for example.

您想检查并正确设置窗体中每个控件上的 Anchor 和 Dock 属性。控件上的 Anchor 属性表明控件“锚定”到窗体的哪一侧(顶部、底部、左侧、右侧)。调整窗体大小时,控件与其锚点之间的距离将保持不变。例如,这使您可以将控件保留在右下角。

The Dock property instructs the control to fill the entire parent form or to fill one side of it (again top, bottom, left or right).

Dock 属性指示控件填充整个父窗体或填充它的一侧(再次顶部、底部、左侧或右侧)。

回答by Steven A. Lowe

Anchor and Dock properties

锚点和码头属性

回答by Franci Penov

Look at the Dynamic Layout: Anchoring and Docking sample at http://msdn.microsoft.com/en-us/library/aa289756(VS.71).aspx

http://msdn.microsoft.com/en-us/library/aa289756(VS.71).aspx查看动态布局:锚定和停靠示例

It's in VB, but the concepts and the propeties/methods you need are the same in C#.

它在 VB 中,但您需要的概念和属性/方法在 C# 中是相同的。

回答by Joel Coehoorn

There are some layout panel controls that help you keep things proportioned as the form expands/contracts:

有一些布局面板控件可帮助您在表单展开/收缩时保持比例:

TableLayoutPanel
FlowLayoutPanel

TableLayoutPanel
FlowLayoutPanel

回答by Brian Ensink

As to layouts, I'm not quite sure what you mean, but I'm using Visual Studio 2008's default GUI editor.

至于布局,我不太清楚你的意思,但我使用的是 Visual Studio 2008 的默认 GUI 编辑器。

There are some special 'container' type panels that you could stick on your form such as FlowLayoutPaneland TableLayoutPanel. These types of containers have additional layout behavior.

有一些特殊的“容器”类型面板可以粘贴在表单上,​​例如FlowLayoutPanelTableLayoutPanel。这些类型的容器具有额外的布局行为。

If you find that some of your controls still don't want to behave during resize, then use the right-click context menu of the control to list the controls ancestors: its parent, its parent's parent, etc. You may find that the troublesome control is a child of some special container which has its own layout rules.

如果你发现你的某些控件在调整大小时仍然不想表现,那么使用控件的右键单击上下文菜单列出控件的祖先:它的父级、它的父级的父级等。你可能会发现麻烦control 是一些特殊容器的子级,它有自己的布局规则。