简体   繁体   English

混合WinForms和WPF应用程序中.Net 3.5中的DPI缩放

[英]DPI Scaling in .Net 3.5 in Mixed WinForms and WPF Application

In this post , CodeNaked discusses how to change the TextFormattingModeProperty of the application. 本文中 ,CodeNaked讨论了如何更改应用程序的TextFormattingModeProperty。 This solves my problem (see below) perfectly in .Net 4. However my production application is in .Net 3.5 which doesn't have that property at all. 这在.Net 4中完美地解决了我的问题(请参阅下文)。但是,我的生产应用程序在.Net 3.5中完全没有该属性。 How do I accomplish the same thing in .Net 3.5? 如何在.Net 3.5中完成相同的操作?

My root problem: 我的根本问题是:

I have a winforms application based in .Net 3.5 that has some WPF controls on certain screens. 我有一个基于.Net 3.5的Winforms应用程序,该应用程序在某些屏幕上具有一些WPF控件。 When the Windows DPI setting is set to 150% (not 120%), scaling occurs as expected. Windows DPI设置设置为150%(而不是120%)时,按预期进行缩放。 However, as soon as a WPF control is created, the scaling is set back to 100% for all windows. 但是,一旦创建WPF控件,所有窗口的缩放比例都将设置回100%。 I would like the scaling to remain unchanged. 我希望缩放比例保持不变。

I created a test application to demonstrate. 我创建了一个测试应用程序进行演示。 By default it opens a winform that has a button that will open another winform with a wpf control. 默认情况下,它将打开一个带有按钮的winform,该按钮将使用wpf控件打开另一个winform。 Left unchanged, opening the second form will cause scaling to revert to 100%. 保持不变,打开第二个窗体将导致缩放比例恢复为100%。 However, if line 11 in Form1.vb is uncommented, scaling will continue to occur correctly when the second form is opened. 但是,如果不注释Form1.vb中的第11行,则在打开第二个窗体时,缩放将继续正确进行。

Dim newApp As New App() 'Uncomment to fix automatic scaling

Class App: 类应用程序:

Imports System.Windows
Imports System.Windows.Media

Partial Public Class App
    Inherits Application

    Public Sub App()
        TextOptions.TextFormattingModeProperty.OverrideMetadata(GetType(Window), New FrameworkPropertyMetadata(TextFormattingMode.Display, FrameworkPropertyMetadataOptions.AffectsMeasure Or FrameworkPropertyMetadataOptions.AffectsRender Or FrameworkPropertyMetadataOptions.Inherits))

    End Sub
End Class

You have to directly define your app is DPI aware 您必须直接定义您的应用程序是否支持DPI

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633543(v=vs.85).aspx http://msdn.microsoft.com/zh-CN/library/windows/desktop/ms633543(v=vs.85).aspx

I've created app.manifest file in your project and added 我已经在您的项目中创建了app.manifest文件并添加了

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

where asmv3 is asmv3在哪里

xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM