Skip to content

Fix SetupWindowForScreen using wrong DPI source#30

Open
shanselman wants to merge 1 commit intomasterfrom
fix/setup-window-dpi-source
Open

Fix SetupWindowForScreen using wrong DPI source#30
shanselman wants to merge 1 commit intomasterfrom
fix/setup-window-dpi-source

Conversation

@shanselman
Copy link
Owner

SetupWindowForScreen was using PresentationSource.FromVisual(this) to get DPI, which returns the DPI of the monitor the window is currently on - not the target screen. Replaced with GetDpiForScreen(screen) which correctly queries the target monitor DPI via GetDpiForMonitor P/Invoke.

SetupWindowForScreen was using PresentationSource.FromVisual(this) which
returns the DPI of the monitor the window is currently on, not the target
screen's DPI. This is incorrect when positioning onto a different monitor.

Replaced with GetDpiForScreen(screen) which correctly queries the target
monitor's DPI via GetDpiForMonitor P/Invoke.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a DPI handling bug in multi-monitor setups where SetupWindowForScreen was incorrectly using the current window's monitor DPI instead of the target screen's DPI. The fix ensures windows are correctly positioned on monitors with different DPI scaling settings.

Changes:

  • Replaced PresentationSource.FromVisual(this) DPI retrieval with GetDpiForScreen(screen) in SetupWindowForScreen to query the target monitor's actual DPI
  • Added DpiChanged event handler for monitor windows to dynamically handle runtime DPI changes

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1147 to +1150
window.Left = screen.WorkingArea.X / ctx.DpiScaleX;
window.Top = screen.WorkingArea.Y / ctx.DpiScaleY;
window.Width = screen.WorkingArea.Width / ctx.DpiScaleX;
window.Height = screen.WorkingArea.Height / ctx.DpiScaleY;
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The window repositioning and resizing in the DpiChanged handler should include a guard to prevent unnecessary updates when values haven't changed significantly. The main window's OnDpiChanged (lines 601-603) and the Loaded handler (lines 1125-1126) both check if the new position/size differs significantly before updating. Without this guard, there's a potential for unnecessary layout cycles or edge-case infinite loops if setting the window properties somehow triggers another DPI change event.

Copilot uses AI. Check for mistakes.
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.

2 participants