Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a C# program that uses both winforms and WPF and I'm struggling to get it to work in high DPI environments. Currently, if I remove all WPF projects from the solution and run it, it will scale fine, however as soon as I add any wpf back in (necessary for the functionality) the scaling fails and the UI becomes unusable.

This seems like the exact same issue as previous SO questions: DPI Scaling in .Net 3.5 in Mixed WinForms and WPF Application and Problems getting WinForms to scale correctly with DPI

I followed the advice on these questions and tried adding a manifest file and the dpi aware code provided.

I also upgraded the project to .net framework 4.6.1 (from 4.0) and included the app.config setting: <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />

This did affect the main shell of the program (so instead of loading up in a small window with all the controls squished, the main program window opens maximised and appears normal) However, when I enter any winforms subform or plugin from the main window, the scaling fails.

When I enter any WPF subform or plugin, or return to the main screen, these render correctly. It is only the winforms features that are failing to scale properly.

Has anyone got any ideas on how to get mixed winforms/WPF projects to scale correctly in high DPI?

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
490 views
Welcome To Ask or Share your Answers For Others

1 Answer

When WPF is loaded into a project, it promotes the process to be System DPI Aware. In order to get around this, first : 1) set [assembly: DisableDpiAwareness] attribute above the namespace declaration in your entry assembly 2) you can add an app.manifest file in your project and add the following :

 <asmv3:windowsSettings
 xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
       <dpiAware>true/PM</dpiAware> 
 </asmv3:windowsSettings>

This should render your WinForms and WPF content both at the right target DPI. (This will however fail to scale if you have a multi monitor setup with different DPIs).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...