Fix DPI jump on window close in net462 #1626

This commit is contained in:
ema
2025-05-15 01:33:41 +08:00
parent f7b22fb600
commit 75c45f96c2
4 changed files with 41 additions and 6 deletions
+14 -5
View File
@@ -20,6 +20,7 @@ using QuickLook.Common.Helpers;
using QuickLook.Helpers;
using QuickLook.NativeMethods;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
@@ -28,13 +29,9 @@ using System.Threading.Tasks;
using System.Windows;
using Wpf.Ui.Appearance;
using Wpf.Ui.Violeta.Appearance;
using Wpf.Ui.Violeta.Win32;
namespace QuickLook;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public static readonly string LocalDataPath = SettingHelper.LocalDataPath;
@@ -51,6 +48,19 @@ public partial class App : Application
private bool _cleanExit = true;
private Mutex _isRunning;
static App()
{
// Explicitly set to PerMonitor to avoid being overridden by the system
if (SHCore.SetProcessDpiAwareness(SHCore.PROCESS_DPI_AWARENESS.PROCESS_PER_MONITOR_DPI_AWARE) is uint result)
{
Debug.WriteLine(
result == 0 ?
"DPI Awareness applied successfully" :
$"DPI Awareness manual setup failed. Error Code: {result}"
);
}
}
protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
@@ -71,7 +81,6 @@ public partial class App : Application
ThemeManager.Apply(OSThemeHelper.AppsUseDarkTheme() ? ApplicationTheme.Dark : ApplicationTheme.Light);
UxTheme.ApplyPreferredAppMode();
DpiAware.SetProcessDpiAwareness();
base.OnStartup(e);
}
+16
View File
@@ -0,0 +1,16 @@
using System.Runtime.InteropServices;
namespace QuickLook.NativeMethods;
internal static class SHCore
{
public enum PROCESS_DPI_AWARENESS
{
PROCESS_DPI_UNAWARE,
PROCESS_SYSTEM_DPI_AWARE,
PROCESS_PER_MONITOR_DPI_AWARE
}
[DllImport("shcore.dll")]
public static extern uint SetProcessDpiAwareness(PROCESS_DPI_AWARENESS awareness);
}
+1 -1
View File
@@ -111,7 +111,7 @@
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="UnblockZoneIdentifier " Version="1.0.0">
<PackageReference Include="UnblockZoneIdentifier" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
+10
View File
@@ -20,6 +20,7 @@ using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using QuickLook.Helpers;
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Animation;
@@ -108,6 +109,15 @@ public partial class ViewerWindow : Window
buttonShare.ToolTip = TranslationHelper.Get("MW_Share");
}
public new void Close()
{
// Workaround to prevent DPI jump animation when closing window in .NET Framework 4.6.2
// Safe to remove this line if QuickLook no longer targets .NET Framework 4.6.2
Hide();
base.Close();
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();