Fix OverflowException in WindowChromeWorker._HandleNCHitTest when dragging across different-DPI monitors - #1997
Conversation
…gging across different-DPI monitors Dragging the preview window onto a monitor with a different DPI (e.g. a non-primary 4K display) crashed with an arithmetic overflow inside WPF's WindowChromeWorker hit-test. The crash was triggered on WM_NCHITTEST, whose DPI math can't be made safe from the outside, so: - Short-circuit WM_NCHITTEST to HTCLIENT in ViewerWindow, and restore window dragging by hand (WM_NCLBUTTONDOWN + HT CAPTION) on the title area. - Apply the WM_DPICHANGED suggested rect so the HWND and WPF geometry stay in sync as the window moves between monitors. - Opt into Per-Monitor V2 DPI awareness (SetProcessDpiAwarenessContext) with a V1 fallback. - Clamp MoveWindow coordinates/size and sanitize non-finite window size so a degenerate rect can never reach Win32/WPF. Fixes QL-Win#1996
Reviewer's GuideThe PR prevents the .NET Framework 4.6.2 WindowChrome WM_NCHITTEST overflow during cross-DPI monitor dragging by bypassing WPF hit-testing, restoring native title-bar dragging, synchronizing WM_DPICHANGED geometry, enabling Per-Monitor V2 DPI awareness with fallback, and sanitizing all window coordinates and sizes. Sequence diagram for cross-DPI viewer window draggingsequenceDiagram
actor User
participant ViewerWindow
participant WPF as WindowChromeWorker
participant Win32
participant Monitor
User->>ViewerWindow: TitleArea_MouseLeftButtonDown
ViewerWindow->>Win32: ReleaseCapture()
ViewerWindow->>Win32: SendMessage(WM_NCLBUTTONDOWN, HTCAPTION)
User->>Monitor: Drag window across DPI boundary
Monitor-->>ViewerWindow: WM_DPICHANGED(suggestedRect)
ViewerWindow->>Win32: MoveWindow(suggestedRect)
User->>ViewerWindow: Window message WM_NCHITTEST
ViewerWindow-->>WPF: HTCLIENT
Note over ViewerWindow,WPF: WPF WindowChrome hit-testing is bypassed
Flow diagram for safe viewer window geometryflowchart TD
Input["Requested window size and position"] --> Size["PositionWindow"]
Size --> Finite["FinitePositive"]
Finite --> Move["MoveWindow"]
Move --> Clamp["ToInt32Clamped"]
Clamp --> Rect["Clamped coordinates and minimum dimensions"]
Rect --> Win32["User32.MoveWindow"]
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="QuickLook/ViewerWindow.xaml.cs" line_range="222-226" />
<code_context>
+ // overflow is in WPF's own DPI math, so no condition on the window rect can reliably
+ // detect it). Dragging still works because the content panels use WM_NCLBUTTONDOWN(HT
+ // CAPTION) directly rather than relying on a hit test result.
+ if (msg == WM_NCHITTEST)
+ {
+ handled = true;
+ return new IntPtr(1); // HTCLIENT
+ }
+
+ if (msg == WM_DPICHANGED && lParam != IntPtr.Zero)
</code_context>
<issue_to_address>
**issue (bug_risk):** Returning HTCLIENT for every WM_NCHITTEST prevents WindowChrome from reporting resize hit-test zones such as HTLEFT, HTRIGHT, HTTOP, and HTBOTTOM. Because ViewerWindow remains configured with ResizeMode="CanResize", users can no longer resize the preview window through its borders.
**Triggers:** When the user attempts to resize the preview window from any border or corner.
**Suggested fix:** Handle only the problematic hit-test path, or implement the WindowChrome resize hit-test zones and initiate native resizing manually instead of returning HTCLIENT for every point.
</issue_to_address>Sourcery assessment
Approval pending. 1 finding to address first.
Blocking findings: QuickLook/ViewerWindow.xaml.cs:226
… WM_NCHITTEST Sourcery correctly flagged that returning HTCLIENT for every WM_NCHITTEST disabled the resize borders while ResizeMode=CanResize. Report HTLEFT/HTRIGHT/ HTTOP/HTBOTTOM and the corner zones at the window edges, and HTCLIENT elsewhere, still bypassing the WindowChrome hit-test that overflows during cross-DPI drags.
…t32() The OverflowException that remained on monitors above/left of the primary was in our own WndProc, not WPF. IntPtr.ToInt32() compiles to conv.u8 + conv.ovf.i4; when a screen coordinate is negative Windows sign-extends lParam's high 32 bits, so ToInt32() sees a huge unsigned value and overflows. Decode the packed signed 16-bit mouse coordinates via ToInt64() + an unchecked low-32-bit cast and sign extension, which never overflows. Also make the WndProc hook re-attachable (for HwndSource recreation after a display change) and detach it in OnClosing.
Update: root cause identified preciselyAfter field-testing across several monitor arrangements, the actual overflow is now confirmed. It is not in WPF's Root cause
On a 64-bit process, when the mouse is on a monitor above or left of the primary (negative screen coordinate), Windows sign-extends This is exactly why it reproduced with the monitor above the primary (Y negative) but not left (X negative keeps the low 16-bit half negative while lParam stays a 32-bit positive value). It is monitor-arrangement/coordinate-sign dependent, not resolution dependent — so any arrangement with a negative-coordinate monitor (2-3× 8K included) is covered. FixDecode the packed signed 16-bit mouse coordinates using int v = unchecked((int)lParam.ToInt64());
int lo = v & 0xFFFF;
int hi = (v >> 16) & 0xFFFF;
int mx = (lo & 0x8000) != 0 ? lo - 0x10000 : lo;
int my = (hi & 0x8000) != 0 ? hi - 0x10000 : hi;This is pure int math with no checked conversion, so it can never overflow. Verified by decompiling the built exe: Also
|
…und window GetCurrentDesktopRectInPixel and GetCurrentScaleFactor picked the monitor from GetForegroundWindow(). When the selected file sits on the desktop, the foreground window is the desktop (Progman), which always resolves to the primary monitor, so a file on a secondary desktop would open its preview on the wrong screen. Use the cursor position (Screen.FromPoint / MonitorFromPoint) instead, which is where the user is looking when they press Space.
Additional fix: preview opened on the wrong monitor for files on a secondary desktopWhile testing, a file selected on a secondary monitor's desktop would open its preview on the primary monitor. This only happened for desktop files — files selected inside Explorer on the same monitor were positioned correctly. Root cause
FixDetermine the monitor from the cursor position instead — when the user presses Space, the cursor is on the monitor where the file is:
Added |
…e-race overflow MediaInfoViewer: - Set MediaInfo's output language from the UI culture so field labels render in the user's language (e.g. Chinese), with a fallback to English. - Translate PE/EXE field labels that the zh-CN language CSV is missing, and normalize full-width fallback labels (Linker_Version → Linker_Version). - Replace the fixed-width text block with a two-column grid (SharedSizeGroup) so every label/value pair aligns pixel-perfectly regardless of CJK/ASCII mixing, with a bold section header and a dedicated colon column. ViewerWindow: - Stop removing the WM_NCHITTEST hook in OnClosing. Removing it before base.OnClosing leaves the still-alive window unguarded for an instant, so a WM_NCHITTEST (e.g. mouse resting on the close button) can fall through to WPF's WindowChromeWorker and overflow. The HwndSource releases the hook chain when the window actually closes.
Additional fixes1. MediaInfo viewer: localized output + aligned table layout
2. Close-button overflow race
|
Problem
Dragging the preview window onto a monitor with a different DPI — e.g. a non-primary 4K display — crashed QuickLook with:
This is the crash reported in #1996 (crashes when dragging the preview window on a non-primary monitor). It happens on any preview type — the trigger is the shared
ViewerWindow, not a specific plugin.Root cause
The overflow is raised by WPF (.NET Framework 4.6.2)
WindowChromeWorker._HandleNCHitTeston theWM_NCHITTESTmessage. I decompiled the installedPresentationFramework.dlland confirmed that_HitTestNcaandDpiHelper.DevicePixelsToLogical/DeviceRectToLogicalcontain no overflowable(int)casts — the bad arithmetic lives entirely inside WPF's per-window DPI math, which becomes inconsistent as the window is dragged onto a monitor with a different DPI. That inconsistency can't be corrected from the outside, so the safe fix is to avoid the offending hit-test entirely.Changes
ViewerWindow: answerWM_NCHITTESTwithHTCLIENTto bypass the overflowing WindowChrome hit-test, and restore window dragging manually (WM_NCLBUTTONDOWN+HT CAPTION) on the title area, sinceWindow.DragMove()depends on a hit-test result.ViewerWindow: apply theWM_DPICHANGEDsuggested rect so the HWND and WPF geometry stay in sync when the window moves between monitors.App/SHCore: opt into Per-Monitor V2 DPI awareness (SetProcessDpiAwarenessContext) with a V1 fallback.WindowHelper/ViewerWindow.Actions: clampMoveWindowcoordinates/size and sanitize non-finite window sizes so a degenerate rect can never reach Win32/WPF.Fixes #1996.
Summary by Sourcery
Prevent cross-monitor DPI preview crashes while improving monitor-aware window placement and MediaInfo presentation.
New Features:
Bug Fixes:
Enhancements: