Skip to content

Commit 8839056

Browse files
committed
Fixes #37
1 parent e95b945 commit 8839056

3 files changed

Lines changed: 10 additions & 22 deletions

File tree

src/Directory.Build.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<Authors>Morten Nielsen - https://xaml.dev</Authors>
1313
<Company>Morten Nielsen - https://xaml.dev</Company>
1414
<PackageIcon>logo.png</PackageIcon>
15-
<Version>1.4.0</Version>
15+
<Version>1.4.1</Version>
1616
<WinAppSDKVersion>1.0.1</WinAppSDKVersion>
1717
<DefineConstants Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(WinAppSDKVersion)','1.1-a'))">$(DefineConstants);WINAPPSDK_1_1_OR_NEWER</DefineConstants>
1818
</PropertyGroup>

src/WinUIEx/WinUIEx.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<PackageId>WinUIEx</PackageId>
1919
<Product>WinUI Extensions</Product>
2020
<PackageReleaseNotes>
21-
- Added WebAuthenticator API to simplify OAuth workflows with your external browser
21+
- Addresses an issue with window resizing when moving across monitors with different scaling (Issue #37).
2222
</PackageReleaseNotes>
2323
</PropertyGroup>
2424

src/WinUIEx/WindowEx.cs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public partial class WindowEx : Window
2626
private readonly ContentControl windowArea;
2727
private readonly WindowMessageMonitor mon;
2828
private readonly Microsoft.UI.Windowing.OverlappedPresenter overlappedPresenter;
29-
private uint currentDpi;
3029

3130
/// <summary>
3231
/// Initializes a new instance of the <see cref="WindowEx"/> class.
@@ -35,8 +34,7 @@ public WindowEx()
3534
{
3635
overlappedPresenter = Microsoft.UI.Windowing.OverlappedPresenter.Create();
3736
AppWindow.SetPresenter(overlappedPresenter);
38-
currentDpi = this.GetDpiForWindow();
39-
37+
4038
var rootContent = new Grid();
4139
rootContent.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto), MinHeight = 0 });
4240
rootContent.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
@@ -77,32 +75,22 @@ private unsafe void OnWindowMessage(object? sender, Messaging.WindowMessageEvent
7775
{
7876
// Restrict min-size
7977
MINMAXINFO* rect2 = (MINMAXINFO*)e.Message.LParam;
78+
var currentDpi = this.GetDpiForWindow();
8079
rect2->ptMinTrackSize.x = (int)(Math.Max(MinWidth * (currentDpi / 96f), rect2->ptMinTrackSize.x));
8180
rect2->ptMinTrackSize.y = (int)(Math.Max(MinHeight * (currentDpi / 96f), rect2->ptMinTrackSize.y));
8281
}
8382
break;
8483
case WindowsMessages.WM_DPICHANGED:
8584
{
86-
var newDpi = this.GetDpiForWindow();
87-
if (newDpi != currentDpi)
88-
{
89-
var oldDpi = currentDpi;
90-
currentDpi = newDpi;
91-
OnDpiChanged(oldDpi, newDpi);
92-
}
85+
// Resize to account for DPI change
86+
var suggestedRect = (Windows.Win32.Foundation.RECT*)e.Message.LParam;
87+
bool result = Windows.Win32.PInvoke.SetWindowPos(new Windows.Win32.Foundation.HWND(this.GetWindowHandle()), new Windows.Win32.Foundation.HWND(), suggestedRect->left, suggestedRect->top,
88+
suggestedRect->right - suggestedRect->left, suggestedRect->bottom - suggestedRect->top, Windows.Win32.UI.WindowsAndMessaging.SET_WINDOW_POS_FLAGS.SWP_NOZORDER | Windows.Win32.UI.WindowsAndMessaging.SET_WINDOW_POS_FLAGS.SWP_NOACTIVATE);
9389
break;
9490
}
9591
}
9692
}
9793

98-
private void OnDpiChanged(uint oldDpi, uint newDpi)
99-
{
100-
var oldScale = oldDpi / 96f;
101-
var currentSize = AppWindow.Size;
102-
this.SetWindowSize((int)(currentSize.Width / oldScale), (int)(currentSize.Height / oldScale));
103-
currentDpi = newDpi;
104-
}
105-
10694
private struct MINMAXINFO
10795
{
10896
#pragma warning disable CS0649
@@ -408,7 +396,7 @@ public Microsoft.UI.Windowing.AppWindowPresenterKind PresenterKind
408396
/// </summary>
409397
public double Width
410398
{
411-
get { return AppWindow.Size.Width / (currentDpi / 96d); }
399+
get { return AppWindow.Size.Width / (this.GetDpiForWindow() / 96d); }
412400
set
413401
{
414402
this.SetWindowSize(value, Height);
@@ -420,7 +408,7 @@ public double Width
420408
/// </summary>
421409
public double Height
422410
{
423-
get { return AppWindow.Size.Height / (currentDpi / 96d); }
411+
get { return AppWindow.Size.Height / (this.GetDpiForWindow() / 96d); }
424412
set
425413
{
426414
this.SetWindowSize(Width, value);

0 commit comments

Comments
 (0)