.net 根据分辨率调整表单/控件大小的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1786277/
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
Best way to resize form/controls according to resolution?
提问by broke
I was an idiot and designed my VB app on a 17inch monitor in a 1280X1024 resolution completely forgetting about what it would like on another machine. This might be a long shot, but is there an easy way to get the resolution of the users monitor and re-size the controls and form accordingly?
我是个白痴,在 1280X1024 分辨率的 17 英寸显示器上设计了我的 VB 应用程序,完全忘记了它在另一台机器上会是什么样子。这可能是一个长镜头,但是有没有一种简单的方法来获得用户监视器的分辨率并相应地重新调整控件和表单的大小?
Thanks in advance.
提前致谢。
回答by John
You can put all of the controls in a TableLayoutPanel, anchor everything correctly, and resize the window to an appropriate size for the display resolution. The controls will resize with the window if you anchor the entire panel on top, bottom, left, and right, and anchor individual text boxes, combo boxes, etc., within the cells.
您可以将所有控件放在 TableLayoutPanel 中,正确定位所有控件,并将窗口大小调整为适合显示分辨率的大小。如果您将整个面板锚定在顶部、底部、左侧和右侧,并将单个文本框、组合框等锚定在单元格内,则控件将随窗口调整大小。
Takes a little work to figure out how you want things to resize and move around, but not that much.
需要做一些工作来弄清楚您希望事物如何调整大小和移动,但不是那么多。
You can figure out what fraction of the screen you want to take up with your window after getting the numbers from TLiebe's method.
在从 TLiebe 的方法中获取数字后,您可以计算出要在窗口中占据的屏幕部分。
回答by TLiebe
You can get the current screen dimensions with the following:
您可以通过以下方式获取当前屏幕尺寸:
Dim screenHeight As Integer = My.Computer.Screen.Bounds.Height
Dim screenWidth As Integer = My.Computer.Screen.Bounds.Width
As for resizing, if you anchor your controls to the form you should be able to re-size the form to fit the dimensions of the screen as obtained above.
至于调整大小,如果您将控件锚定到表单上,您应该能够调整表单的大小以适应上面获得的屏幕尺寸。
回答by xpda
You can anchor the controls in a form to the top, bottom, left or right. This makes it possible to design the form so the size can change without messing up all the controls. You can do this in Design Mode, setting the anchor property of each control.
您可以将表单中的控件锚定到顶部、底部、左侧或右侧。这使得可以设计表单,以便在不弄乱所有控件的情况下更改大小。您可以在设计模式下执行此操作,设置每个控件的锚点属性。
There is a minimum (and maximum) size property for the form you can use to keep it from getting too small for its controls. You can use Screen.PrimaryScreen.Bounds or My.Computer.Screen.Bounds to get the screen size and set the form size accordingly, or possibly maximize the form if the screen is below a certain size.
表单有一个最小(和最大)大小属性,您可以使用它来防止它变得太小而无法控制。您可以使用 Screen.PrimaryScreen.Bounds 或 My.Computer.Screen.Bounds 来获取屏幕大小并相应地设置表单大小,或者如果屏幕小于特定大小,则可以最大化表单。
回答by OldProgrammer
Understanding Anchor.
了解锚。
Create a blank form.
创建一个空白表格。
Set the form's size to MyComputer.screen.bounds.area width and height.
将表单的大小设置为 MyComputer.screen.bounds.area 的宽度和高度。
Set the form's WindowSize to Maximum.
将窗体的 WindowSize 设置为最大值。
Place a panel control in the middle.
在中间放置一个面板控件。
Put 4 option buttons in the panel, 2 rows each.
在面板中放置 4 个选项按钮,每个按钮 2 行。
Reduce the size of the panel so that the option buttons just fit.
减小面板的尺寸,使选项按钮刚好适合。
Anchor the panel to all 4 sides of the form.
将面板固定到表格的所有 4 边。
Start the app and select the minimize button and the form's sides will now be accesible to shrink and stretch.
启动应用程序并选择最小化按钮,表单的侧面现在可以收缩和拉伸。
Begin shrinking the form from any side and notice how the panel's sides move with the direction of the shrinking.
从任何一侧开始收缩表单,并注意面板的两侧如何随收缩方向移动。
Change the anchoring of the panel to top and left.
将面板的锚定更改为顶部和左侧。
If you shrink from the top or left the panel eventually scrolls toward the opposite edge.
如果您从顶部收缩或向左收缩,面板最终会向相反的边缘滚动。
If you shrink from the bottom or right the form' edge scrolls toward the panel.
如果从底部或右侧缩小,则表单的边缘会向面板滚动。
回答by MunsterMan
Here's the code I used to resize my form on loadup
这是我用来在加载时调整表单大小的代码
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Me.Height = My.Computer.Screen.WorkingArea.Height
MyBase.OnLoad(e)
End Sub

