Skip to content

Commit

Permalink
Correctly handle DPI change and size in device independent units
Browse files Browse the repository at this point in the history
  • Loading branch information
dotMorten committed Apr 17, 2022
1 parent 3918150 commit 74b9de2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
29 changes: 25 additions & 4 deletions src/WinUIEx/WindowEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class WindowEx : Window
private readonly ContentControl windowArea;
private readonly WindowMessageMonitor mon;
private readonly Microsoft.UI.Windowing.OverlappedPresenter overlappedPresenter;
private uint currentDpi;

/// <summary>
/// Initializes a new instance of the <see cref="WindowEx"/> class.
Expand All @@ -34,6 +35,7 @@ public WindowEx()
{
overlappedPresenter = Microsoft.UI.Windowing.OverlappedPresenter.Create();
AppWindow.SetPresenter(overlappedPresenter);
currentDpi = this.GetDpiForWindow();

var rootContent = new Grid();
rootContent.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto), MinHeight = 0 });
Expand Down Expand Up @@ -79,9 +81,28 @@ private unsafe void OnWindowMessage(object? sender, Messaging.WindowMessageEvent
rect2->ptMinTrackSize.y = Math.Max((int)MinHeight, 39);
}
break;
case WindowsMessages.WM_DPICHANGED:
{
var newDpi = this.GetDpiForWindow();
if (newDpi != currentDpi)
{
var oldDpi = currentDpi;
currentDpi = newDpi;
OnDpiChanged(oldDpi, newDpi);
}
break;
}
}
}

private void OnDpiChanged(uint oldDpi, uint newDpi)
{
var oldScale = oldDpi / 96f;
var currentSize = AppWindow.Size;
this.SetWindowSize((int)(currentSize.Width / oldScale), (int)(currentSize.Height / oldScale));
currentDpi = newDpi;
}

private struct MINMAXINFO
{
#pragma warning disable CS0649
Expand Down Expand Up @@ -387,10 +408,10 @@ public Microsoft.UI.Windowing.AppWindowPresenterKind PresenterKind
/// </summary>
public double Width
{
get { return AppWindow.Size.Width; }
get { return AppWindow.Size.Width / (currentDpi / 96d); }
set
{
this.SetWindowSize(value, AppWindow.Size.Height);
this.SetWindowSize(value, Height);
}
}

Expand All @@ -399,10 +420,10 @@ public double Width
/// </summary>
public double Height
{
get { return AppWindow.Size.Height; }
get { return AppWindow.Size.Height / (currentDpi / 96d); }
set
{
this.SetWindowSize(AppWindow.Size.Width, value);
this.SetWindowSize(Width, value);
}
}

Expand Down
17 changes: 11 additions & 6 deletions src/WinUIEx/WindowExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static partial class WindowExtensions
/// <remarks>
/// <para><see href = "https://docs.microsoft.com/windows/win32/api//winuser/nf-winuser-getdpiforwindow">Learn more about this API from docs.microsoft.com</see>.</para>
/// </remarks>
public static uint GetDpiForWindow(this Microsoft.UI.Xaml.Window window) => (uint?)(window.Content?.XamlRoot?.RasterizationScale * 96f) ?? HwndExtensions.GetDpiForWindow(window.GetWindowHandle());
public static uint GetDpiForWindow(this Microsoft.UI.Xaml.Window window) => HwndExtensions.GetDpiForWindow(window.GetWindowHandle());

/// <summary>Brings the thread that created the specified window into the foreground and activates the window.</summary>
/// <param name="window">
Expand Down Expand Up @@ -142,13 +142,15 @@ public static void CenterOnScreen(this Microsoft.UI.Xaml.Window window, double?
/// Positions and resizes the window
/// </summary>
/// <param name="window">Window</param>
/// <param name="x">Left side of the window in device independent pixels</param>
/// <param name="y">Top side of the window in device independent pixels</param>
/// <param name="x">Left side of the window</param>
/// <param name="y">Top side of the window</param>
/// <param name="width">Width of the window in device independent pixels, or <c>null</c> if keeping the current size</param>
/// <param name="height">Height of the window in device independent pixels, or <c>null</c> if keeping the current size</param>
public static void MoveAndResize(this Microsoft.UI.Xaml.Window window, double x, double y, double width, double height)
=> window.GetAppWindow().MoveAndResize(new Windows.Graphics.RectInt32((int)x, (int)y, (int)width, (int)height)); // TODO: Adjust for dpi
//=> HwndExtensions.SetWindowPositionAndSize(window.GetWindowHandle(), x, y, width, height);
{
var scale = HwndExtensions.GetDpiForWindow(window.GetWindowHandle()) / 96f;
window.GetAppWindow().MoveAndResize(new Windows.Graphics.RectInt32((int)x, (int)y, (int)(width * scale), (int)(height * scale)));
}

/// <summary>
/// Sets the width and height of the window in device-independent pixels.
Expand All @@ -157,7 +159,10 @@ public static void MoveAndResize(this Microsoft.UI.Xaml.Window window, double x,
/// <param name="width">Width of the window in device-independent units.</param>
/// <param name="height">Height of the window in device-independent units.</param>
public static void SetWindowSize(this Microsoft.UI.Xaml.Window window, double width, double height)
=> window.GetAppWindow().Resize(new Windows.Graphics.SizeInt32((int)width, (int)height));
{
var scale = HwndExtensions.GetDpiForWindow(window.GetWindowHandle()) / 96f;
window.GetAppWindow().Resize(new Windows.Graphics.SizeInt32((int)(width * scale), (int)(height * scale)));
}

/// <summary>
/// Sets the window presenter kind used.
Expand Down

0 comments on commit 74b9de2

Please sign in to comment.