From 2d48ef66b1161b1da16c3658829580bc50f9c47c Mon Sep 17 00:00:00 2001 From: Xinnony Date: Sun, 2 Jan 2022 00:59:41 +0100 Subject: [PATCH 1/3] Fix crash on windows 7 () shcore.dll not exist on windows 7 --- FluentWPF/AcrylicWindow.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/FluentWPF/AcrylicWindow.cs b/FluentWPF/AcrylicWindow.cs index 768577f..5fb4e32 100644 --- a/FluentWPF/AcrylicWindow.cs +++ b/FluentWPF/AcrylicWindow.cs @@ -372,7 +372,19 @@ protected static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lPar var monitorRectangle = monitorInfo.rcMonitor; var win = (Window)HwndSource.FromHwnd(hwnd).RootVisual; - GetDpiForMonitor(hMonitor, MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out var dpiX, out var dpiY); + if (Environment.OSVersion.Version.Major >= 10 || (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 3)) + { + GetDpiForMonitor(hMonitor, MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out var dpiX, out var dpiY); + } + else + { + // must be an old version of Windows (7 or 8) + // an old version of Windows (7 or 8) + // Get scale of main window and assume scale is the same for all monitors + var dpiScale = VisualTreeHelper.GetDpi(Application.Current.MainWindow); + dpiX = dpiScale.DpiScaleX; + dpiY = dpiScale.DpiScaleY; + } var maxWidth = win.MaxWidth / 96.0 * dpiX; var maxHeight = win.MaxHeight / 96.0 * dpiY; var minWidth = win.MinWidth / 96.0 * dpiX; From 81bd4b594ca63a11db88f49f0bf33026d375cd22 Mon Sep 17 00:00:00 2001 From: Xinnony Date: Sun, 2 Jan 2022 02:13:54 +0100 Subject: [PATCH 2/3] Fix for 4.5 net framework --- FluentWPF/AcrylicWindow.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/FluentWPF/AcrylicWindow.cs b/FluentWPF/AcrylicWindow.cs index 5fb4e32..1146a4d 100644 --- a/FluentWPF/AcrylicWindow.cs +++ b/FluentWPF/AcrylicWindow.cs @@ -372,18 +372,20 @@ protected static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lPar var monitorRectangle = monitorInfo.rcMonitor; var win = (Window)HwndSource.FromHwnd(hwnd).RootVisual; + uint dpiX, dpiY; if (Environment.OSVersion.Version.Major >= 10 || (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 3)) { - GetDpiForMonitor(hMonitor, MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out var dpiX, out var dpiY); + GetDpiForMonitor(hMonitor, MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out dpiX, out dpiY); } else { // must be an old version of Windows (7 or 8) // an old version of Windows (7 or 8) // Get scale of main window and assume scale is the same for all monitors - var dpiScale = VisualTreeHelper.GetDpi(Application.Current.MainWindow); - dpiX = dpiScale.DpiScaleX; - dpiY = dpiScale.DpiScaleY; + var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static); + var dpiYProperty = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static); + dpiX = (uint)dpiXProperty.GetValue(null, null); + dpiY = (uint)dpiYProperty.GetValue(null, null); } var maxWidth = win.MaxWidth / 96.0 * dpiX; var maxHeight = win.MaxHeight / 96.0 * dpiY; From df2497146bd8576fdc94ea766c26c05b46f81ef5 Mon Sep 17 00:00:00 2001 From: Xinnony Date: Sun, 2 Jan 2022 02:26:05 +0100 Subject: [PATCH 3/3] Update AcrylicWindow.cs --- FluentWPF/AcrylicWindow.cs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/FluentWPF/AcrylicWindow.cs b/FluentWPF/AcrylicWindow.cs index 1146a4d..68090f8 100644 --- a/FluentWPF/AcrylicWindow.cs +++ b/FluentWPF/AcrylicWindow.cs @@ -372,7 +372,7 @@ protected static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lPar var monitorRectangle = monitorInfo.rcMonitor; var win = (Window)HwndSource.FromHwnd(hwnd).RootVisual; - uint dpiX, dpiY; + uint dpiX = 96, dpiY = 96; if (Environment.OSVersion.Version.Major >= 10 || (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 3)) { GetDpiForMonitor(hMonitor, MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out dpiX, out dpiY); @@ -382,10 +382,23 @@ protected static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lPar // must be an old version of Windows (7 or 8) // an old version of Windows (7 or 8) // Get scale of main window and assume scale is the same for all monitors - var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static); - var dpiYProperty = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static); - dpiX = (uint)dpiXProperty.GetValue(null, null); - dpiY = (uint)dpiYProperty.GetValue(null, null); + using (ManagementClass mc = new ManagementClass("Win32_DesktopMonitor")) + { + using (ManagementObjectCollection moc = mc.GetInstances()) + { + + uint PixelsPerXLogicalInch = 0; // dpi for x + uint PixelsPerYLogicalInch = 0; // dpi for y + + foreach (ManagementObject each in moc) + { + PixelsPerXLogicalInch = uint.Parse((each.Properties["PixelsPerXLogicalInch"].Value.ToString())); + PixelsPerYLogicalInch = uint.Parse((each.Properties["PixelsPerYLogicalInch"].Value.ToString())); + } + dpiX = PixelsPerXLogicalInch; + dpiY = PixelsPerYLogicalInch; + } + } } var maxWidth = win.MaxWidth / 96.0 * dpiX; var maxHeight = win.MaxHeight / 96.0 * dpiY;