Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixed WPF and winforms project DPI awareness

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

like image 616
Anya Hope Avatar asked Jan 20 '16 13:01

Anya Hope


1 Answers

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).

like image 106
rohit21agrawal Avatar answered Sep 21 '22 05:09

rohit21agrawal