cengal.user_interface.gui.nt.dpi_awareness.versions.v_0.dpi_awareness

 1#!/usr/bin/env python
 2# coding=utf-8
 3
 4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
 5# 
 6# Licensed under the Apache License, Version 2.0 (the "License");
 7# you may not use this file except in compliance with the License.
 8# You may obtain a copy of the License at
 9# 
10#     http://www.apache.org/licenses/LICENSE-2.0
11# 
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18
19__all__ = ['ProcessDpiAwareness', 'set_dpi_awareness']
20
21
22import os
23from enum import IntEnum
24
25
26"""
27Module Docstring
28Docstrings: http://www.python.org/dev/peps/pep-0257/
29"""
30
31__author__ = "ButenkoMS <gtalk@butenkoms.space>"
32__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
33__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
34__license__ = "Apache License, Version 2.0"
35__version__ = "4.4.1"
36__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
37__email__ = "gtalk@butenkoms.space"
38# __status__ = "Prototype"
39__status__ = "Development"
40# __status__ = "Production"
41
42
43"""
44https://stackoverflow.com/questions/41315873/attempting-to-resolve-blurred-tkinter-text-scaling-on-windows-10-high-dpi-disp
45https://stackoverflow.com/questions/44398075/can-dpi-scaling-be-enabled-disabled-programmatically-on-a-per-session-basis
46
47typedef enum _PROCESS_DPI_AWARENESS { 
48    PROCESS_DPI_UNAWARE = 0,
49    /*  DPI unaware. This app does not scale for DPI changes and is
50        always assumed to have a scale factor of 100% (96 DPI). It
51        will be automatically scaled by the system on any other DPI
52        setting. */
53
54    PROCESS_SYSTEM_DPI_AWARE = 1,
55    /*  System DPI aware. This app does not scale for DPI changes.
56        It will query for the DPI once and use that value for the
57        lifetime of the app. If the DPI changes, the app will not
58        adjust to the new DPI value. It will be automatically scaled
59        up or down by the system when the DPI changes from the system
60        value. */
61
62    PROCESS_PER_MONITOR_DPI_AWARE = 2
63    /*  Per monitor DPI aware. This app checks for the DPI when it is
64        created and adjusts the scale factor whenever the DPI changes.
65        These applications are not automatically scaled by the system. */
66} PROCESS_DPI_AWARENESS;
67"""
68
69
70class ProcessDpiAwareness(IntEnum):
71    process_dpi_unaware = 0
72    process_system_dpi_aware = 1
73    process_per_monitor_dpi_aware = 2
74
75
76def set_dpi_awareness(process_dpi_awareness: ProcessDpiAwareness):
77    if 'nt' != os.name:
78        return
79    
80    from ctypes import c_int, windll, byref
81    from platform import win32_ver
82
83    # Query DPI Awareness (Windows 10 and 8)
84    awareness = c_int()
85    errorCode = windll.shcore.GetProcessDpiAwareness(0, byref(awareness))
86    # print(awareness.value)
87
88    windows_major_version, _, _, _ = win32_ver()
89    if windows_major_version in {'8', '10'}:
90        # Set DPI Awareness  (Windows 10 and 8)
91        errorCode = windll.shcore.SetProcessDpiAwareness(int(process_dpi_awareness))
92        # the argument is the awareness level, which can be 0, 1 or 2:
93        # for 1-to-1 pixel control I seem to need it to be non-zero (I'm using level 2)
94    else:
95        # Set DPI Awareness  (Windows 7 and Vista)
96        success = windll.user32.SetProcessDPIAware()
97        # 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)
class ProcessDpiAwareness(enum.IntEnum):
71class ProcessDpiAwareness(IntEnum):
72    process_dpi_unaware = 0
73    process_system_dpi_aware = 1
74    process_per_monitor_dpi_aware = 2

An enumeration.

process_dpi_unaware = <ProcessDpiAwareness.process_dpi_unaware: 0>
process_system_dpi_aware = <ProcessDpiAwareness.process_system_dpi_aware: 1>
process_per_monitor_dpi_aware = <ProcessDpiAwareness.process_per_monitor_dpi_aware: 2>
Inherited Members
enum.Enum
name
value
builtins.int
conjugate
bit_length
to_bytes
from_bytes
as_integer_ratio
real
imag
numerator
denominator
def set_dpi_awareness( process_dpi_awareness: ProcessDpiAwareness):
77def set_dpi_awareness(process_dpi_awareness: ProcessDpiAwareness):
78    if 'nt' != os.name:
79        return
80    
81    from ctypes import c_int, windll, byref
82    from platform import win32_ver
83
84    # Query DPI Awareness (Windows 10 and 8)
85    awareness = c_int()
86    errorCode = windll.shcore.GetProcessDpiAwareness(0, byref(awareness))
87    # print(awareness.value)
88
89    windows_major_version, _, _, _ = win32_ver()
90    if windows_major_version in {'8', '10'}:
91        # Set DPI Awareness  (Windows 10 and 8)
92        errorCode = windll.shcore.SetProcessDpiAwareness(int(process_dpi_awareness))
93        # the argument is the awareness level, which can be 0, 1 or 2:
94        # for 1-to-1 pixel control I seem to need it to be non-zero (I'm using level 2)
95    else:
96        # Set DPI Awareness  (Windows 7 and Vista)
97        success = windll.user32.SetProcessDPIAware()
98        # 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)