Skip to content

Windows: per-monitor DPI thread context for monitors() and grab() (#219) - #612

Open
jeanluyac-sketch wants to merge 1 commit into
BoboTiG:mainfrom
jeanluyac-sketch:fix-219-per-monitor-dpi
Open

jeanluyac-sketch wants to merge 1 commit into
BoboTiG:mainfrom
jeanluyac-sketch:fix-219-per-monitor-dpi

Conversation

@jeanluyac-sketch

Copy link
Copy Markdown
Contributor

Fixes #219

_set_dpi_awareness() calls SetProcessDpiAwareness(2) and ignores the result. It fails with E_ACCESSDENIED when the process DPI mode was already set (host executable manifest, GUI toolkit, earlier call), so the process stays system-DPI aware and monitors with a different scaling than the primary one report scaled sizes (2926 = 2560 × 200%/175% in the issue).

monitors() and grab() now run inside SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) (falling back to V1 before Windows 10 1703) and restore the caller's previous thread context afterwards, so the host application's DPI behaviour is untouched. On Windows without the API it is a no-op and the existing process-level call still applies.

  • Tests added/updated: unit tests for set/restore, restore on error, V1 fallback and missing API, plus a live test that monitors() leaves the thread context unchanged
  • Documentation updated (or your review concluded: not needed)
  • Release notes added (v11.0.0 notes)
  • ./check.ps1 passed

Tested on Windows 11 / Python 3.14 with four monitors: test_windows.py 15/15 pass. The full suite has 5 failures that also happen on main on this machine (a monitor at a negative x origin); I'll send a separate tests-only PR for those. Caveat: all my monitors are at 100% scaling, so I could not reproduce the original mixed-DPI sizes; a check from someone with mixed scaling would be welcome.

AI assistance disclosure

  • AI was partially used to create this change: the diagnosis, implementation and tests were drafted with Claude; I reviewed them and ran the tests on my Windows machine.

…() (BoboTiG#219)

SetProcessDpiAwareness() fails with E_ACCESSDENIED when the process DPI
mode was already set, leaving the process system-DPI aware so monitors
with a different scaling than the primary one report scaled sizes.
Wrap monitors() and grab() in SetThreadDpiAwarenessContext(PER_MONITOR_AWARE_V2),
falling back to V1, and restore the caller's context afterwards.

AI-assisted (Claude): implementation and tests drafted with AI, reviewed by the author.
@BoboTiG
BoboTiG requested a balanced review from Copilot and removed request for Copilot December 1, 2026 08:53
@BoboTiG

BoboTiG commented Dec 1, 2026

Copy link
Copy Markdown
Owner

@halldorfannar I would like your input here, if possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

monitors Width and height errors when two monitors have different DPI

2 participants