DPI Scaling in .Net 3.5 in Mixed WinForms and WPF Application

别说谁变了你拦得住时间么 提交于 2019-12-18 07:45:38

问题


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

回答1:


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"


来源:https://stackoverflow.com/questions/19552299/dpi-scaling-in-net-3-5-in-mixed-winforms-and-wpf-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!