From 688a45f57e91d6825fe88341ef928f9c772d0d50 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 26 Jun 2022 22:26:18 +0200 Subject: [PATCH] Ensure that we keep the window at the same display when resizing Use wxRect::Inflate(), which preserves the position of the center of the rectangle, instead of just changing its size, which could the position of the center and result in the window snapping back to the previous display (and, even more catastrophically, then being moved back to the new one when handling the resulting WM_DPICHANGED and so on forever). --- src/msw/nonownedwnd.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/msw/nonownedwnd.cpp b/src/msw/nonownedwnd.cpp index d9a7760615..cdeee88d54 100644 --- a/src/msw/nonownedwnd.cpp +++ b/src/msw/nonownedwnd.cpp @@ -292,9 +292,15 @@ bool wxNonOwnedWindow::HandleDPIChange(const wxSize& newDPI, const wxRect& newRe // size is usually a decent guess, it's typically not exactly correct. // We can't always do much better, but at least ensure that the window // is still big enough to show its contents. - wxSize newSize = newRect.GetSize(); - newSize.IncTo(GetBestSize()); - SetSize(wxRect(newRect.GetPosition(), newSize)); + wxSize diff = GetBestSize() - newRect.GetSize(); + diff.IncTo(wxSize(0, 0)); + + // Use wxRect::Inflate() to ensure that the center of the (possibly) + // bigger rectangle is at the same position as the center of the + // proposed one, to prevent moving the window back to the old display + // from which it might have been just moved to this one, as doing this + // would result in an infinite stream of WM_DPICHANGED messages. + SetSize(newRect.Inflate(diff.x, diff.y)); } Refresh();