How can I make a WinForms DPI-Aware application?

I work for a company that has a Winforms application as our flagship product. We recently redesigned branding and user interface. We noticed that the forms no longer display the text correctly, and some controls are out of alignment or have disappeared from the edge.

Is it possible for me to make forms in this DPI-Aware application with the least amount of re-factoring since we don’t have time.

+4
source share
2 answers

It is important that you do not specify the coordinates and pixel sizes. This eliminates the use of Control.Top and Control.Left , which the designer does when you simply place controls on the form.

To get a user interface that works with various DPI settings, almost everything needs to be dynamic. Controls must have Control.AutoSize . But just turning on AutoSize completely ruin your layout, since the control position would still be fixed.

To dynamically position controls, you can use container controls such as FlowLayoutPanel and TableLayoutPanel (with dimensions set to AutoSize ). The usual controls inside them will then simply move around in shape in accordance with automatically determined sizes.

As you can see, this is not easy, it takes a little experience to get it right, and it requires a lot of testing (virtual machines with different DPI settings work fine). But I think that this must be done, as it always annoys me if something looks stupid and buggies on my laptop.

+13
source

The fastest way is to override OnPaint and use the Graphics.DpiX and DpiY properties. Refer to http://msdn.microsoft.com/en-us/library/ms701681(VS.85).aspx only recommendation is to use a manifest file instead of SetDpiAware

-3
source

Source: https://habr.com/ru/post/1343609/


All Articles