Per-Monitor V2 DPI Coordinate Metrology for Reliable Windows UIA Automation
Automated desktop and browser tests frequently work at 100% scaling (96 DPI) and miss controls by 40 to 150 pixels the moment a user runs a primary display at 125% (120 DPI), 150% (144 DPI), or spans an external monitor. The root cause is mixing logical CSS pixels (window.devicePixelRatio), DPI-virtualized Win32 client coordinates, and physical screen coordinates reported by UI Automation (UIA).
1. Declare Per-Monitor V2 Context Before Window Creation
Every native Windows automation probe or desktop shell (ZeroToCodeLab.exe and UiaDpiVerifier.exe) must call SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) before allocating HWNDs or message loops. Without Per-Monitor V2, Windows bitmap-scales UIA bounding rectangles transparently and returns stale coordinates relative to system DPI.
2. Exact Logical-to-Physical Projection
Inside a Chromium/Edge or WebView2 client area (Chrome Legacy Window / Chrome_WidgetWin_1):
- Scale factor equals
dpi / 96.0(for example,120 / 96 = 1.2500). - Given Win32 client origin
(originPhysX, originPhysY)fromClientToScreen, a target center at(xCss, yCss)maps to physical screen pixel(originPhysX + round(xCss * scale), originPhysY + round(yCss * scale)). - Inverting back to CSS pixels yields bounded sub-pixel rounding parity (
<= 1.0 CSS px), ensuring clickable hit-test regions match between DOM layout and Win32 pointer injection.
3. Non-Intrusive Background Verification
Launching Playwright/CDP automation on an isolated port (127.0.0.1:9222) inside a dedicated minimized profile preserves the operator's active foreground window (GetForegroundWindow() unchanged) while still allowing UIA accessibility inspection and hardware-accelerated WebView2 telemetry.
Experiment interactively with multi-monitor DPI transitions and axis-aligned bounding-box hit parity here: https://nahianakhtar.github.io/demos/uia-metrology/.