WinForms does not scale sizes correctly when changing DPI (even for simple controls)

496 Views Asked by At

I cannot get WinForms to properly scale the UI when the DPI setting is changed: different controls are scaled by a different factor, messing up the UI design.

This is what I’ve tried:

  • In Windows 7 set the text size to 100% (= 96 dpi).
  • Start Visual Studio 16.9.1 and create a new WinForms app with C# and .NET 5.0
  • Add a simple text box and a button to the form
  • Compile the project
  • Run the program: both controls have a height of 23 pixels (checked visually and programmatically)
  • Set the OS text size to 150% (= 144 dpi) and log out and in again to apply the changes
  • Run the program again: now the text box has a height of 31 pixels and the button has a height of 38 pixels.

So when increasing the OS text size (= DPI) by 50%:

  • the height of the text box increases by 33%
  • the height of the button increases by 67%

Notes:

Why are these controls scaled differently? Why are they not scaled by 50% as you would expect? What can I do to have these two controls scaled by the same factor?

0

There are 0 best solutions below