From 2be653eaa5e6073e2a9cf72955a418f4a9147dd7 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 29 Oct 2026 19:34:14 +0000 Subject: [PATCH 1/2] Fit the main window to the monitor's work area at startup WPF sizes are DPI-scaled, so the nominal 1600x1000 window overflowed the screen at high display scaling (eg. 2560x1440 at 150% is ~1707x920 units), hiding the titlebar. On startup, shrink the window (and its minimum size, if needed) to fit the work area of its monitor, and center it there. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KaEsnLMDyw72GZ8a6ZxAGq --- src/BmsConfig/MainWindow.xaml | 1 - src/BmsConfig/MainWindow.xaml.cs | 6 ++- src/BmsConfig/WindowFitting.cs | 88 ++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/BmsConfig/WindowFitting.cs diff --git a/src/BmsConfig/MainWindow.xaml b/src/BmsConfig/MainWindow.xaml index d53e372..0f674e7 100644 --- a/src/BmsConfig/MainWindow.xaml +++ b/src/BmsConfig/MainWindow.xaml @@ -3,7 +3,6 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Falcon BMS Config Editor" Width="1600" Height="1000" MinWidth="1000" MinHeight="600" - WindowStartupLocation="CenterScreen" Icon="Config.ico" FontFamily="{StaticResource UiFont}" FontSize="14" Background="{DynamicResource WindowBackground}" diff --git a/src/BmsConfig/MainWindow.xaml.cs b/src/BmsConfig/MainWindow.xaml.cs index b0746a9..bf1653a 100644 --- a/src/BmsConfig/MainWindow.xaml.cs +++ b/src/BmsConfig/MainWindow.xaml.cs @@ -56,7 +56,11 @@ public MainWindow() CategoryCombo.SelectedIndex = 0; _suppressEvents = false; - SourceInitialized += (s, e) => ThemeManager.ApplyTitleBar(this); + SourceInitialized += (s, e) => + { + ThemeManager.ApplyTitleBar(this); + WindowFitting.FitToWorkArea(this); // also centers the window + }; Loaded += OnLoaded; PreviewKeyDown += OnWindowPreviewKeyDown; Drop += OnDrop; diff --git a/src/BmsConfig/WindowFitting.cs b/src/BmsConfig/WindowFitting.cs new file mode 100644 index 0000000..6a74410 --- /dev/null +++ b/src/BmsConfig/WindowFitting.cs @@ -0,0 +1,88 @@ +using System; +using System.Runtime.InteropServices; +using System.Windows; +using System.Windows.Interop; +using System.Windows.Media; + +namespace BmsConfig +{ + /// + /// Keeps the window's startup size within the monitor's work area. WPF sizes are in 1/96" units, so the + /// nominal 1600x1000 window is scaled up by the display's DPI setting -- eg. on 2560x1440 at 150% the work + /// area is only ~1707x920 units, and the titlebar would be off-screen. + /// + static class WindowFitting + { + /// Fraction of the work area the window may take up, at most, when it opens. + const double MaxFraction = 0.92; + + /// + /// Shrinks the window (and its minimum size, if need be) to fit the work area of the monitor it's on, and + /// centers it there. Call from SourceInitialized, ie. once the window has a handle but before it's shown. + /// + public static void FitToWorkArea(Window window) + { + Rect work = GetWorkArea(window); + if (work.IsEmpty || work.Width <= 0 || work.Height <= 0) + return; + + window.MinWidth = Math.Min(window.MinWidth, work.Width); + window.MinHeight = Math.Min(window.MinHeight, work.Height); + window.Width = Math.Max(window.MinWidth, Math.Min(window.Width, work.Width * MaxFraction)); + window.Height = Math.Max(window.MinHeight, Math.Min(window.Height, work.Height * MaxFraction)); + + window.Left = work.Left + (work.Width - window.Width) / 2; + window.Top = work.Top + (work.Height - window.Height) / 2; + } + + /// The work area (screen minus taskbar) of the window's monitor, in WPF units. + static Rect GetWorkArea(Window window) + { + try + { + IntPtr hwnd = new WindowInteropHelper(window).Handle; + IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); + MONITORINFO info = new MONITORINFO { cbSize = Marshal.SizeOf(typeof(MONITORINFO)) }; + + if (monitor != IntPtr.Zero && GetMonitorInfo(monitor, ref info)) + { + // Pixels -> WPF units, at this monitor's DPI. + DpiScale dpi = VisualTreeHelper.GetDpi(window); + return new Rect( + info.rcWork.Left / dpi.DpiScaleX, + info.rcWork.Top / dpi.DpiScaleY, + (info.rcWork.Right - info.rcWork.Left) / dpi.DpiScaleX, + (info.rcWork.Bottom - info.rcWork.Top) / dpi.DpiScaleY); + } + } + catch (Exception) + { + // Fall through to the primary monitor's work area. + } + return SystemParameters.WorkArea; + } + + const int MONITOR_DEFAULTTONEAREST = 2; + + [StructLayout(LayoutKind.Sequential)] + struct RECT + { + public int Left, Top, Right, Bottom; + } + + [StructLayout(LayoutKind.Sequential)] + struct MONITORINFO + { + public int cbSize; + public RECT rcMonitor; + public RECT rcWork; + public int dwFlags; + } + + [DllImport("user32.dll")] + static extern IntPtr MonitorFromWindow(IntPtr hwnd, int flags); + + [DllImport("user32.dll")] + static extern bool GetMonitorInfo(IntPtr monitor, ref MONITORINFO info); + } +} From 2c1a8b58e4396a9ad97e4ffb965ba66efcb276c5 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 29 Oct 2026 20:09:59 +0000 Subject: [PATCH 2/2] Fit window using SystemParameters.WorkArea, without p/invoke Drop the MonitorFromWindow/GetMonitorInfo interop in favour of pure WPF, to keep the windowing code portable. Size and position are now set in the constructor, before the window is created. Caps the startup size at 80% of the work area. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01KaEsnLMDyw72GZ8a6ZxAGq --- src/BmsConfig/MainWindow.xaml.cs | 8 ++-- src/BmsConfig/WindowFitting.cs | 68 +++++--------------------------- 2 files changed, 13 insertions(+), 63 deletions(-) diff --git a/src/BmsConfig/MainWindow.xaml.cs b/src/BmsConfig/MainWindow.xaml.cs index bf1653a..ec019ec 100644 --- a/src/BmsConfig/MainWindow.xaml.cs +++ b/src/BmsConfig/MainWindow.xaml.cs @@ -56,11 +56,9 @@ public MainWindow() CategoryCombo.SelectedIndex = 0; _suppressEvents = false; - SourceInitialized += (s, e) => - { - ThemeManager.ApplyTitleBar(this); - WindowFitting.FitToWorkArea(this); // also centers the window - }; + WindowFitting.FitToWorkArea(this); // also centers the window + + SourceInitialized += (s, e) => ThemeManager.ApplyTitleBar(this); Loaded += OnLoaded; PreviewKeyDown += OnWindowPreviewKeyDown; Drop += OnDrop; diff --git a/src/BmsConfig/WindowFitting.cs b/src/BmsConfig/WindowFitting.cs index 6a74410..82fb3fd 100644 --- a/src/BmsConfig/WindowFitting.cs +++ b/src/BmsConfig/WindowFitting.cs @@ -1,28 +1,29 @@ using System; -using System.Runtime.InteropServices; using System.Windows; -using System.Windows.Interop; -using System.Windows.Media; namespace BmsConfig { /// - /// Keeps the window's startup size within the monitor's work area. WPF sizes are in 1/96" units, so the + /// Keeps the window's startup size within the screen's work area. WPF sizes are in 1/96" units, so the /// nominal 1600x1000 window is scaled up by the display's DPI setting -- eg. on 2560x1440 at 150% the work /// area is only ~1707x920 units, and the titlebar would be off-screen. /// + /// + /// Deliberately pure WPF (no p/invoke), for portability. SystemParameters.WorkArea describes the primary + /// monitor, which is where new windows open by default. + /// static class WindowFitting { /// Fraction of the work area the window may take up, at most, when it opens. - const double MaxFraction = 0.92; + const double MaxFraction = 0.8; /// - /// Shrinks the window (and its minimum size, if need be) to fit the work area of the monitor it's on, and - /// centers it there. Call from SourceInitialized, ie. once the window has a handle but before it's shown. + /// Shrinks the window (and its minimum size, if need be) to fit the work area, and centers it there. + /// Call before the window is shown, eg. from its constructor. /// public static void FitToWorkArea(Window window) { - Rect work = GetWorkArea(window); + Rect work = SystemParameters.WorkArea; if (work.IsEmpty || work.Width <= 0 || work.Height <= 0) return; @@ -31,58 +32,9 @@ public static void FitToWorkArea(Window window) window.Width = Math.Max(window.MinWidth, Math.Min(window.Width, work.Width * MaxFraction)); window.Height = Math.Max(window.MinHeight, Math.Min(window.Height, work.Height * MaxFraction)); + window.WindowStartupLocation = WindowStartupLocation.Manual; window.Left = work.Left + (work.Width - window.Width) / 2; window.Top = work.Top + (work.Height - window.Height) / 2; } - - /// The work area (screen minus taskbar) of the window's monitor, in WPF units. - static Rect GetWorkArea(Window window) - { - try - { - IntPtr hwnd = new WindowInteropHelper(window).Handle; - IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); - MONITORINFO info = new MONITORINFO { cbSize = Marshal.SizeOf(typeof(MONITORINFO)) }; - - if (monitor != IntPtr.Zero && GetMonitorInfo(monitor, ref info)) - { - // Pixels -> WPF units, at this monitor's DPI. - DpiScale dpi = VisualTreeHelper.GetDpi(window); - return new Rect( - info.rcWork.Left / dpi.DpiScaleX, - info.rcWork.Top / dpi.DpiScaleY, - (info.rcWork.Right - info.rcWork.Left) / dpi.DpiScaleX, - (info.rcWork.Bottom - info.rcWork.Top) / dpi.DpiScaleY); - } - } - catch (Exception) - { - // Fall through to the primary monitor's work area. - } - return SystemParameters.WorkArea; - } - - const int MONITOR_DEFAULTTONEAREST = 2; - - [StructLayout(LayoutKind.Sequential)] - struct RECT - { - public int Left, Top, Right, Bottom; - } - - [StructLayout(LayoutKind.Sequential)] - struct MONITORINFO - { - public int cbSize; - public RECT rcMonitor; - public RECT rcWork; - public int dwFlags; - } - - [DllImport("user32.dll")] - static extern IntPtr MonitorFromWindow(IntPtr hwnd, int flags); - - [DllImport("user32.dll")] - static extern bool GetMonitorInfo(IntPtr monitor, ref MONITORINFO info); } }