可以在每个会话的基础上以编程方式启用/禁用 DPI 缩放吗?

问题描述:

我的应用程序恰好是使用 pygame 用 Python 编写的,它包装了 SDL,但我想这可能是一个与 Windows API 相关的更普遍的问题.

My application happens to be written in Python using pygame, which wraps SDL, but I'm imagining that this is probably a more-general question to do with the Windows API.

在我的一些 Python 应用程序中,即使在高分辨率下,我也希望在 Windows 10 下进行逐像素控制.例如,我希望能够确保,如果我的 Surface Pro 3 的原始分辨率为 2160x1440,那么我可以进入具有这些尺寸的全屏模式,并显示完全符合这些尺寸的全屏图像.

In some of my Python applications, I want pixel-for-pixel control under Windows 10 even at high resolutions. I want to be able to ensure, for example, that if my Surface Pro 3 has a native resolution of 2160x1440, then I can enter full-screen mode with those dimensions and present a full-screen image of exactly those dimensions.

对此的障碍是DPI 缩放".默认情况下,在 Windows 的设置"->显示"下,更改文本、应用程序和其他项目的大小"的值为150%(推荐)",结果我只能看到图像的 2/3.我发现了如何解决这种行为...

The barrier to this is "DPI scaling". By default, under Windows' Settings -> Display, the value of "Change the size of text, apps, and other items" is "150% (Recommended)" and the result is that I only see 2/3 of my image. I have discovered how to fix this behaviour...

  1. 在系统范围内,通过将该滑块向下移动到 100%(但对于大多数其他应用程序来说这是不受欢迎)
  2. 仅适用于 python.exepythonw.exe,通过转到这些可执行文件的属性"对话框、兼容性"选项卡,然后单击在高 DPI 上禁用显示缩放"设置".我可以单独为我或所有用户执行此操作.我还可以通过以编程方式在注册表中设置适当的键来自动执行此过程.或者通过 .exe.manifest 文件(这似乎也需要更改全局设置,以首选外部清单,可能会对其他应用程序产生副作用).
  1. systemwide, by moving that slider down to 100% (but that's undesirable for most other applications)
  2. just for python.exe and pythonw.exe, by going to those executables' "Properties" dialogs, Compatibility tab, and clicking "Disable display scaling on high DPI settings". I can do this for me alone, or for all users. I can also automate this process by setting the appropriate keys in the registry programmatically. Or via .exe.manifest files (which also seems to require a global setting change, to prefer external manifests, with possible side-effects on other applications).

我的问题是:在打开图形窗口之前,我可以在每次启动时从内部我的程序执行此操作吗?我或使用我的软件的任何人都不一定希望为所有 Python 应用程序启用此设置——我们可能只在运行特定 Python 程序时才需要它.我在想象可能有一个 winapi 调用(或者在 SDL 中失败,由 pygame 包装)可以实现这一点,但到目前为止,我的研究是一片空白.

My question is: can I do this from inside my program on a per-launch basis, before I open my graphics window? I, or anyone using my software, won't necessarily want this setting enabled for all Python applications ever—we might want it just when running particular Python programs. I'm imagining there might be a winapi call (or failing that something inside SDL, wrapped by pygame) that could achieve this, but so far my research is drawing a blank.

这是我一直在寻找的答案,基于 IInspectable 和 andlabs 的评论(非常感谢):

Here's the answer I was looking for, based on comments by IInspectable and andlabs (many thanks):

  import ctypes

  # Query DPI Awareness (Windows 10 and 8)
  awareness = ctypes.c_int()
  errorCode = ctypes.windll.shcore.GetProcessDpiAwareness(0, ctypes.byref(awareness))
  print(awareness.value)

  # Set DPI Awareness  (Windows 10 and 8)
  errorCode = ctypes.windll.shcore.SetProcessDpiAwareness(2)
  # the argument is the awareness level, which can be 0, 1 or 2:
  # for 1-to-1 pixel control I seem to need it to be non-zero (I'm using level 2)

  # Set DPI Awareness  (Windows 7 and Vista)
  success = ctypes.windll.user32.SetProcessDPIAware()
  # behaviour on later OSes is undefined, although when I run it on my Windows 10 machine, it seems to work with effects identical to SetProcessDpiAwareness(1)

认知级别定义如下:

typedef enum _PROCESS_DPI_AWARENESS { 
    PROCESS_DPI_UNAWARE = 0,
    /*  DPI unaware. This app does not scale for DPI changes and is
        always assumed to have a scale factor of 100% (96 DPI). It
        will be automatically scaled by the system on any other DPI
        setting. */

    PROCESS_SYSTEM_DPI_AWARE = 1,
    /*  System DPI aware. This app does not scale for DPI changes.
        It will query for the DPI once and use that value for the
        lifetime of the app. If the DPI changes, the app will not
        adjust to the new DPI value. It will be automatically scaled
        up or down by the system when the DPI changes from the system
        value. */

    PROCESS_PER_MONITOR_DPI_AWARE = 2
    /*  Per monitor DPI aware. This app checks for the DPI when it is
        created and adjusts the scale factor whenever the DPI changes.
        These applications are not automatically scaled by the system. */
} PROCESS_DPI_AWARENESS;

Level 2 听起来最适合我的目标,尽管 1 也可以工作,前提是系统分辨率/DPI 缩放比例没有变化.

Level 2 sounds most appropriate for my goal although 1 will also work provided there's no change in system resolution / DPI scaling.

SetProcessDpiAwareness 将失败,errorCode = -2147024891 = 0x80070005 = E_ACCESSDENIED 如果之前已为当前进程调用过它(包括在由于注册表项或 .manifest 文件而启动进程)

SetProcessDpiAwareness will fail with errorCode = -2147024891 = 0x80070005 = E_ACCESSDENIED if it has previously been called for the current process (and that includes being called by the system when the process is launched, due to a registry key or .manifest file)