简体   繁体   中英

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)

screenshot1

I am new to c# windows forms. As shown in screenshot1, I have Form1 with some controls and each group of controls were put in a panel. I designed the application in PC1 as shown in Screenshot1 which is fit the screen resolution and worked well.

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.

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 . I do not know if the problem is related to user control or related to all controls in Form1 (they should resize themselves)

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.

In WinForms you are setting the Size of every element so they will not re size according to the app size. 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.

By default in WinForms, all controls that you place on the form while designing it have a fixed size . 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. 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. 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. The only time this kind of design is acceptable is when you're designing for a touch screen UI, like a restaurant 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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