根据屏幕大小自动调整 winform 和控件 [英] Automatically adjusting winform and controls to screen size

查看:34
本文介绍了根据屏幕大小自动调整 winform 和控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

我使用的是 VS 2012.

I am using VS 2012.

推荐答案

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

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.

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

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

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

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.

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

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 specifications bookmarked for reference. This is another good reference.

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

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 container controls and child controls. The form itself is a container control, and you can set its child controls 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.

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

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

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

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.

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

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.

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

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.

这篇关于根据屏幕大小自动调整 winform 和控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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