diff --git a/src/settings-ui/Settings.UI/Helpers/NativeMethods.cs b/src/settings-ui/Settings.UI/Helpers/NativeMethods.cs index 389a0e56b00..376a1d097b1 100644 --- a/src/settings-ui/Settings.UI/Helpers/NativeMethods.cs +++ b/src/settings-ui/Settings.UI/Helpers/NativeMethods.cs @@ -41,6 +41,9 @@ public static class NativeMethods [DllImport("user32.dll")] public static extern bool ShowWindow(System.IntPtr hWnd, int nCmdShow); + [DllImport("user32.dll")] + public static extern int GetDpiForWindow(System.IntPtr hWnd); + [DllImport("user32.dll")] public static extern bool AllowSetForegroundWindow(int dwProcessId); diff --git a/src/settings-ui/Settings.UI/OobeWindow.xaml.cs b/src/settings-ui/Settings.UI/OobeWindow.xaml.cs index d351fe4d550..cce140fcd09 100644 --- a/src/settings-ui/Settings.UI/OobeWindow.xaml.cs +++ b/src/settings-ui/Settings.UI/OobeWindow.xaml.cs @@ -4,6 +4,7 @@ using System; using interop; +using Microsoft.PowerToys.Settings.UI.Helpers; using Microsoft.PowerToys.Settings.UI.OOBE.Enums; using Microsoft.PowerToys.Settings.UI.OOBE.Views; using Microsoft.UI; @@ -21,28 +22,44 @@ public sealed partial class OobeWindow : Window { private PowerToysModules initialModule; + private const int ExpectedWidth = 1100; + private const int ExpectedHeight = 700; + private const int DefaultDPI = 96; + private int _currentDPI; + private WindowId _windowId; + private IntPtr _hWnd; + private AppWindow _appWindow; + public OobeWindow(PowerToysModules initialModule) { this.InitializeComponent(); // Set window icon - var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this); - WindowId windowId = Win32Interop.GetWindowIdFromWindow(hWnd); - AppWindow appWindow = AppWindow.GetFromWindowId(windowId); - appWindow.SetIcon("icon.ico"); + _hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this); + _windowId = Win32Interop.GetWindowIdFromWindow(_hWnd); + _appWindow = AppWindow.GetFromWindowId(_windowId); + _appWindow.SetIcon("icon.ico"); - OverlappedPresenter presenter = appWindow.Presenter as OverlappedPresenter; + OverlappedPresenter presenter = _appWindow.Presenter as OverlappedPresenter; presenter.IsResizable = false; presenter.IsMinimizable = false; presenter.IsMaximizable = false; + var dpi = NativeMethods.GetDpiForWindow(_hWnd); + _currentDPI = dpi; + float scalingFactor = (float)dpi / DefaultDPI; + int width = (int)(ExpectedWidth * scalingFactor); + int height = (int)(ExpectedHeight * scalingFactor); + SizeInt32 size; - size.Width = 1650; - size.Height = 1050; - appWindow.Resize(size); + size.Width = width; + size.Height = height; + _appWindow.Resize(size); this.initialModule = initialModule; + this.SizeChanged += OobeWindow_SizeChanged; + ResourceLoader loader = ResourceLoader.GetForViewIndependentUse(); Title = loader.GetString("OobeWindow_Title"); @@ -67,6 +84,23 @@ public OobeWindow(PowerToysModules initialModule) }); } + private void OobeWindow_SizeChanged(object sender, WindowSizeChangedEventArgs args) + { + var dpi = NativeMethods.GetDpiForWindow(_hWnd); + if (_currentDPI != dpi) + { + // Reacting to a DPI change. Should not cause a resize -> sizeChanged loop. + _currentDPI = dpi; + float scalingFactor = (float)dpi / DefaultDPI; + int width = (int)(ExpectedWidth * scalingFactor); + int height = (int)(ExpectedHeight * scalingFactor); + SizeInt32 size; + size.Width = width; + size.Height = height; + _appWindow.Resize(size); + } + } + private void Window_Closed(object sender, WindowEventArgs args) { App.ClearOobeWindow();