Fix OverflowException in WindowChromeWorker._HandleNCHitTest when dragging across different-DPI monitors - #1997
Open
bobo198504 wants to merge 2 commits into
Open
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 changes from triggering WPF hit-test overflows in preview windows.
Bug Fixes:
Enhancements: