在高 DPI 显示器上正确显示

尽管 Direct2D 可解决许多高 DPI 问题,但应采取两个步骤来确保应用程序在高 DPI 显示器上正常工作。

步骤 1:创建窗口后使用窗口自己的 DPI

GetDpiForWindow 函数检索指定窗口的每英寸点数 (dpi) 值。 若要使用该值设置窗口的宽度,请使用以下公式:

<Dpi> * <宽度,以像素> 为单位/ <默认 DPI>

...其中 DPI 是由 GetDpiForWindow 重新尝试的值,默认 DPI 为 96 (在标头) 中winuser.h定义为 USER_DEFAULT_SCREEN_DPI 。 垂直轴的公式类似:

<Dpi> * <高度,以像素> 为单位/ <默认垂直 DPI>

创建简单 Direct2D 应用程序的步骤 2.3 中的代码示例检索窗口的 DPI,然后将其大小设置为 640 × 480,缩放为 DPI。

注意

对于通用 Windows 平台 (UWP) 应用,可以使用 DisplayInformation::LogicalDpi 属性。

步骤 2:声明应用程序可感知 DPI

当应用程序声明自己为 DPI 感知时,它是一个语句,指定应用程序在高达 200 DPI 的 DPI 设置下表现良好。 在 Windows Vista 和 Windows 7 中,当启用 DPI 虚拟化时,将缩放非 DPI 感知的应用程序,并且应用程序从系统 API(如 GetSystemMetric 函数)接收虚拟化数据。 若要声明应用程序可感知 DPI,请完成以下步骤。

  1. 创建名为 DeclareDPIAware.manifest 的文件。

  2. 将以下 xml 复制到文件中并保存:

    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
      <asmv3:application>
        <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
          <dpiAware>true</dpiAware>
        </asmv3:windowsSettings>
      </asmv3:application>
    </assembly>
    
  3. 在项目的 .vcproj 文件中,在 VisualStudioProject/Configurations 下的每个 Configuration 元素中添加以下条目:

    <Tool
        Name="VCManifestTool"
        AdditionalManifestFiles="DeclareDPIAware.manifest"
    />