How can i Make my WinForms Application DPI-Aware

久未见 提交于 2019-11-28 05:02:32

问题


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.


回答1:


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.




回答2:


The quickest 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



来源:https://stackoverflow.com/questions/5296810/how-can-i-make-my-winforms-application-dpi-aware

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!