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:
With Windows' display scale set to 150%, it displays as follows:
When instead of code, I use the Designer to place these controls, with Windows' display scale set to 150%, it displays as follows:
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.