0

This is a follow-up question from this, but I will include all the necessary information here. I was wondering whether there was a way to automatically scale apps for higher DPI settings, and it turns out there is. I currently implement this by adding app.manifest and activating the commented out part concerning DPI awareness, but I have also done this via SetProcessDPIAware() in the program's main method, which yielded the same result.

When I build a form using Visual Studio's Form Designer, when I run the program with a scaling setting of 125% the entire form is scaled up. However, when I build a form using a combination of lines like Label xinputtext = new Label { Location = new Point(15, 20)} in the form's class and Controls.Add(xinputtext) in the form's constructor method, only the text is scaled up, none of the controls are.

How can I design a form that is affected by automatic DPI scaling without using the Designer?

By request, here is a demonstration:

namespace Just_testing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
           Controls.Add(button1);
           Controls.Add(button2);
        }

        Button button1 = new Button { Location = new Point(13, 13), Text = "button1" };
        Button button2 = new Button { Location = new Point(13, 35), Text = "button2" };
    }
}

With Windows' display scale set to 100%, the top left corner of the screen displays as follows:
100% scale

With Windows' display scale set to 150%, it displays as follows:
150% scale actual

When instead of code, I use the Designer to place these controls, with Windows' display scale set to 150%, it displays as follows:
150% scale desired
And that is as I would want it. Note that the issue is not that the text in the second image is too large: the text scales correctly, but I want the buttons and everything else (including locations, of course) to scale with it. And this does happen if you build the controls using the Designer.

607
  • 35
  • 7
  • It's not quite clear what the problem you are trying to solve. Please add some steps and a very minimal code to reproduce the problem, show the screenshots, and tell the expectation. – Reza Aghaei Jan 01 '20 at 18:17
  • @RezaAghaei Sure; added! – 607 Jan 01 '20 at 19:31
  • 1
    Designer doesn't do a magic. There are a few differences when you use designer or you don't use designer in your example. For `Form` is sets `AutoScaleDimensions` and `AutoScaleMode`. For `Button` is sets `Size`. – Reza Aghaei Jan 01 '20 at 20:17
  • It doesn't have to do with this.SuspendLayout(); and this.ResumeLayout(false);, does it? – 607 Jan 02 '20 at 10:00
  • It seems as if nobody knows. I was not able to figure it out either, and will give up now, sorry. I was hoping to get it figured out so I could spare others effort, but in the time it cost researching this and experimenting with this I could have written the desired behaviour myself several times over. – 607 Jan 03 '20 at 16:54

0 Answers0