From 29d69fa08bb857660691b08679f8d15f6045502d Mon Sep 17 00:00:00 2001 From: dotMorten Date: Mon, 18 Nov 2024 19:39:12 -0800 Subject: [PATCH] Fix splashscreen scale calculation --- src/WinUIEx/NativeMethods.txt | 3 ++- src/WinUIEx/SimpleSplashScreen.cs | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/WinUIEx/NativeMethods.txt b/src/WinUIEx/NativeMethods.txt index b2d485e..39ed06f 100644 --- a/src/WinUIEx/NativeMethods.txt +++ b/src/WinUIEx/NativeMethods.txt @@ -64,4 +64,5 @@ GdipCreateHBITMAPFromBitmap WNDCLASSEXW RegisterClassEx CreateCompatibleDC -SelectObject \ No newline at end of file +SelectObject +MonitorFromPoint diff --git a/src/WinUIEx/SimpleSplashScreen.cs b/src/WinUIEx/SimpleSplashScreen.cs index 0d9f244..e87cb13 100644 --- a/src/WinUIEx/SimpleSplashScreen.cs +++ b/src/WinUIEx/SimpleSplashScreen.cs @@ -49,8 +49,16 @@ public static SimpleSplashScreen ShowDefaultSplashScreen() var manager = new Microsoft.Windows.ApplicationModel.Resources.ResourceManager(); var context = manager.CreateResourceContext(); - var dpi = PInvoke.GetDpiForWindow(PInvoke.GetDesktopWindow()); + uint dpi = 96; + var monitor = PInvoke.MonitorFromPoint(new System.Drawing.Point(0, 0), Windows.Win32.Graphics.Gdi.MONITOR_FROM_FLAGS.MONITOR_DEFAULTTOPRIMARY); + + if(monitor != IntPtr.Zero) + { + PInvoke.GetDpiForMonitor(monitor, Windows.Win32.UI.HiDpi.MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out var dpiX, out var dpiy); + dpi = dpiX; + } var scale = (int)(dpi / 96d * 100); + if (scale == 0) scale = 100; context.QualifierValues["Scale"] = scale.ToString(); var splashScreenImageResource = manager.MainResourceMap.TryGetValue("Files/" + image.Replace('\\','/'), context); if (splashScreenImageResource is not null && splashScreenImageResource.Kind == Microsoft.Windows.ApplicationModel.Resources.ResourceCandidateKind.FilePath)