@@ -125,7 +125,7 @@ public override void OnApplyTemplate()
125125 public static extern bool GetMonitorInfo ( IntPtr hMonitor , ref MONITORINFO lpmi ) ;
126126
127127 [ DllImport ( "shcore.dll" ) ]
128- public static extern int GetDpiForMonitor ( IntPtr hMonitor , int dpiType , out uint dpiX , out uint dpiY ) ;
128+ public static extern int GetDpiForMonitor ( IntPtr hMonitor , MONITOR_DPI_TYPE dpiType , out uint dpiX , out uint dpiY ) ;
129129
130130 [ DllImport ( "user32.dll" , SetLastError = true ) ]
131131 internal static extern bool MoveWindow ( IntPtr hWnd , int X , int Y , int nWidth , int nHeight , bool bRepaint ) ;
@@ -140,6 +140,14 @@ public struct MONITORINFO
140140 public int dwFlags ;
141141 }
142142
143+ public enum MONITOR_DPI_TYPE : int
144+ {
145+ MDT_EFFECTIVE_DPI = 0 ,
146+ MDT_ANGULAR_DPI ,
147+ MDT_RAW_DPI ,
148+ MDT_DEFAULT
149+ }
150+
143151 [ StructLayout ( LayoutKind . Sequential ) ]
144152 public struct RECT
145153 {
@@ -364,8 +372,12 @@ protected static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lPar
364372 var monitorRectangle = monitorInfo . rcMonitor ;
365373
366374 var win = ( Window ) HwndSource . FromHwnd ( hwnd ) . RootVisual ;
367- var maxWidth = ( int ) Math . Min ( Math . Abs ( workingRectangle . right - monitorRectangle . left ) , win . MaxWidth ) ;
368- var maxHeight = ( int ) Math . Min ( Math . Abs ( workingRectangle . bottom - monitorRectangle . top ) , win . MaxHeight ) ;
375+ GetDpiForMonitor ( hMonitor , MONITOR_DPI_TYPE . MDT_EFFECTIVE_DPI , out var dpiX , out var dpiY ) ;
376+ var windowMaxWidth = win . MaxWidth / 96.0 * dpiX ;
377+ var windowMaxHeight = win . MaxHeight / 96.0 * dpiY ;
378+
379+ var maxWidth = ( int ) Math . Min ( Math . Abs ( workingRectangle . right - monitorRectangle . left ) , windowMaxWidth ) ;
380+ var maxHeight = ( int ) Math . Min ( Math . Abs ( workingRectangle . bottom - monitorRectangle . top ) , windowMaxHeight ) ;
369381
370382 info . ptMaxPosition . x = Math . Abs ( workingRectangle . left - monitorRectangle . left ) ;
371383 info . ptMaxPosition . y = Math . Abs ( workingRectangle . top - monitorRectangle . top ) ;
@@ -416,8 +428,13 @@ protected static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lPar
416428 pos . y = cur . y - 8 ;
417429 }
418430
419- var maxWidth = ( int ) Math . Min ( pos . cx , win . MaxWidth ) ;
420- var maxHeight = ( int ) Math . Min ( pos . cy , win . MaxHeight ) ;
431+ var hMonitor = MonitorFromWindow ( hwnd , MONITOR_DEFAULTTONEAREST ) ;
432+ GetDpiForMonitor ( hMonitor , MONITOR_DPI_TYPE . MDT_EFFECTIVE_DPI , out var dpiX , out var dpiY ) ;
433+ var windowMaxWidth = win . MaxWidth / 96.0 * dpiX ;
434+ var windowMaxHeight = win . MaxHeight / 96.0 * dpiY ;
435+
436+ var maxWidth = ( int ) Math . Min ( pos . cx , windowMaxWidth ) ;
437+ var maxHeight = ( int ) Math . Min ( pos . cy , windowMaxHeight ) ;
421438 pos . cx = maxWidth ;
422439 pos . cy = maxHeight ;
423440
0 commit comments