解决 VS 2013 开发的应用程序不具备 DPI 感知能力导致的 windows 10 运行模式下字体模糊(设计模式下清晰):
(1)在Module.vb中,在顶部添加如下代码:
Imports System.Runtime.InteropServices
Module Module1
Public Declare Function SetProcessDpiAwarenessContext Lib "user32.dll" (ByVal dpiFlag As Integer) As Boolean
Public Declare Function SetProcessDpiAwareness Lib "SHCore.dll" (ByVal awareness As PROCESS_DPI_AWARENESS) As Boolean
Public Declare Function SetProcessDPIAware Lib "user32.dll" () As Boolean
Friend Enum PROCESS_DPI_AWARENESS
Process_DPI_Unaware = 0
Process_System_DPI_Aware = 1
Process_Per_Monitor_DPI_Aware = 2
End Enum
Friend Enum DPI_AWARENESS_CONTEXT
DPI_AWARENESS_CONTEXT_UNAWARE = 16
DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = 17
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = 18
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = 34
End Enum
Public Sub Main()
Application.EnableVisualStyles()
Dim s() As String = System.Runtime.InteropServices.RuntimeInformation.OSDescription.Split(" ")
Dim OSver As String = s(2)
Dim curVer As New Version()
If Version.TryParse(OSver, curVer) Then
'Note: the three of the following methods work with Windows 10, however SetProcessDpiAwarenessContext is the best
If curVer >= New Version(6, 3, 0) Then ' win 8.1 added support for per monitor dpi
If curVer >= New Version(10, 0, 15063) Then ' win 10 creators update added support for per monitor v2
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2)
Else
SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.Process_Per_Monitor_DPI_Aware)
End If
Else
SetProcessDPIAware()
End If
Debug.Print(curVer.ToString)
End If
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(Form1)
End Sub
End Module
(2) 在项目属性->应用程序中,取消选中“启用应用程序框架”
'并选择启动对象为“Sub Main”
以下解决方案适用于 VS 2017:
通过 Project Properties->Application->View Windows Settings 打开 app.mainfest
将 Windows-10 兼容性代码添加到 app.mainfest 块内
<!-- Windows 10 compatibility -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
同样在block后面添加如下代码
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
</windowsSettings>
</application>