Asad Ullah
Asad Ullah

Reputation: 2337

Windows Form size issue on different resolutions

I am newbie in window forms development, after developing some forms i have noticed that forms are not displayed correctly in different resolutions, form go beyond the screen in some resolutions

I was wondering if there is any setting which can auto adjust the form according to resolution, or is there any hack or some technique which i can use to design the form.

Please elaborate your answer a bit as i am quite fresh with windows form development.

Thanks

Upvotes: 2

Views: 13621

Answers (2)

Mohamed Ali
Mohamed Ali

Reputation: 3995

You can loop on every control on the form within the load event, and re-scale them and the form itself, the scale is the ratio between the dimensions of the screen you have designed the form for to the dimensions of the screen that the app is working on.

    //this is a utility static class
    public static class Utility{


    public static void fitFormToScreen(Form form, int h, int w)
    {

        //scale the form to the current screen resolution
        form.Height = (int)((float)form.Height * ((float)Screen.PrimaryScreen.Bounds.Size.Height / (float)h));
        form.Width = (int)((float)form.Width * ((float)Screen.PrimaryScreen.Bounds.Size.Width / (float)w));

        //here font is scaled like width
            form.Font = new Font(form.Font.FontFamily, form.Font.Size * ((float)Screen.PrimaryScreen.Bounds.Size.Width / (float)w));

        foreach (Control item in form.Controls)
        {
            fitControlsToScreen(item, h, w);
        }

    }

    static void fitControlsToScreen(Control cntrl, int h, int w)
    {
        if (Screen.PrimaryScreen.Bounds.Size.Height != h)
        {

            cntrl.Height = (int)((float)cntrl.Height * ((float)Screen.PrimaryScreen.Bounds.Size.Height / (float)h));
            cntrl.Top = (int)((float)cntrl.Top * ((float)Screen.PrimaryScreen.Bounds.Size.Height / (float)h));

        }
        if (Screen.PrimaryScreen.Bounds.Size.Width != w)
        {

            cntrl.Width = (int)((float)cntrl.Width * ((float)Screen.PrimaryScreen.Bounds.Size.Width / (float)w));
            cntrl.Left = (int)((float)cntrl.Left * ((float)Screen.PrimaryScreen.Bounds.Size.Width / (float)w));

                cntrl.Font = new Font(cntrl.Font.FontFamily, cntrl.Font.Size * ((float)Screen.PrimaryScreen.Bounds.Size.Width / (float)w));

        }

        foreach (Control item in cntrl.Controls)
        {
            fitControlsToScreen(item, h, w);
        }
    }
    }


        //inside form load event
        //send the width and height of the screen you designed the form for
        Utility.fitFormToScreen(this, 788, 1280);
        this.CenterToScreen();

Upvotes: 5

cdkMoose
cdkMoose

Reputation: 1636

The issue here is more likely that it is not working the way you expect. In WinForms development, when you design a form you are actually setting its size. This follows a function of default font size on the machine the form is being displayed on and does not directly correlate to the resolution on the display in question. So, if you design a large form with many controls or large controls, it may display fine at a high resolution but not at a low one. For a better feel of this sizing, take a look at your Form1.Designer.cs file and you will see size values being set for the controls. These sizes do not equate to pixels, but they should give you a relative sizing. You should probably also research dialog units on MSDN or elsewhere.

You could write some code to react to that resolution in the form load event, but in the end the size will be partially constrained by the number of widgets that you need to display. If you had a multi-line edit field, grid control, tree control or some other large widget, you could automatically resize it based on the current display resolution and resize the window at the same time. But that is an application specific decision based on your needs, that is why windows does not try to automatically resize for you.

As noted above, WPF gives a more flexible form definition model and can be more reactive to realign widgets, but in the end if your form is busy enough, a WPF form have the same issues on a lower resolution.

Upvotes: 1

Related Questions