WPF和Winforms项目DPI混合意识 [英] Mixed WPF and winforms project DPI awareness

查看:113
本文介绍了WPF和Winforms项目DPI混合意识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个同时使用Winforms和WPF的C#程序,我正在努力使其在高DPI环境中工作.当前,如果我从解决方案中删除所有WPF项目并运行它,它将可以很好地进行缩放,但是,一旦我重新添加任何wpf(该功能是必需的),则缩放将失败并且UI变得不可用.

这似乎与先前的SO问题完全相同:无法使用DPI正确缩放WinForms的问题

我遵循了这些问题的建议,并尝试添加清单文件和提供的dpi感知代码.

我还将项目升级到.net Framework 4.6.1(从4.0开始),并包含了app.config设置:< add key ="EnableWindowsFormsHighDpiAutoResizing" value ="true"/>

这确实影响了程序的主外壳(因此,不是在压缩所有控件的情况下将其加载到一个小窗口中,而是将主程序窗口最大化地打开并显示为正常),但是,当我从菜单中输入任何winforms子窗体或插件时主窗口,缩放失败.

当我输入任何WPF子窗体或插件,或返回主屏幕时,这些将正确呈现.仅winforms功能无法正确扩展.

是否有人对如何获得混合Winforms/WPF项目以在高DPI下正确缩放有任何想法?

预先感谢

解决方案

将WPF加载到项目中后,它会将过程升级为系统DPI感知".为了解决这个问题,首先:1)在输入程序集的名称空间声明上方设置[assembly:DisableDpiAwareness]属性2)您可以在项目中添加一个app.manifest文件,并添加以下内容:

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

这应该在正确的目标DPI处同时呈现WinForms和WPF内容.(但是,如果您具有使用不同DPI的多监视器设置,则将无法缩放).

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

解决方案

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

这篇关于WPF和Winforms项目DPI混合意识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆