DPI缩放在.NET 3.5中混合WinForms和WPF应用程序缩放、应用程序、NET、DPI

2023-09-04 08:52:11 作者:你的怜悯我不需要

在this帖子,codeNaked讨论如何改变应用程序的TextFormattingModeProperty。这解决了完美的.NET 4但是我的生产应用程序是在.NET 3.5中不具备的特性,在我所有的问题(见下文)。我如何完成同样的事情在.NET 3.5中?

我的根本问题:

我有一个基于在.net 3.5 WinForms应用程序,它有一些WPF某些屏幕控制。当视窗的DPI设置被设置为150%(不120%),如预期发生结垢。但是,一旦创建了一个WPF控件,缩放重新设置为100%,所有窗口。我想缩放比例保持不变。

我创建了一个测试应用程序的证明。默认情况下它会打开一个WinForm,有一个按钮,将打开另一个的WinForm与WPF控件。保持不变,打开第二形式会造成结垢恢复到100%。但是,如果在F​​orm1.vb的第11行注释掉,比例将继续第二种形式打开正确的时候出现。

 昏暗NEWAPP随着新的应用程序()取消注释来解决自动缩放
 

类应用:

 进口System.Windows
进口System.Windows.Media

部分公众类应用
    继承应用

    公用Sub应用程序()
        TextOptions.TextFormattingModeProperty.OverrideMetadata(的GetType(窗口),新FrameworkPropertyMetadata(TextFormattingMode.Display,FrameworkPropertyMetadataOptions.AffectsMeasure或者FrameworkPropertyMetadataOptions.AffectsRender或者FrameworkPropertyMetadataOptions.Inherits))

    结束小组
末级
 
如何将现有的WinForms WPF项目转换为.NET Core 这里有你想要的答案

解决方案

您必须直接定义你的应用程序是DPI知道

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

我创建app.manifest文件在您的项目,并添加

 < asmv3:用途>
      < asmv3:windowsSettings的xmlns =htt​​p://schemas.microsoft.com/SMI/2005/WindowsSettings>
          < dpiAware>真< / dpiAware>
      < / asmv3:windowsSettings>
  < / asmv3:用途>
 

其中,asmv3为

 的xmlns:asmv3 =瓮:架构 - 微软COM:asm.v3
 

In this post, CodeNaked discusses how to change the TextFormattingModeProperty of the application. 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. How do I accomplish the same thing in .Net 3.5?

My root problem:

I have a winforms application based in .Net 3.5 that has some WPF controls on certain screens. When the Windows DPI setting is set to 150% (not 120%), scaling occurs as expected. However, as soon as a WPF control is created, the scaling is set back to 100% for all windows. 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. Left unchanged, opening the second form will cause scaling to revert to 100%. However, if line 11 in Form1.vb is uncommented, scaling will continue to occur correctly when the second form is opened.

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

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

I've created app.manifest file in your project and added

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

where asmv3 is

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