@@ -112,23 +112,45 @@ public void InitializePosition()
112
112
{
113
113
if ( _settings . SettingWindowTop == null || _settings . SettingWindowLeft == null )
114
114
{
115
- Top = WindowTop ( ) ;
116
- Left = WindowLeft ( ) ;
115
+ SetWindowPosition ( WindowTop ( ) , WindowLeft ( ) ) ;
117
116
}
118
117
else
119
118
{
120
- Top = _settings . SettingWindowTop . Value ;
121
- Left = _settings . SettingWindowLeft . Value ;
119
+ double left = _settings . SettingWindowLeft . Value ;
120
+ double top = _settings . SettingWindowTop . Value ;
121
+ AdjustWindowPosition ( ref top , ref left ) ;
122
+ SetWindowPosition ( top , left ) ;
122
123
}
123
124
WindowState = _settings . SettingWindowState ;
124
125
}
125
126
127
+ private void SetWindowPosition ( double top , double left )
128
+ {
129
+ // Ensure window does not exceed screen boundaries
130
+ top = Math . Max ( top , SystemParameters . VirtualScreenTop ) ;
131
+ left = Math . Max ( left , SystemParameters . VirtualScreenLeft ) ;
132
+ top = Math . Min ( top , SystemParameters . VirtualScreenHeight - ActualHeight ) ;
133
+ left = Math . Min ( left , SystemParameters . VirtualScreenWidth - ActualWidth ) ;
134
+
135
+ Top = top ;
136
+ Left = left ;
137
+ }
138
+
139
+ private void AdjustWindowPosition ( ref double top , ref double left )
140
+ {
141
+ // Adjust window position if it exceeds screen boundaries
142
+ top = Math . Max ( top , SystemParameters . VirtualScreenTop ) ;
143
+ left = Math . Max ( left , SystemParameters . VirtualScreenLeft ) ;
144
+ top = Math . Min ( top , SystemParameters . VirtualScreenHeight - ActualHeight ) ;
145
+ left = Math . Min ( left , SystemParameters . VirtualScreenWidth - ActualWidth ) ;
146
+ }
147
+
126
148
private double WindowLeft ( )
127
149
{
128
150
var screen = Screen . FromPoint ( System . Windows . Forms . Cursor . Position ) ;
129
151
var dip1 = WindowsInteropHelper . TransformPixelsToDIP ( this , screen . WorkingArea . X , 0 ) ;
130
152
var dip2 = WindowsInteropHelper . TransformPixelsToDIP ( this , screen . WorkingArea . Width , 0 ) ;
131
- var left = ( dip2 . X - this . ActualWidth ) / 2 + dip1 . X ;
153
+ var left = ( dip2 . X - ActualWidth ) / 2 + dip1 . X ;
132
154
return left ;
133
155
}
134
156
@@ -137,7 +159,7 @@ private double WindowTop()
137
159
var screen = Screen . FromPoint ( System . Windows . Forms . Cursor . Position ) ;
138
160
var dip1 = WindowsInteropHelper . TransformPixelsToDIP ( this , 0 , screen . WorkingArea . Y ) ;
139
161
var dip2 = WindowsInteropHelper . TransformPixelsToDIP ( this , 0 , screen . WorkingArea . Height ) ;
140
- var top = ( dip2 . Y - this . ActualHeight ) / 2 + dip1 . Y - 20 ;
162
+ var top = ( dip2 . Y - ActualHeight ) / 2 + dip1 . Y ;
141
163
return top ;
142
164
}
143
165
0 commit comments