[OOBE][WinUI3]Scale OOBE window correctly (#17962)

* Scale OOBE window

* [oobe]React to dpi changes (#17967)

Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
This commit is contained in:
Stefan Markovic
2022-04-29 11:06:50 +02:00
committed by GitHub
parent 63d2a5dd71
commit ae0bf84431
2 changed files with 45 additions and 8 deletions

View File

@@ -41,6 +41,9 @@ namespace Microsoft.PowerToys.Settings.UI.Helpers
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern bool ShowWindow(System.IntPtr hWnd, int nCmdShow); public static extern bool ShowWindow(System.IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern int GetDpiForWindow(System.IntPtr hWnd);
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern bool AllowSetForegroundWindow(int dwProcessId); public static extern bool AllowSetForegroundWindow(int dwProcessId);

View File

@@ -4,6 +4,7 @@
using System; using System;
using interop; using interop;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.OOBE.Enums; using Microsoft.PowerToys.Settings.UI.OOBE.Enums;
using Microsoft.PowerToys.Settings.UI.OOBE.Views; using Microsoft.PowerToys.Settings.UI.OOBE.Views;
using Microsoft.UI; using Microsoft.UI;
@@ -21,28 +22,44 @@ namespace Microsoft.PowerToys.Settings.UI
{ {
private PowerToysModules initialModule; 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) public OobeWindow(PowerToysModules initialModule)
{ {
this.InitializeComponent(); this.InitializeComponent();
// Set window icon // Set window icon
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this); _hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
WindowId windowId = Win32Interop.GetWindowIdFromWindow(hWnd); _windowId = Win32Interop.GetWindowIdFromWindow(_hWnd);
AppWindow appWindow = AppWindow.GetFromWindowId(windowId); _appWindow = AppWindow.GetFromWindowId(_windowId);
appWindow.SetIcon("icon.ico"); _appWindow.SetIcon("icon.ico");
OverlappedPresenter presenter = appWindow.Presenter as OverlappedPresenter; OverlappedPresenter presenter = _appWindow.Presenter as OverlappedPresenter;
presenter.IsResizable = false; presenter.IsResizable = false;
presenter.IsMinimizable = false; presenter.IsMinimizable = false;
presenter.IsMaximizable = 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; SizeInt32 size;
size.Width = 1650; size.Width = width;
size.Height = 1050; size.Height = height;
appWindow.Resize(size); _appWindow.Resize(size);
this.initialModule = initialModule; this.initialModule = initialModule;
this.SizeChanged += OobeWindow_SizeChanged;
ResourceLoader loader = ResourceLoader.GetForViewIndependentUse(); ResourceLoader loader = ResourceLoader.GetForViewIndependentUse();
Title = loader.GetString("OobeWindow_Title"); Title = loader.GetString("OobeWindow_Title");
@@ -67,6 +84,23 @@ namespace Microsoft.PowerToys.Settings.UI
}); });
} }
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) private void Window_Closed(object sender, WindowEventArgs args)
{ {
App.ClearOobeWindow(); App.ClearOobeWindow();