From 3cd7224182e1680b373111d28e44c8d521db38f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6ssler?= Date: Tue, 10 Sep 2024 19:29:46 +0200 Subject: [PATCH] Fix window too big using high screen scaling --- Guard.WPF/MainWindow.xaml.cs | 44 ++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/Guard.WPF/MainWindow.xaml.cs b/Guard.WPF/MainWindow.xaml.cs index a94cde8..23bc9d6 100644 --- a/Guard.WPF/MainWindow.xaml.cs +++ b/Guard.WPF/MainWindow.xaml.cs @@ -74,6 +74,13 @@ private void OnWindowLoaded() SimpleIconsManager.LoadIcons(); + CheckWindowSizeAndPosition(); + + if (SettingsManager.Settings.MinimizeToTray) + { + AddTrayIcon(); + } + StatsClient = new( "A-SH-2619747927", new AptabaseOptions { Host = "https://aptabase.tkoessler.de" } @@ -93,11 +100,6 @@ private void OnWindowLoaded() } CheckLocalTime(); - - if (SettingsManager.Settings.MinimizeToTray) - { - AddTrayIcon(); - } } internal void ApplyTheme(ThemeSetting theme) @@ -412,5 +414,37 @@ internal IntPtr GetWindowHandle() { return windowInteropHandle; } + + private void CheckWindowSizeAndPosition() + { + try + { + if (Left < 0) + { + Left = 0; + } + if (Top < 0) + { + Top = 0; + } + int screenWidth = (int)SystemParameters.WorkArea.Width; + int screenHeight = (int)SystemParameters.WorkArea.Height; + if (Left + Width > screenWidth) + { + Width = screenWidth - Left; + } + if (Top + Height > screenHeight) + { + Height = screenHeight - Top; + } + } + catch (Exception e) + { + Log.Logger.Warning( + "Failed to check window size and position: {Exception}", + e.Message + ); + } + } } }