54

I have noticed that some applications change their controls' positions to fit themselves as much as possible in the current resolution. For example, if the window is maximized, the controls are set in such a way that the overall GUI looks balanced.

Is it possible to make or implement this functionality in Visual studio 2010 using C#?

jordanz
  • 367
  • 4
  • 12
Afnan Bashir
  • 7,319
  • 20
  • 76
  • 138

10 Answers10

72

Use Dock and Anchor properties. Here is a good article. Note that these will handle changes when maximizing/minimizing. That is a little different that if the screen resolution changes, but it will be along the same idea.

Phil Ross
  • 25,590
  • 9
  • 67
  • 77
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • I must mention the AutoScaleMode which made me waist **lots** of time of games with Anchor&Dock (which weren't actually the problem...) – ephraim May 07 '18 at 11:28
27

Use combinations of these to get the desired result:

  1. Set Anchor property to None, the controls will not be resized, they only shift their position.

  2. Set Anchor property to Top+Bottom+Left+Right, the controls will be resized but they don't change their position.

  3. Set the Minimum Size of the form to a proper value.

  4. Set Dock property.

  5. Use Form Resize event to change whatever you want

I don't know how font size (label, textbox, combobox, etc.) will be affected in (1) - (4), but it can be controlled in (5).

Bhaskar
  • 1,028
  • 12
  • 16
12
float widthRatio = Screen.PrimaryScreen.Bounds.Width / 1280;
float heightRatio = Screen.PrimaryScreen.Bounds.Height / 800f;
SizeF scale = new SizeF(widthRatio, heightRatio);
this.Scale(scale);
foreach (Control control in this.Controls)
{
control.Font = new Font("Verdana", control.Font.SizeInPoints * heightRatio * widthRatio);
}
  • @AfnanBashir perhaps, but this one saves you tons of time, if you already have a Windows form with tons of controls. +1 –  Oct 12 '15 at 19:39
  • That's an interesting approach (even if the ratio calculation could be improved). Ostensibly it relies on `AutoScaleMode` of the form being `Font`. – Zeus Jul 08 '21 at 02:31
2

..and to detect a change in resolution to handle it (once you're using Docking and Anchoring like SwDevMan81 suggested) use the SystemEvents.DisplaySettingsChanged event in Microsoft.Win32.

1

sorry I saw the question late, Here is an easy programmatically solution that works well on me,

Create those global variables:

 float firstWidth;
 float firstHeight;

after on load, fill those variables;

 firstWidth = this.Size.Width;
 firstHeight = this.Size.Height;

then select your form and put these code to your form's SizeChange event;

 private void AnaMenu_SizeChanged(object sender, EventArgs e)
    {
        

        float size1 = this.Size.Width /  firstWidth;
        float size2 = this.Size.Height / firstHeight;

            SizeF scale = new SizeF(size1, size2);
        firstWidth = this.Size.Width;
        firstHeight = this.Size.Height;

        foreach (Control control in this.Controls)
        {
                
            control.Font = new Font(control.Font.FontFamily, control.Font.Size* ((size1+ size2)/2));
            
            control.Scale(scale);
                

        }


    }

I hope this helps, it works perfect on my projects.

  • I'm getting the error `Value of '∞' is not valid for 'emSize'. 'emSize' should be greater than 0 and less than or equal to System.Single.MaxValue. Parameter name: emSize` in the line `control.Font = new Font(control.Font.FontFamily, control.Font.Size* ((size1+ size2)/2));` after using your code.. – Bumba Nov 29 '21 at 13:29
0

Here I like to use https://www.netresize.net/index.php?c=3a&id=11#buyopt. But it is paid version.

You also can get their source codes if you buy 1 Site License (Unlimited Developers).

How ever I am finding the nuget package solution.

Zin Min
  • 3,898
  • 1
  • 20
  • 24
-1

add this code at page load do for all control or add all control in containers

int x;
Point pt = new Point();
x = Screen.PrimaryScreen.WorkingArea.Width - 1024;
x = x / 2;
pt.Y = groupBox1.Location.Y + 50;
pt.X = groupBox1.Location.X + x;
groupBox1.Location = pt;
sschrass
  • 7,014
  • 6
  • 43
  • 62
chirag
  • 39
  • 4
-2

in the form load event add this line

this.WindowState = FormWindowState.Maximized;
Bohemian
  • 412,405
  • 93
  • 575
  • 722
soldiershin
  • 1,612
  • 1
  • 18
  • 29
-3
private void MainForm_Load( object sender, EventArgs e ) 
     { 
        this.Size = Screen.PrimaryScreen.WorkingArea.Size 
     }
Sherif Hamdy
  • 587
  • 6
  • 10
-6
this.WindowState = FormWindowState.Maximized;
ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
  • 4
    Welcome to Stack Overflow! While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – ryanyuyu Sep 29 '16 at 13:15
  • 3
    This code will only make your windows maximized, but the size of all controls inside the windows won't dynamically changed. – Vincent Elbert Budiman May 04 '18 at 05:37