@@ -66,19 +66,11 @@ private void PositionWindow(Size size)
66
66
if ( WindowState == WindowState . Maximized )
67
67
return ;
68
68
69
+ size = new Size ( Math . Max ( MinWidth , size . Width ) , Math . Max ( MinHeight , size . Height ) ) ;
70
+
69
71
var newRect = IsLoaded ? ResizeAndCentreExistingWindow ( size ) : ResizeAndCentreNewWindow ( size ) ;
70
72
71
- if ( IsLoaded )
72
- {
73
- this . MoveWindow ( newRect . Left , newRect . Top , newRect . Width , newRect . Height ) ;
74
- }
75
- else
76
- {
77
- Top = newRect . Top ;
78
- Left = newRect . Left ;
79
- Width = newRect . Width ;
80
- Height = newRect . Height ;
81
- }
73
+ this . MoveWindow ( newRect . Left , newRect . Top , newRect . Width , newRect . Height ) ;
82
74
}
83
75
84
76
private Rect ResizeAndCentreExistingWindow ( Size size )
@@ -96,52 +88,60 @@ private Rect ResizeAndCentreExistingWindow(Size size)
96
88
// |LB | B |RB |10%
97
89
// |---|-----------|---|---
98
90
99
- const double limitPercentX = 0.1 ;
100
- const double limitPercentY = 0.1 ;
91
+ var scale = DpiHelper . GetScaleFactorFromWindow ( this ) ;
92
+
93
+ var limitPercentX = 0.1 * scale . Horizontal ;
94
+ var limitPercentY = 0.1 * scale . Vertical ;
101
95
102
- var oldRect = new Rect ( Left , Top , Width , Height ) ;
96
+ // use absolute pixels for calculation
97
+ var pxSize = new Size ( scale . Horizontal * size . Width , scale . Vertical * size . Height ) ;
98
+ var pxOldRect = this . GetWindowRectInPixel ( ) ;
103
99
104
100
// scale to new size, maintain centre
105
- var newRect = Rect . Inflate ( oldRect ,
106
- ( Math . Max ( MinWidth , size . Width ) - oldRect . Width ) / 2 ,
107
- ( Math . Max ( MinHeight , size . Height ) - oldRect . Height ) / 2 ) ;
101
+ var pxNewRect = Rect . Inflate ( pxOldRect ,
102
+ ( pxSize . Width - pxOldRect . Width ) / 2 ,
103
+ ( pxSize . Height - pxOldRect . Height ) / 2 ) ;
108
104
109
- var desktopRect = WindowHelper . GetDesktopRectFromWindow ( this ) ;
105
+ var desktopRect = WindowHelper . GetDesktopRectFromWindowInPixel ( this ) ;
110
106
111
107
var leftLimit = desktopRect . Left + desktopRect . Width * limitPercentX ;
112
108
var rightLimit = desktopRect . Right - desktopRect . Width * limitPercentX ;
113
109
var topLimit = desktopRect . Top + desktopRect . Height * limitPercentY ;
114
110
var bottomLimit = desktopRect . Bottom - desktopRect . Height * limitPercentY ;
115
111
116
- if ( oldRect . Left < leftLimit && oldRect . Right < rightLimit ) // L
117
- newRect . Location = new Point ( Math . Max ( oldRect . Left , desktopRect . Left ) , newRect . Top ) ;
118
- else if ( oldRect . Left > leftLimit && oldRect . Right > rightLimit ) // R
119
- newRect . Location = new Point ( Math . Min ( oldRect . Right , desktopRect . Right ) - newRect . Width , newRect . Top ) ;
112
+ if ( pxOldRect . Left < leftLimit && pxOldRect . Right < rightLimit ) // L
113
+ pxNewRect . Location = new Point ( Math . Max ( pxOldRect . Left , desktopRect . Left ) , pxNewRect . Top ) ;
114
+ else if ( pxOldRect . Left > leftLimit && pxOldRect . Right > rightLimit ) // R
115
+ pxNewRect . Location = new Point ( Math . Min ( pxOldRect . Right , desktopRect . Right ) - pxNewRect . Width , pxNewRect . Top ) ;
120
116
else // C, fix window boundary
121
- newRect . Offset (
122
- Math . Max ( 0 , desktopRect . Left - newRect . Left ) + Math . Min ( 0 , desktopRect . Right - newRect . Right ) , 0 ) ;
123
-
124
- if ( oldRect . Top < topLimit && oldRect . Bottom < bottomLimit ) // T
125
- newRect . Location = new Point ( newRect . Left , Math . Max ( oldRect . Top , desktopRect . Top ) ) ;
126
- else if ( oldRect . Top > topLimit && oldRect . Bottom > bottomLimit ) // B
127
- newRect . Location = new Point ( newRect . Left ,
128
- Math . Min ( oldRect . Bottom , desktopRect . Bottom ) - newRect . Height ) ;
117
+ pxNewRect . Offset (
118
+ Math . Max ( 0 , desktopRect . Left - pxNewRect . Left ) + Math . Min ( 0 , desktopRect . Right - pxNewRect . Right ) , 0 ) ;
119
+
120
+ if ( pxOldRect . Top < topLimit && pxOldRect . Bottom < bottomLimit ) // T
121
+ pxNewRect . Location = new Point ( pxNewRect . Left , Math . Max ( pxOldRect . Top , desktopRect . Top ) ) ;
122
+ else if ( pxOldRect . Top > topLimit && pxOldRect . Bottom > bottomLimit ) // B
123
+ pxNewRect . Location = new Point ( pxNewRect . Left ,
124
+ Math . Min ( pxOldRect . Bottom , desktopRect . Bottom ) - pxNewRect . Height ) ;
129
125
else // C, fix window boundary
130
- newRect . Offset ( 0 ,
131
- Math . Max ( 0 , desktopRect . Top - newRect . Top ) + Math . Min ( 0 , desktopRect . Bottom - newRect . Bottom ) ) ;
126
+ pxNewRect . Offset ( 0 ,
127
+ Math . Max ( 0 , desktopRect . Top - pxNewRect . Top ) + Math . Min ( 0 , desktopRect . Bottom - pxNewRect . Bottom ) ) ;
132
128
133
- return newRect ;
129
+ // return absolute location and relative size
130
+ return new Rect ( pxNewRect . Location , size ) ;
134
131
}
135
132
136
133
private Rect ResizeAndCentreNewWindow ( Size size )
137
134
{
138
- var desktopRect = WindowHelper . GetCurrentDesktopRect ( ) ;
135
+ var desktopRect = WindowHelper . GetCurrentDesktopRectInPixel ( ) ;
136
+ var scale = DpiHelper . GetCurrentScaleFactor ( ) ;
137
+ var pxSize = new Size ( scale . Horizontal * size . Width , scale . Vertical * size . Height ) ;
139
138
140
- var newRect = Rect . Inflate ( desktopRect ,
141
- ( Math . Max ( MinWidth , size . Width ) - desktopRect . Width ) / 2 ,
142
- ( Math . Max ( MinHeight , size . Height ) - desktopRect . Height ) / 2 ) ;
139
+ var pxLocation = new Point (
140
+ desktopRect . X + ( desktopRect . Width - pxSize . Width ) / 2 ,
141
+ desktopRect . Y + ( desktopRect . Height - pxSize . Height ) / 2 ) ;
143
142
144
- return newRect ;
143
+ // return absolute location and relative size
144
+ return new Rect ( pxLocation , size ) ;
145
145
}
146
146
147
147
internal void UnloadPlugin ( )
0 commit comments