Can DPI scaling be enabled / disabled programmatically based on session?

My application appears to be written in Python using pygame which wraps the SDL, but I'm guessing this is probably a more general Windows API related question.

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

The obstacle to this is "DPI scaling". By default, under Settings -> Display, the value for "Resize text, apps and other items" is "150% (recommended)" and as a result I only see 2/3 of my image. I discovered how to fix this behavior ...

  • across the country by moving this slider to 100% (but undesirable for most other apps)
  • for python.exe

    and only pythonw.exe

    , by going to the Properties dialogs of the executables, the Compatibility tab, and clicking Disable High DPI Display Scaling. 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 requires a global configuration change to prefer external manifests, with possible side effects for other applications).

My question is, can I do this from within my launcher-based program, before opening the graphics window? I, or anyone using my software, would not necessarily want this setting enabled for all Python applications, we might only want it when running certain Python programs. I'm guessing there might be a call winapi

(or glitch in something inside the SDL wrapped by pygame) that could achieve this, but so far my research is drawing a gap.

+4


source to share


1 answer


Here is the answer I was looking for based on the comments of IInspectable and andlabs (thanks a lot):

  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)

      

Awareness levels are defined as follows:



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 sounds most suitable for my purpose, although 1 will also work as long as there is no change in system resolution / DPI scaling.

SetProcessDpiAwareness

will exit with errorCode = -2147024891 = 0x80070005 = E_ACCESSDENIED

if it was previously called for the current process (and includes a call by the system when starting a process due to a registry key or file .manifest

)

+5


source







All Articles