[Peek]Fix title bar glitch after maximizing window (#31172)

This commit is contained in:
Davide Giacometti
2024-02-05 18:05:02 +01:00
committed by GitHub
parent cf23574c6b
commit 4426df671e
2 changed files with 25 additions and 4 deletions

View File

@@ -2,7 +2,6 @@
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using ManagedCommon; using ManagedCommon;
using Microsoft.UI.Xaml; 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) internal static void CenterOnMonitor(this Window window, HWND hwndDesktop, double? width = null, double? height = null)
{ {
var hwndToCenter = new HWND(window.GetWindowHandle()); 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); var monitor = PInvoke.MonitorFromWindow(hwndDesktop, MONITOR_FROM_FLAGS.MONITOR_DEFAULTTONEAREST);
MONITORINFO info = default(MONITORINFO); MONITORINFO info = default(MONITORINFO);
info.cbSize = 40; info.cbSize = 40;
PInvoke.GetMonitorInfo(monitor, ref info); PInvoke.GetMonitorInfo(monitor, ref info);
var dpi = PInvoke.GetDpiForWindow(new HWND(hwndDesktop)); 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 scalingFactor = dpi / 96d;
var w = width.HasValue ? (int)(width * scalingFactor) : windowRect.right - windowRect.left; var w = width.HasValue ? (int)(width * scalingFactor) : windowRect.right - windowRect.left;
var h = height.HasValue ? (int)(height * scalingFactor) : windowRect.bottom - windowRect.top; 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 cy = (info.rcMonitor.bottom + info.rcMonitor.top) / 2;
var left = cx - (w / 2); var left = cx - (w / 2);
var top = cy - (h / 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) private static void SetWindowPosOrThrow(HWND hWnd, HWND hWndInsertAfter, int x, int y, int cx, int cy, SET_WINDOW_POS_FLAGS uFlags)

View File

@@ -18,3 +18,5 @@ MONITORINFO
GetWindowRect GetWindowRect
SET_WINDOW_POS_FLAGS SET_WINDOW_POS_FLAGS
SetWindowPos SetWindowPos
GetWindowPlacement
SetWindowPlacement