Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can DPI scaling be enabled/disabled programmatically on a per-session basis?

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.

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.

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. 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).

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.

like image 872
jez Avatar asked Jun 06 '17 19:06

jez


People also ask

How do I fix Disable display scaling on high DPI settings?

Change application properties In Explorer or on the Start menu, right-click the application name, select Properties, select the Compatibility tab, and then select the Disable display scaling on high DPI settings check box.

What is Override high DPI scaling behavior?

High DPI scaling override Application: The application will be unaware of high DPI and will not be scaled; System: The application will again be unaware of high DPI and the system will scale it; System (Enhanced): This is only available in Windows 10 (1703)+.

How do you do DPI scaling?

DPI setting controls the size of the text, apps and icons. A lower DPI setting will make them appear smaller and a higher setting will make them appear bigger. By default Windows has setting of 96 DPI. Open Registry Editorby pressing Windows + R key combination, type in regedit and press Enter.

What does enable high DPI Scaling do?

High-DPI scaling improvements on Windows 10 The new option is called "System (Enhanced)," and when enabled the text and interface will look crispier and elements will be sized correctly. Though, some parts of the app may continue to look a little blurry, but it's still a significant improvement.


1 Answers

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)

The 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 appropriate for my goal although 1 will also work provided there's no change in system resolution / DPI scaling.

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)

like image 190
jez Avatar answered Oct 12 '22 11:10

jez