我正在尝试使用PySDL2绑定为具有SDL2的Windows 10编写DPI感知应用程序。目前,我不关心支持任何其他比“周年更新”更早的操作系统或Windows版本。问题是,我没有收到WM_DPICHANGED消息,当在具有不同缩放比例的监视器之间拖动时,我需要调整窗口大小。
我使用Windows API将整个过程设置为可识别每显示器dpi(v2),这似乎可以正常工作。窗口内容永远不会缩放,我可以正确地轮询当前的显示DPI,并且非客户端区域(例如标题栏)也将由Windows缩放。如WM_MOUSEMOVE所示,其他本地Windows消息也将通过。
版本:Windows 10 1809,Python 3.7.2,SDL 2.0.9全部为64位,PySDL2 0.9.6,如果要在本地运行此文件,则需要将SDL2.dll放在以下目录的“ / native”子目录中您的工作目录,或适当修改第3行
import sys
import os
os.environ["PYSDL2_DLL_PATH"] = os.path.abspath(os.getcwd() + "/native")
import sdl2.ext
import ctypes
# https://docs.microsoft.com/de-de/windows/desktop/hidpi/wm-dpichanged
WM_DPICHANGED = 0x02E0
WM_MOUSEMOVE = 0x0200
# https://docs.microsoft.com/de-de/windows/desktop/hidpi/dpi-awareness-context
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = -4
def run():
# Set the DPI Awareness Context to DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 (-4), which means we need to do all
# DPI scaling by ourselves, EXCEPT for win32 popups and non client areas (like the title bar)
# This function was introduced with the Windows 10 Anniversary Update, so we should probably have fallbacks for
# other operating system versions
# https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setprocessdpiawarenesscontext
if not ctypes.windll.user32.SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2):
print("Unable to set DPI awareness")
sdl2.ext.init()
# We want to receive native WM_DPICHANGED messages, so we need to tell SDL to pass them through
sdl2.SDL_EventState(sdl2.SDL_SYSWMEVENT, sdl2.SDL_ENABLE)
window_width = 640
window_height = 480
window = sdl2.ext.Window("λ³", (window_width, window_height), flags=sdl2.SDL_WINDOW_OPENGL | sdl2.SDL_WINDOW_ALLOW_HIGHDPI)
sdl2_window = window.window
sdl2.SDL_SetWindowResizable(sdl2_window, True)
window.show()
running = True
scaling_percentage = 100
while running:
sdl2_events = sdl2.ext.get_events()
for sdl2_event in sdl2_events:
if sdl2_event.type == sdl2.SDL_QUIT:
running = False
break
elif sdl2_event.type == sdl2.SDL_SYSWMEVENT:
message_pointer = sdl2_event.syswm.msg
# Workaround because the default pointer type that is returned has no definition for the structure
message_pointer_usable = ctypes.cast(message_pointer, ctypes.POINTER(sdl2.syswm.SDL_SysWMmsg))
sys_wm_msg = message_pointer_usable.contents
windows_msg = message_pointer_usable.contents.msg.win
# On 32 bit Windows both W and L are 32 bit, on 64 bit Windows both are 64 bit
# When values are stored in the 'high' and 'low' 'word' of W or L, we still mean the first 16 bit or
# 16 bit after that, regardless of the platform
l_high = (windows_msg.lParam & 0xFFFF0000) >> 16
l_low = windows_msg.lParam & 0xFFFF
w_high = (windows_msg.wParam & 0xFFFF0000) >> 16
w_low = windows_msg.wParam & 0xFFFF
if windows_msg.msg == WM_DPICHANGED:
print("This never gets called")
if windows_msg.msg == WM_MOUSEMOVE:
# This works
print(f"x {l_low}, y {l_high}")
display_index = sdl2.SDL_GetWindowDisplayIndex(sdl2_window)
diagonal_dpi = ctypes.c_float()
horizontal_dpi = ctypes.c_float()
vertical_dpi = ctypes.c_float()
# This also works and reports the percentage correctly when dragging between monitors
sdl2.SDL_GetDisplayDPI(display_index, ctypes.byref(diagonal_dpi), ctypes.byref(horizontal_dpi), ctypes.byref(vertical_dpi))
new_scaling_percentage = int(diagonal_dpi.value/96*100)
if new_scaling_percentage != scaling_percentage:
scaling_percentage = new_scaling_percentage
print(f"Display scale changed to {scaling_percentage}%")
sdl2.SDL_Delay(10)
sdl2.ext.quit()
return 0
if __name__ == "__main__":
sys.exit(run())