From 4426df671e73bf722c3092e5fb25f0dd1f0ea970 Mon Sep 17 00:00:00 2001 From: Davide Giacometti Date: Mon, 5 Feb 2024 18:05:02 +0100 Subject: [PATCH] [Peek]Fix title bar glitch after maximizing window (#31172) --- .../Peek.UI/Extensions/WindowExtensions.cs | 25 ++++++++++++++++--- src/modules/peek/Peek.UI/NativeMethods.txt | 4 ++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/modules/peek/Peek.UI/Extensions/WindowExtensions.cs b/src/modules/peek/Peek.UI/Extensions/WindowExtensions.cs index c7fd32a875..b4f9b58b57 100644 --- a/src/modules/peek/Peek.UI/Extensions/WindowExtensions.cs +++ b/src/modules/peek/Peek.UI/Extensions/WindowExtensions.cs @@ -2,7 +2,6 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.Runtime.InteropServices; using ManagedCommon; using Microsoft.UI.Xaml; @@ -25,12 +24,31 @@ namespace Peek.UI.Extensions internal static void CenterOnMonitor(this Window window, HWND hwndDesktop, double? width = null, double? height = null) { var hwndToCenter = new HWND(window.GetWindowHandle()); + + // If the window is maximized, restore to normal state before change its size + var placement = default(WINDOWPLACEMENT); + if (PInvoke.GetWindowPlacement(hwndToCenter, ref placement)) + { + if (placement.showCmd == SHOW_WINDOW_CMD.SW_MAXIMIZE) + { + placement.showCmd = SHOW_WINDOW_CMD.SW_SHOWNORMAL; + if (!PInvoke.SetWindowPlacement(hwndToCenter, in placement)) + { + Logger.LogError($"SetWindowPlacement failed with error {Marshal.GetLastWin32Error()}"); + } + } + } + else + { + Logger.LogError($"GetWindowPlacement failed with error {Marshal.GetLastWin32Error()}"); + } + var monitor = PInvoke.MonitorFromWindow(hwndDesktop, MONITOR_FROM_FLAGS.MONITOR_DEFAULTTONEAREST); MONITORINFO info = default(MONITORINFO); info.cbSize = 40; PInvoke.GetMonitorInfo(monitor, ref info); var dpi = PInvoke.GetDpiForWindow(new HWND(hwndDesktop)); - PInvoke.GetWindowRect(new HWND(hwndToCenter), out RECT windowRect); + PInvoke.GetWindowRect(hwndToCenter, out RECT windowRect); var scalingFactor = dpi / 96d; var w = width.HasValue ? (int)(width * scalingFactor) : windowRect.right - windowRect.left; var h = height.HasValue ? (int)(height * scalingFactor) : windowRect.bottom - windowRect.top; @@ -38,7 +56,8 @@ namespace Peek.UI.Extensions var cy = (info.rcMonitor.bottom + info.rcMonitor.top) / 2; var left = cx - (w / 2); var top = cy - (h / 2); - SetWindowPosOrThrow(new HWND(hwndToCenter), default(HWND), left, top, w, h, SET_WINDOW_POS_FLAGS.SWP_SHOWWINDOW); + + SetWindowPosOrThrow(hwndToCenter, default(HWND), left, top, w, h, SET_WINDOW_POS_FLAGS.SWP_NOACTIVATE); } private static void SetWindowPosOrThrow(HWND hWnd, HWND hWndInsertAfter, int x, int y, int cx, int cy, SET_WINDOW_POS_FLAGS uFlags) diff --git a/src/modules/peek/Peek.UI/NativeMethods.txt b/src/modules/peek/Peek.UI/NativeMethods.txt index 3e15b22f94..e65e72c12f 100644 --- a/src/modules/peek/Peek.UI/NativeMethods.txt +++ b/src/modules/peek/Peek.UI/NativeMethods.txt @@ -17,4 +17,6 @@ _SVGIO MONITORINFO GetWindowRect SET_WINDOW_POS_FLAGS -SetWindowPos \ No newline at end of file +SetWindowPos +GetWindowPlacement +SetWindowPlacement \ No newline at end of file