How do I autoscaling my application in different screen resolutions?

0

I made a program with Windows forms in visual studio c # 2017, and I want to be able to add that the forms automatically adjust to the resolution of the screen, but I have not achieved it. In my search I found the following code:

void AjustarResolución(Form form)
    {
        String ancho = Screen.PrimaryScreen.Bounds.Size.Width.ToString();//Obtenemos el ancho de la pantalla
        String alto = Screen.PrimaryScreen.Bounds.Size.Height.ToString();//Obtenemos el alto de la pantalla
        String tamaño = ancho + "x" + alto;//concatenamos para hacer un switch
        switch (tamaño)
        {
            case "800x600":
                cambiarResolucion(form, 110F, 110F);//Hacemos el ajuste con esta función
                break;
            case "1024x600":
                cambiarResolucion(form, 96F, 110F);
                break;
            default:
                cambiarResolucion(form, 96F, 110F);
                break;
        }
    }

    private static void cambiarResolucion(Form formu, float ancho, float alto)
    {
        formu.AutoScaleDimensions = new System.Drawing.SizeF(ancho, alto);//Ajustamos la resolución
        formu.PerformAutoScale();//Escalo el control y los controles
    }

But I have not managed to make it work, it is necessary to clarify that the forms remove the title bar to be able to make a personalized bar. And if you could help me to put this code in a class, so that all the forms can be referenced to that class and not repeating this code with all the forms.

    
asked by Macx 03.01.2019 в 04:13
source

1 answer

2

For controls to scale when you expand or decrease the form you should base on the use of Anchor and Dock , they are the only forms supported by winform, but you should evaluate pass to WPF

Dynamically Resizing Controls On Windows Forms - Using Anchor Property

Configuring Your Form for Resizing

in these official articles about Windows Forms explains how to apply these properties

Adjust the size and Windows Forms Scale

Automatic scaling in Windows Forms

    
answered by 03.01.2019 в 04:55