using Microsoft.UI; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; using System; using System.Runtime.InteropServices; using Windows.Graphics; using WinRT.Interop; namespace Sonex.Client.Dialogs { public static class AppWindowsHelper { public static void CenterWindow(Window window, int width, int height) { var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(window); var windowId = Win32Interop.GetWindowIdFromWindow(hwnd); var appWindow = AppWindow.GetFromWindowId(windowId); var displayArea = DisplayArea.GetFromWindowId( windowId, DisplayAreaFallback.Primary); var x = (displayArea.WorkArea.Width - width) / 2; var y = (displayArea.WorkArea.Height - height) / 2; appWindow.MoveAndResize(new RectInt32(x, y, width, height)); } [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); public static void BringToFront(Window window) { window.Activate(); // WinUI 3 / Windows App SDK 1.4+ window.AppWindow.MoveInZOrderAtTop(); var hwnd = WindowNative.GetWindowHandle(window); SetForegroundWindow(hwnd); } } }