关于这个问题中报告的问题:
一个应用程序,它在设计上不知道 DPI,依靠 Windows 虚拟化来扩展其 UI 内容,突然(尽管经过一些修改,导致一个小版本更新) - 显然没有明显的原因- 变为 DPI-Aware(系统感知)。
由于 DPI-Awareness 已成为 UI 呈现的一个相关方面,鉴于可用屏幕分辨率的多样性(以及相关的 DPI 缩放设置),大多数组件生产商已经适应了高 DPI,并且他们的产品是 DPI-Aware(DPI 变化时缩放被检测到)并使用 DPI-Aware 程序集(通常引用 WPF 程序集,定义为 DPI-Aware)。
当项目中(直接或间接)引用这些 DPI-Aware 组件之一时,如果 DPI-Awareness 尚未明确禁用,则 DPI-Unaware 应用程序将变为 DPI-Aware。
声明程序集 DPI-Awareness 的更直接(和推荐)的方法是在应用程序清单中显式声明它。
请参阅 Hans Passant 对 Visual Studio 2017 之前的应用程序清单设置的回答:
如何配置应用程序以在具有高 DPI 设置的机器上运行
自 Visual Studio 2015-Upd.1 起,此设置已存在于 中app.manifest
,只需取消注释即可。设置部分:<dpiAware>false</dpiAware>
。
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
//(...)
<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
DPI. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. -->
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">false</dpiAware>
</windowsSettings>
</application>
//(...)
</assembly>
有关详细信息,请参阅这些 MSDN 文章:
Windows 上的高 DPI 桌面应用程序开发
设置进程的默认 DPI 感知
另一种方法是使用这些 Windows API 函数设置进程上下文 DPI-Awareness:
Windows 7
SetProcessDPIAware
[DllImport("user32.dll", SetLastError=true)]
static extern bool SetProcessDPIAware();
Windows 8.1
SetProcessDpiAwareness
[DllImport("shcore.dll")]
static extern int SetProcessDpiAwareness(ProcessDPIAwareness value);
enum ProcessDPIAwareness
{
DPI_Unaware = 0,
System_DPI_Aware = 1,
Per_Monitor_DPI_Aware = 2
}
Windows 10 版本 1703
SetProcessDpiAwarenessContext()
(选择每显示器 DPI 感知时,请使用Context_PerMonitorAwareV2
)
另请参阅:混合模式 DPI 缩放和 DPI 感知 API - MSDN
Windows 10 版本 1809(2018 年 10 月)
新增DPI_AWARENESS_CONTEXT
:DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED
DPI 不知道基于 GDI 的内容质量的提高。此模式的行为类似于 DPI_AWARENESS_CONTEXT_UNAWARE,但也使系统能够在窗口显示在高 DPI 监视器上时自动提高文本和其他基于 GDI 的图元的渲染质量。
使用该GetWindowDpiAwarenessContext()
函数检索DPI_AWARENESS_CONTEXT
WindowGetThreadDpiAwarenessContext()
的DPI_AWARENESS_CONTEXT
句柄和当前线程的句柄。然后从结构GetAwarenessFromDpiAwarenessContext()
中检索DPI_AWARENESS
值。DPI_AWARENESS_CONTEXT
[DllImport("user32.dll", SetLastError=true)]
static extern IntPtr GetWindowDpiAwarenessContext(IntPtr hWnd);
[DllImport("user32.dll", SetLastError=true)]
static extern IntPtr GetThreadDpiAwarenessContext();
[DllImport("user32.dll", SetLastError=true)]
static extern int GetAwarenessFromDpiAwarenessContext(IntPtr DPI_AWARENESS_CONTEXT);
[DllImport("user32.dll", SetLastError=true)]
static extern int SetProcessDpiAwarenessContext(DpiAwarenessContext value);
// Virtual enumeration: DPI_AWARENESS_CONTEXT is *contextual*.
// This value is returned by GetWindowDpiAwarenessContext() or GetThreadDpiAwarenessContext()
// and finalized by GetAwarenessFromDpiAwarenessContext(). See the Docs.
enum DpiAwarenessContext
{
Context_Undefined = 0,
Context_Unaware = -1,
Context_SystemAware = -2,
Context_PerMonitorAware = -3,
Context_PerMonitorAwareV2 = -4,
Context_UnawareGdiScaled = -5
}
由于 DPI-Awareness 是基于线程的,因此这些设置可以应用于特定线程。这在重新设计用户界面以实现 DPI 感知时很有用,可以让系统在关注更重要的功能的同时扩展不太重要的组件。
SetThreadDpiAwarenessContext
(参数相同SetProcessDpiAwarenessContext()
)
Assemblyinfo.cs
如果引用 WPF 程序集的第三方/外部组件重新定义了应用程序的 DPI-Awareness 状态,则可以禁用此自动行为,在项目中插入一个参数Assemblyinfo.cs
:
[assembly: System.Windows.Media.DisableDpiAwareness]