Mixed WPF project and winforms DPI

I have a C # program that uses both winforms and WPF, and I'm struggling to get it working in high DPI environments. Currently, if I remove all WPF projects from the solution and run it, it will scale accurately, however, as soon as I add any wpf back (necessary for functions), the scaling will fail and the user interface will become unusable.

This seems to be the same problem as previous SO questions: DPI extension in .Net 3.5 in mixed WinForms and WPF applications and Problems with WinForms properly scaled using DPI

I followed the advice on these issues and tried to add a manifest file and the provided dpi-enabled code.

I also upgraded the project to .NET Framework 4.6.1 (from 4.0) and enabled the app.config parameter: <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />

This affected the main shell of the program (therefore, instead of loading in a small window with all the control tools, the main program window opens maximized and appears normally). However, when I enter any subform or winforms plugin from the Main window, scaling is not performed.

When I enter any WPF subform or plugin or return to the main screen, they are displayed correctly. Only winforms functions cannot scale properly.

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

Thanks in advance

+4
c # winforms wpf
source share
2 answers

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

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

This should display your WinForms and WPF content as in the correct DPI target. (However, this will not scale if you have set up multiple monitors with different DPIs).

+4
source share

My colleague finally worked. Because none of the configuration options, etc. Will not work for this project, we tried to remove the winforms shell and replace it with the WPF shell. After the main shell project was rewritten in WPF, all the "plugins" appeared in the correct DPI scaling.

Not the best fix that I know is related to re-writing existing code, however this is the only thing that sorted out the problem for us.

0
source share

All Articles