简体   繁体   English

如何将Windows窗体适合/重新调整为任何屏幕分辨率?

[英]How do I fit/re-size Windows Form to any screen resolution?

I know this is duplicated question but I checked all other related question and their answers did not help ( the result still the same as shown in screenshot 2) 我知道这是重复的问题,但我检查了所有其他相关的问题,他们的答案没有帮助(结果仍然与屏幕截图2中显示的相同)

screenshot1

I am new to c# windows forms. 我是c#windows表单的新手。 As shown in screenshot1, I have Form1 with some controls and each group of controls were put in a panel. 如屏幕截图1所示,我有Form1 ,其中包含一些控件,每组控件都放在一个面板中。 I designed the application in PC1 as shown in Screenshot1 which is fit the screen resolution and worked well. 我在PC1中设计了应用程序,如屏幕截图1所示,它符合屏幕分辨率并且运行良好。

Screenshot2

My application was developed in 1366 x 768 screen resolution (as shown in Screenshot1) but when I run this application in different computer (PC2) with different monitor size and different screen resolution the form appeared too big and part of the application is missing or is out of the screen. 我的应用程序是在1366 x 768屏幕分辨率下开发的(如屏幕截图1所示)但是当我在具有不同显示器尺寸和不同屏幕分辨率的不同计算机(PC2)中运行此应用程序时,表单显得太大而且部分应用程序丢失或者是离开屏幕。

However I solved this issue using Anchors but another issues came up which is: the user control does not re-size itself ( as shown in screenshot2) and part of it is cut or went under panel1 . 然而,我使用Anchors解决了这个问题,但是出现了另一个问题:用户控件没有重新调整大小(如屏幕截图2所示),部分内容被剪切或移至panel1下。 I do not know if the problem is related to user control or related to all controls in Form1 (they should resize themselves) 我不知道问题是与用户控制有关还是与Form1中的所有控件有关(他们应该自己调整大小)

I even tried the following code but the result still the same: 我甚至尝试了以下代码,但结果仍然相同:

this.WindowState = FormWindowState.Maximized;
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
Screen.PrimaryScreen.WorkingArea

I have been searching to solve this issue the whole day yesterday but I failed, Please help me with any idea/suggestion it might work. 昨天我一直在寻找解决这个问题,但我失败了,请帮助我任何想法/建议它可能会起作用。 Thank you 谢谢

If you are working with Windows Forms and you cannot switch to WPF then you will prefer to do all the design in the lowest resolution at which you must run. 如果您正在使用Windows窗体而无法切换到WPF,那么您更愿意以必须运行的最低分辨率进行所有设计。

In WinForms you are setting the Size of every element so they will not re size according to the app size. 在WinForms中,您要设置每个元素的大小,以便它们不会根据应用程序大小重新调整大小。 What they will do is to be distributed along the empty space (if you program them to do so) increasing the free space between them, that's all. 他们将要做的是沿着空白区域分发(如果你为它们编程),增加它们之间的自由空间,就是这样。

Another option are LayoutPanels as Sinatr said as they try to offer the WPF panel functionality. 另一个选项是LayoutPanels,正如Sinatr所说,他们试图提供WPF面板功能。

By default in WinForms, all controls that you place on the form while designing it have a fixed size . 默认情况下,在WinForms中,您在设计表单时放置的所有控件都具有固定大小 If you don't do anything special, whatever size the controls are when you place them are the size that they are always going to have, no matter what machine you're running on. 如果你没有做任何特别的事情,无论你放置什么样的机器,当你放置它们时控件的大小都是它们总是具有的大小。

As you've noticed, that isn't always going to give good results. 正如你所注意到的那样,并不总能带来好结果。 The way you work around it is to use a fluid layout , with liberal use of the TableLayoutPanel and/or FlowLayoutPanel container controls, as well as the Anchor and Dock properties for individual child controls. 解决它的方法是使用流畅的布局 ,自由使用TableLayoutPanel和/或FlowLayoutPanel容器控件,以及各个子控件的AnchorDock属性。 If you take special care to lay out the controls on your form correctly, they can be dynamically resized and rearranged to fit the available screen space. 如果您特别注意在表单上正确布置控件,可以动态调整其大小并重新排列以适应可用的屏幕空间。

This code 这段代码

this.WindowState = FormWindowState.Maximized;
this.Location = new Point(0, 0);
this.Size = Screen.PrimaryScreen.WorkingArea.Size;
Screen.PrimaryScreen.WorkingArea

doesn't do anything. 什么都不做 The only thing you need there is the very first line. 你唯一需要的是第一行。 Once you make the form maximized, it will fill the entire screen. 一旦您使表单最大化,它将填满整个屏幕。 You don't need to force it by setting its Size or Location properties. 您无需通过设置其“ Size或“ Location属性来强制它。 In fact, those have no effect on a maximized form. 事实上,那些对最大化形式没有影响。

The problem is presumably that the controls placed on your form do not resize themselves automatically (as discussed above). 问题可能是表单上的控件不会自动调整大小(如上所述)。 If you had a big enough screen, you'd see empty space where the form was filling the screen but had no controls on it. 如果你有一个足够大的屏幕,你会看到表格填满屏幕但没有控件的空白区域。 You have the opposite problem: on a smaller screen, controls don't fit and therefore overlap each other. 你有相反的问题:在较小的屏幕上,控件不适合,因此彼此重叠。

It isn't a perfect situation. 这不是一个完美的情况。 Even if you code up a perfect dynamic layout, if you try to run the application on a system with a screen that is significantly smaller than your design intended, you're going to end up with buttons that are too small to poke. 即使您编写了一个完美的动态布局,如果您尝试在屏幕明显小于您的设计的系统上运行该应用程序,您最终会得到太小而无法戳的按钮。 That's why applications are not, in general, designed this way. 这就是为什么应用程序通常不是这样设计的。 A screenful of buttons is terrible UI. 屏幕上的按钮是糟糕的 UI。 The only time this kind of design is acceptable is when you're designing for a touch screen UI, like a restaurant POS. 这种设计唯一可以接受的是当你设计一个触摸屏UI,比如餐厅POS。 And in that case, you already have a pretty good idea what size screens your clients are going to be using, since it's all specialty hardware. 在这种情况下,您已经非常清楚客户将要使用的屏幕大小,因为它是所有专业硬件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM