vb.net 根据屏幕大小自动调整 winform 和控件

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

Automatically adjusting winform and controls to screen size

vb.netwinforms

提问by Jose M.

I created a winform application. The size of each screen is 1361, 768 in pixels. This worked great for larger screens and/or laptops. But now I have to move my application to 10inch screen tablets, which means my application does not fit.

我创建了一个 winform 应用程序。每个屏幕的大小为 1361, 768 像素。这对于大屏幕和/或笔记本电脑非常有用。但是现在我必须将我的应用程序移到 10 英寸屏幕的平板电脑上,这意味着我的应用程序不适合。

I have never had to deal with this issue before, how can auto adjust each form size and adjust all of the controls and panels when viewing on smaller screens?

我以前从未处理过这个问题,在较小的屏幕上查看时如何自动调整每个表单大小并调整所有控件和面板?

I am using VS 2012.

我正在使用 VS 2012。

回答by Cody Gray

Making forms fully scalable in WinForms is possible, but it takes a bit of work. The good news is that most of this work is done at design-time, arranging the controls properly so that everything is done for you automatically by the framework. It's drudgery, but it isn't difficult. Rejoice that you don't have to write the scaling code by hand, form-by-form, like you did with VB 6.

在 WinForms 中使表单完全可扩展是可能的,但这需要一些工作。好消息是,大部分工作是在设计时完成的,正确安排控件,以便框架自动为您完成所有工作。这是苦差事,但并不难。庆幸您不必像使用 VB 6 那样逐个表单地手动编写缩放代码。

There are four fundamental properties that you will need to acquaint yourself with:

您需要熟悉四个基本属性:

The last two should be quite familiar web developers who know CSS—they do the same thing here. Padding controls the inner margin around a control, while margin controls the outer margin. You will need to set these correctly to ensure that your controls can "breathe", because the automatic scaling code is just going to jam them up against one another.

最后两个应该是熟悉 CSS 的非常熟悉的 Web 开发人员——他们在这里做同样的事情。填充控制控件周围的内边距,而边距控制外边距。您需要正确设置这些以确保您的控件可以“呼吸”,因为自动缩放代码只会将它们相互干扰。

The "standard" margins around a control in a Windows desktop application are approximately 12–15 pixels. You should make sure that you leave at least this much room. Then add additional margins/padding as you see fit to separate things. I keep these layout specificationsbookmarked for reference. Thisis another good reference.

Windows 桌面应用程序中控件周围的“标准”边距约为 12-15 像素。您应该确保至少留出这么多空间。然后添加额外的边距/填充,因为你认为适合分开的东西。我保留这些布局规范书签以供参考。是另一个很好的参考。

The next step is to instruct the layout manager how you want the controls to be arranged and resized. The key to this is to think in terms of containercontrols and childcontrols. The form itself is a container control, and you can set its childcontrols to either Anchor or Dock within its boundaries. One or more of those child controls can itself be a container control, and its child controls can be Anchored or Docked within its borders. The nesting is virtually unlimited, but for your own sanity and reasonable redraw performance, you'll want to keep it to a reasonable minimum.

下一步是指示布局管理器您希望如何排列和调整控件的大小。关键是要考虑容器控件和控件。窗体本身是一个容器控件,您可以在其边界内将其控件设置为 Anchor 或 Dock。这些子控件中的一个或多个本身可以是容器控件,并且其子控件可以锚定或停靠在其边界内。嵌套实际上是无限的,但为了您自己的理智和合理的重绘性能,您需要将其保持在合理的最低限度。

A good way of doing this is to use the two provided invisible layout helpers, FlowLayoutPaneland TableLayoutPanel. Personally, I don't find the former very useful very often, at least not for standard Windows applications. But the TableLayoutPanel is invaluable.

这样做的一个好方法是使用提供的两个不可见布局助手FlowLayoutPanelTableLayoutPanel. 就我个人而言,我不觉得前者非常有用,至少对于标准的 Windows 应用程序不是。但是 TableLayoutPanel 是无价的。

Generally what I will do is fill my entire form with a TableLayoutPanel (margins = 0, dock = fill). Then I will add individual controls (or sometimes another nested TableLayoutPanel) to its cells. Those child controls will have their margins set appropriately, and will have either their Anchor or Dock properties set, depending on whether I want that control to have a fixed size or resize dynamically.

通常我会做的是用 TableLayoutPanel(边距 = 0,停靠 = 填充)填充我的整个表单。然后我将向其单元格添加单个控件(或有时另一个嵌套的 TableLayoutPanel)。这些子控件将适当地设置它们的边距,并且将设置它们的 Anchor 或 Dock 属性,具体取决于我是否希望该控件具有固定大小或动态调整大小。

Before you get the hang of how these properties interact and how it all works, you'll probably need to play around with your layout a bit. Make a backup of your forms and then just dig in. Or, you might find it easier to start designing each form from scratch (you can still copy-and-paste individual controls in order to preserve their other properties). Eventually, it will all start making sense to you, and you'll be up and going in a jiffy.

在了解这些属性如何交互以及它们如何工作之前,您可能需要稍微调整一下布局。备份您的表单,然后深入研究。或者,您可能会发现从头开始设计每个表单更容易(您仍然可以复制并粘贴单个控件以保留它们的其他属性)。最终,这一切都会对你产生意义,你很快就会站起来。

The great thing is, once this is all set up, all you have to do is ensure that your form is resizable. Then, whether the user manually resizes it or uses the maximize/restore button, it'll automatically fill their screen size. This also works well for all DPI settings, which is another common Achilles' heel of WinForms devs.

最棒的是,一旦这一切都设置好了,你所要做的就是确保你的表单可以调整大小。然后,无论用户手动调整大小还是使用最大化/恢复按钮,它都会自动填充他们的屏幕大小。这也适用于所有 DPI 设置,这是 WinForms 开发人员的另一个常见致命弱点。

回答by fsalazar_sch

Try to get the resolutions variables to adjust your screens, there is an answer to get these variables using the Screen class

尝试获取分辨率变量来调整您的屏幕,有一个使用 Screen 类获取这些变量的答案

Getting Screen Resolution

获取屏幕分辨率

回答by user3994976

DevExpress has a great control call the Layout Control. This control helps to maintain consistent whitespace between controls as the form is resized. It does take a little study to use the control effectively but once you understand how to use this control the results are consistent and you are able to speed through form design.

DevExpress 有一个很棒的控件叫做布局控件。此控件有助于在调整窗体大小时在控件之间保持一致的空白。有效地使用控件确实需要一些研究,但是一旦您了解如何使用此控件,结果就会保持一致,您就可以加快表单设计的速度。