using System; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; using System.Windows.Media; namespace ClassNotify.Client.Services { public static class DisplayBoundsService { private const uint MONITOR_DEFAULTTONEAREST = 2; private const uint MONITOR_DEFAULTTOPRIMARY = 1; [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; public int Width => Right - Left; public int Height => Bottom - Top; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public struct MONITORINFO { public int cbSize; public RECT rcMonitor; public RECT rcWork; public uint dwFlags; } [DllImport("user32.dll")] private static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags); [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi); public struct WindowBoundsDIP { public double Left; public double Top; public double Width; public double Height; public override string ToString() => $"Left={Left:F1}, Top={Top:F1}, Width={Width:F1}, Height={Height:F1}"; } /// /// Retrieves the exact monitor bounds in WPF DIPs (Device Independent Pixels) /// for the specified window, guaranteeing 100% monitor coverage without DPI overflow. /// public static WindowBoundsDIP GetMonitorBoundsDIP(Window window) { try { var hwnd = new WindowInteropHelper(window).Handle; IntPtr hMonitor = IntPtr.Zero; if (hwnd != IntPtr.Zero) { hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); } MONITORINFO mi = new MONITORINFO(); mi.cbSize = Marshal.SizeOf(typeof(MONITORINFO)); if (hMonitor != IntPtr.Zero && GetMonitorInfo(hMonitor, ref mi)) { var dpi = VisualTreeHelper.GetDpi(window); double scaleX = dpi.DpiScaleX > 0 ? dpi.DpiScaleX : 1.0; double scaleY = dpi.DpiScaleY > 0 ? dpi.DpiScaleY : 1.0; return new WindowBoundsDIP { Left = mi.rcMonitor.Left / scaleX, Top = mi.rcMonitor.Top / scaleY, Width = mi.rcMonitor.Width / scaleX, Height = mi.rcMonitor.Height / scaleY }; } } catch { // Fallback on any Win32 or visual tree interop error } // Fallback to PrimaryScreenWidth / PrimaryScreenHeight if monitor info fails double w = SystemParameters.PrimaryScreenWidth > 0 ? SystemParameters.PrimaryScreenWidth : 1920; double h = SystemParameters.PrimaryScreenHeight > 0 ? SystemParameters.PrimaryScreenHeight : 1080; return new WindowBoundsDIP { Left = 0, Top = 0, Width = w, Height = h }; } /// /// Applies deterministic borderless fullscreen bounds to the window. /// Eliminates WPF layered window Maximized bugs and DPI misalignments. /// public static void ApplyFullscreenBounds(Window window) { window.WindowState = WindowState.Normal; window.WindowStartupLocation = WindowStartupLocation.Manual; window.WindowStyle = WindowStyle.None; window.ResizeMode = ResizeMode.NoResize; var bounds = GetMonitorBoundsDIP(window); window.Left = bounds.Left; window.Top = bounds.Top; window.Width = bounds.Width; window.Height = bounds.Height; } } }