From fb2cf67ba63607c59b5d6a5ce6b6c2307805bd63 Mon Sep 17 00:00:00 2001 From: punker76 Date: Tue, 20 Feb 2024 22:34:19 +0100 Subject: [PATCH] fix: #4449 move saved maximized window before set window position Closes #4449 --- src/MahApps.Metro/Native/WinApiHelper.cs | 13 ++++++++++++- src/MahApps.Metro/NativeMethods.txt | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/MahApps.Metro/Native/WinApiHelper.cs b/src/MahApps.Metro/Native/WinApiHelper.cs index e494dbcfe..743fe7f7d 100644 --- a/src/MahApps.Metro/Native/WinApiHelper.cs +++ b/src/MahApps.Metro/Native/WinApiHelper.cs @@ -110,9 +110,20 @@ public static unsafe void SetWindowPlacement(Window? window, WINDOWPLACEMENT? wp } var hWnd = new WindowInteropHelper(window).EnsureHandle(); + + var placementBefore = new WINDOWPLACEMENT { length = (uint)Marshal.SizeOf() }; + PInvoke.GetWindowPlacement((HWND)hWnd, &placementBefore); + if (placementBefore.showCmd is SHOW_WINDOW_CMD.SW_NORMAL or SHOW_WINDOW_CMD.SW_SHOWMAXIMIZED && placement.showCmd is SHOW_WINDOW_CMD.SW_SHOWMAXIMIZED) + { + if (PInvoke.MoveWindow((HWND)hWnd, placement.rcNormalPosition.X, placement.rcNormalPosition.Y, placement.rcNormalPosition.Width, placement.rcNormalPosition.Height, false) == false) + { + Trace.TraceWarning($"{window}: The window can not moved! (MoveWindow) {placement}"); + } + } + if (PInvoke.SetWindowPlacement(new HWND(hWnd), &placement) == false) { - Trace.TraceWarning($"{window}: The window placement {wp} could not be (SetWindowPlacement)!"); + Trace.TraceWarning($"{window}: The window placement {wp} could not be set (SetWindowPlacement) {placement}!"); } } diff --git a/src/MahApps.Metro/NativeMethods.txt b/src/MahApps.Metro/NativeMethods.txt index a596d2f99..1ff1c1bb0 100644 --- a/src/MahApps.Metro/NativeMethods.txt +++ b/src/MahApps.Metro/NativeMethods.txt @@ -6,6 +6,7 @@ LoadLibrary LoadString MapVirtualKey MonitorFromWindow +MoveWindow ReleaseCapture SendMessage SetWindowPlacement