Afnan Bashir
Afnan Bashir

Reputation: 7419

How to auto resize and adjust Form controls with change in resolution

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#?

Upvotes: 56

Views: 253072

Answers (10)

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.

Upvotes: 1

Zin Min
Zin Min

Reputation: 4685

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.

Upvotes: 0

user6899728
user6899728

Reputation: 1

this.WindowState = FormWindowState.Maximized;

Upvotes: -7

Sherif Hamdy
Sherif Hamdy

Reputation: 593

private void MainForm_Load( object sender, EventArgs e ) 
     { 
        this.Size = Screen.PrimaryScreen.WorkingArea.Size 
     }

Upvotes: -4

Bhaskar
Bhaskar

Reputation: 1036

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).

Upvotes: 28

SwDevMan81
SwDevMan81

Reputation: 49978

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.

Upvotes: 74

soldiershin
soldiershin

Reputation: 1620

in the form load event add this line

this.WindowState = FormWindowState.Maximized;

Upvotes: -3

chirag
chirag

Reputation: 39

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;

Upvotes: -1

Mohammad Shahnawaz
Mohammad Shahnawaz

Reputation: 175

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);
}

Upvotes: 12

Thushan Fernando
Thushan Fernando

Reputation: 316

..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.

Upvotes: 2

Related Questions