@@ -293,39 +293,31 @@ private void InitializePosition()
293
293
{
294
294
if ( _settings . SearchWindowScreen == SearchWindowScreens . RememberLastLaunchLocation )
295
295
{
296
- // Get the previous screen width, height, DPI X, and DPI Y
297
296
double previousScreenWidth = _settings . PreviousScreenWidth ;
298
297
double previousScreenHeight = _settings . PreviousScreenHeight ;
299
- double previousDpiX = _settings . PreviousDpiX ;
300
- double previousDpiY = _settings . PreviousDpiY ;
298
+ double previousDpiX , previousDpiY ;
299
+ GetDpi ( out previousDpiX , out previousDpiY ) ;
301
300
302
- // Save current screen width, height, DPI X, and DPI Y
303
301
_settings . PreviousScreenWidth = SystemParameters . VirtualScreenWidth ;
304
302
_settings . PreviousScreenHeight = SystemParameters . VirtualScreenHeight ;
305
- _settings . PreviousDpiX = GetDpiX ( ) ;
306
- _settings . PreviousDpiY = GetDpiY ( ) ;
303
+ double currentDpiX , currentDpiY ;
304
+ GetDpi ( out currentDpiX , out currentDpiY ) ;
307
305
308
- // If previous screen width, height, DPI X, or DPI Y are not zero
309
306
if ( previousScreenWidth != 0 && previousScreenHeight != 0 &&
310
- previousDpiX != 0 && previousDpiY != 0 )
307
+ previousDpiX != 0 && previousDpiY != 0 &&
308
+ ( previousScreenWidth != SystemParameters . VirtualScreenWidth ||
309
+ previousScreenHeight != SystemParameters . VirtualScreenHeight ||
310
+ previousDpiX != currentDpiX || previousDpiY != currentDpiY ) )
311
311
{
312
- // If previous and current screen properties are different, adjust position
313
- if ( previousScreenWidth != SystemParameters . VirtualScreenWidth ||
314
- previousScreenHeight != SystemParameters . VirtualScreenHeight ||
315
- previousDpiX != GetDpiX ( ) || previousDpiY != GetDpiY ( ) )
316
- {
317
- AdjustPositionForResolutionChange ( ) ;
318
- return ;
319
- }
312
+ AdjustPositionForResolutionChange ( ) ;
313
+ return ;
320
314
}
321
315
322
- // If previous screen width, height, DPI X, and DPI Y are the same, initialize with previous position
323
316
Left = _settings . WindowLeft ;
324
317
Top = _settings . WindowTop ;
325
318
}
326
319
else
327
320
{
328
- // Keep the existing logic
329
321
var screen = SelectedScreen ( ) ;
330
322
switch ( _settings . SearchWindowAlign )
331
323
{
@@ -346,8 +338,10 @@ private void InitializePosition()
346
338
Top = VerticalTop ( screen ) ;
347
339
break ;
348
340
case SearchWindowAligns . Custom :
349
- Left = WindowsInteropHelper . TransformPixelsToDIP ( this , screen . WorkingArea . X + _settings . CustomWindowLeft , 0 ) . X ;
350
- Top = WindowsInteropHelper . TransformPixelsToDIP ( this , 0 , screen . WorkingArea . Y + _settings . CustomWindowTop ) . Y ;
341
+ var customLeft = WindowsInteropHelper . TransformPixelsToDIP ( this , screen . WorkingArea . X + _settings . CustomWindowLeft , 0 ) ;
342
+ var customTop = WindowsInteropHelper . TransformPixelsToDIP ( this , 0 , screen . WorkingArea . Y + _settings . CustomWindowTop ) ;
343
+ Left = customLeft . X ;
344
+ Top = customTop . Y ;
351
345
break ;
352
346
}
353
347
}
@@ -357,83 +351,46 @@ private void AdjustPositionForResolutionChange()
357
351
{
358
352
double screenWidth = SystemParameters . VirtualScreenWidth ;
359
353
double screenHeight = SystemParameters . VirtualScreenHeight ;
360
- double screenLeft = SystemParameters . VirtualScreenLeft ;
361
- double screenTop = SystemParameters . VirtualScreenTop ;
362
- double currentDpiX = GetDpiX ( ) ;
363
- double currentDpiY = GetDpiY ( ) ;
364
-
365
- // Get current screen width, height, left, top, and DPI X, DPI Y
366
- double currentScreenWidth = screenWidth ;
367
- double currentScreenHeight = screenHeight ;
368
- double currentScreenLeft = screenLeft ;
369
- double currentScreenTop = screenTop ;
354
+ double currentDpiX , currentDpiY ;
355
+ GetDpi ( out currentDpiX , out currentDpiY ) ;
370
356
371
- // Get previous window left, top, and DPI X, DPI Y
372
357
double previousLeft = _settings . WindowLeft ;
373
358
double previousTop = _settings . WindowTop ;
374
- double previousDpiX = _settings . PreviousDpiX ;
375
- double previousDpiY = _settings . PreviousDpiY ;
359
+ double previousDpiX , previousDpiY ;
360
+ GetDpi ( out previousDpiX , out previousDpiY ) ;
376
361
377
- // Calculate ratios for width, height, DPI X, and DPI Y
378
- double widthRatio = currentScreenWidth / _settings . PreviousScreenWidth ;
379
- double heightRatio = currentScreenHeight / _settings . PreviousScreenHeight ;
362
+ double widthRatio = screenWidth / _settings . PreviousScreenWidth ;
363
+ double heightRatio = screenHeight / _settings . PreviousScreenHeight ;
380
364
double dpiXRatio = currentDpiX / previousDpiX ;
381
365
double dpiYRatio = currentDpiY / previousDpiY ;
382
366
383
- // Adjust previous position according to current resolution and DPI
384
367
double newLeft = previousLeft * widthRatio * dpiXRatio ;
385
368
double newTop = previousTop * heightRatio * dpiYRatio ;
386
369
387
- // Ensure the adjusted position is within the current screen boundaries
388
- if ( newLeft + ActualWidth > currentScreenLeft + currentScreenWidth )
389
- {
390
- newLeft = currentScreenLeft + currentScreenWidth - ActualWidth ;
391
- }
392
- else if ( newLeft < currentScreenLeft )
393
- {
394
- newLeft = currentScreenLeft ;
395
- }
370
+ double screenLeft = SystemParameters . VirtualScreenLeft ;
371
+ double screenTop = SystemParameters . VirtualScreenTop ;
396
372
397
- if ( newTop + ActualHeight > currentScreenTop + currentScreenHeight )
398
- {
399
- newTop = currentScreenTop + currentScreenHeight - ActualHeight ;
400
- }
401
- else if ( newTop < currentScreenTop )
402
- {
403
- newTop = currentScreenTop ;
404
- }
373
+ double maxX = screenLeft + screenWidth - ActualWidth ;
374
+ double maxY = screenTop + screenHeight - ActualHeight ;
405
375
406
- // Set the new position
407
- Left = newLeft ;
408
- Top = newTop ;
376
+ Left = Math . Max ( screenLeft , Math . Min ( newLeft , maxX ) ) ;
377
+ Top = Math . Max ( screenTop , Math . Min ( newTop , maxY ) ) ;
409
378
}
410
379
411
- private double GetDpiX ( )
380
+ private void GetDpi ( out double dpiX , out double dpiY )
412
381
{
413
- // Get the PresentationSource for this visual
414
382
PresentationSource source = PresentationSource . FromVisual ( this ) ;
415
- // Check if the PresentationSource and its CompositionTarget are not null
416
383
if ( source != null && source . CompositionTarget != null )
417
384
{
418
- // Get the transform matrix for the CompositionTarget
419
385
Matrix m = source . CompositionTarget . TransformToDevice ;
420
- // Calculate DPI in X direction
421
- double dpiX = 96 * m . M11 ;
422
- return dpiX ;
386
+ dpiX = 96 * m . M11 ;
387
+ dpiY = 96 * m . M22 ;
423
388
}
424
- return 96 ; //Return a default DPI of 96 if PresentationSource or CompositionTarget is null
425
- }
426
-
427
- private double GetDpiY ( )
428
- {
429
- PresentationSource source = PresentationSource . FromVisual ( this ) ;
430
- if ( source != null && source . CompositionTarget != null )
389
+ else
431
390
{
432
- Matrix m = source . CompositionTarget . TransformToDevice ;
433
- double dpiY = 96 * m . M22 ;
434
- return dpiY ;
391
+ dpiX = 96 ;
392
+ dpiY = 96 ;
435
393
}
436
- return 96 ;
437
394
}
438
395
private void UpdateNotifyIconText ( )
439
396
{
0 commit comments