Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i Make my WinForms Application DPI-Aware

Tags:

c#

winforms

dpi

I'm working in a company that has a Winforms application as our flagship product. we recently reworked the branding and UI. We have noticed that the Forms now do not display the text properly and some controls are out of alignment or have disappeared off the edge.

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

like image 842
Sam Avatar asked Dec 28 '22 23:12

Sam


1 Answers

It's essential that you don't specify pixel coordinates and sizes. This rules out using Control.Top and Control.Left, which is what the designer does, when you "just place" the controls on a form.

To get an UI that works with different DPI settings, pretty much everything has to be dynamically sized. The controls should have Control.AutoSize enabled. But just enabling AutoSize would totally screw up your layout, since the control position would still be static.

To get dynamically position the controls, you can use container controls, like the FlowLayoutPanel and the TableLayoutPanel (with sizes set to AutoSize). The normal controls inside of those would then just move arround the form, according to automatically determined sizes.

As you can see, this isn't simple, requires a bit of experience to get it right and needs a huge amount of testing (virtual machines with different DPI settings work great). But I think it should definitely be done, since I'm always annoyed if something looks stupid and buggy on my laptop.

like image 196
xod Avatar answered Dec 30 '22 12:12

xod