Windows Forms and High DPI Screens

末鹿安然 提交于 2020-02-04 04:49:23

问题


I have an Outlook Add In called TMO (http://gotmo.co.uk) which has a Windows Form that is opened on the click of the Help Button in the Outlook toolbar. The problem is that when I use TMO on High DPI Screens such as Surface Pro the form renders very convoluted (see screenshot). However on other laptops the form renders correctly.

In fact if I run a test app on Surface Pro from where I load the same form it renders correctly. See the attached screenshots.

From my reading so far Windows Forms need special handling to render properly on high DPI screens. But I am unable to figure out what code needs to be written to do this. My two specific questions are

  1. WHy does the same form render differenlty on High DPI screens when launched from within Outlook vs STand Alone.
  2. What code needs to be written (generically) to handle Windows FOrms rendering on High DPI screens.


回答1:


Enable per monitor DPI awareness in the app.config

<System.Windows.Forms.ApplicationConfigurationSection>
   <add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection> 

Reference - https://msdn.microsoft.com/en-us/library/mt799789(v=vs.110).aspx




回答2:


I eventually solved it by re-designing the forms slightly so that they render correctly on all screens. In addition following helped

  1. Group individual controls into containers such as groupboxes and panels.
  2. Within each parent container use the Anchor property of the controls properly so that they scale within their parent containers.
  3. Set the AutoScaleMode of the parent form to Dpi, set AutoSizeMode to GrowAndShrink

I was able to follow the above 3 steps repeatedly across forms and change them reliably to work on both High DPI and regular screens




回答3:


Application UI can behave differently based off of the DPI awareness mode that the thread or process that the UI is in running under. Outlook probably runs with "System" DPI awareness mode, while your app might be running in a different mode when running outside of the Outlook process. I wasn't clear of whether your code is running within the Outlook process or is just launched by the Outlook process, but I suspect that you're dealing with an unexpected DPI-awareness-mode change.

I'd use sys-internals to determine the mode that your UI is running in - in the two different cases.

You might have success if you validate your UI code running with "System" DPI awareness mode, that way the code won't be surprised/messed up when it runs in a different context... if that is what's going on.

It looks to me like the WinForms code is assuming that it should be running at System DPI, or a different DPI in some way, than what Outlooks is using.



来源:https://stackoverflow.com/questions/44243080/windows-forms-and-high-dpi-screens

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