@@ -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