From db37ab7373cb3faffab5aaeb0d45f60fe6b00c46 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 16 Mar 2025 14:39:56 +0800 Subject: [PATCH] Use PInvoke.DwmSetWindowAttribute instead of DllImport --- Flow.Launcher.Core/Resource/Theme.cs | 55 ++----------------- .../NativeMethods.txt | 3 +- Flow.Launcher.Infrastructure/Win32Helper.cs | 27 +++++++++ 3 files changed, 35 insertions(+), 50 deletions(-) diff --git a/Flow.Launcher.Core/Resource/Theme.cs b/Flow.Launcher.Core/Resource/Theme.cs index e30b79ec6..5d98c44eb 100644 --- a/Flow.Launcher.Core/Resource/Theme.cs +++ b/Flow.Launcher.Core/Resource/Theme.cs @@ -12,8 +12,6 @@ using System.Windows.Shell; using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.UserSettings; -using System.Runtime.InteropServices; -using System.Windows.Interop; using Microsoft.Win32; using Flow.Launcher.Plugin; using TextBox = System.Windows.Controls.TextBox; @@ -70,43 +68,11 @@ namespace Flow.Launcher.Core.Resource } #region Blur Handling - private const int DWMWA_WINDOW_CORNER_PREFERENCE = 33; - public enum DWM_WINDOW_CORNER_PREFERENCE - { - Default = 0, - DoNotRound = 1, - Round = 2, - RoundSmall = 3 - } - [DllImport("dwmapi.dll")] - private static extern int DwmSetWindowAttribute(IntPtr hwnd, int dwAttribute, ref DWM_WINDOW_CORNER_PREFERENCE pvAttribute, int cbAttribute); - public static void SetWindowCornerPreference(System.Windows.Window window, DWM_WINDOW_CORNER_PREFERENCE preference) - { - IntPtr hWnd = new WindowInteropHelper(window).Handle; - DwmSetWindowAttribute(hWnd, DWMWA_WINDOW_CORNER_PREFERENCE, ref preference, sizeof(int)); - } - - private Window GetMainWindow() - { - return Application.Current.Dispatcher.Invoke(() => Application.Current.MainWindow); - } public void RefreshFrame() { Application.Current.Dispatcher.Invoke(() => { - Window mainWindow = Application.Current.MainWindow; - if (mainWindow == null) - return; - - IntPtr mainWindowPtr = new WindowInteropHelper(mainWindow).Handle; - if (mainWindowPtr == IntPtr.Zero) - return; - - HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr); - if (mainWindowSrc == null) - return; - // Remove OS minimizing/maximizing animation // Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 3); @@ -124,7 +90,7 @@ namespace Flow.Launcher.Core.Resource }, DispatcherPriority.Normal); } - public void AutoDropShadow() + private void AutoDropShadow() { SetWindowCornerPreference("Default"); RemoveDropShadowEffectFromCurrentTheme(); @@ -153,24 +119,15 @@ namespace Flow.Launcher.Core.Resource } } - public void SetWindowCornerPreference(string cornerType) + private void SetWindowCornerPreference(string cornerType) { Application.Current.Dispatcher.Invoke(() => { - System.Windows.Window mainWindow = GetMainWindow(); + Window mainWindow = Application.Current.MainWindow; if (mainWindow == null) return; - DWM_WINDOW_CORNER_PREFERENCE preference = cornerType switch - { - "DoNotRound" => DWM_WINDOW_CORNER_PREFERENCE.DoNotRound, - "Round" => DWM_WINDOW_CORNER_PREFERENCE.Round, - "RoundSmall" => DWM_WINDOW_CORNER_PREFERENCE.RoundSmall, - "Default" => DWM_WINDOW_CORNER_PREFERENCE.Default, - _ => DWM_WINDOW_CORNER_PREFERENCE.Default, - }; - - SetWindowCornerPreference(mainWindow, preference); + Win32Helper.DWMSetCornerPreferenceForWindow(mainWindow, cornerType); }, DispatcherPriority.Normal); } @@ -189,7 +146,7 @@ namespace Flow.Launcher.Core.Resource if (windowBorderStyle == null) return; - Window mainWindow = GetMainWindow(); + Window mainWindow = Application.Current.MainWindow; if (mainWindow == null) return; @@ -309,7 +266,7 @@ namespace Flow.Launcher.Core.Resource }, DispatcherPriority.Render); } - public void ColorizeWindow(string Mode) + private void ColorizeWindow(string mode) { Application.Current.Dispatcher.Invoke(() => { diff --git a/Flow.Launcher.Infrastructure/NativeMethods.txt b/Flow.Launcher.Infrastructure/NativeMethods.txt index b352c43d1..e7256e8c4 100644 --- a/Flow.Launcher.Infrastructure/NativeMethods.txt +++ b/Flow.Launcher.Infrastructure/NativeMethods.txt @@ -19,4 +19,5 @@ WM_SYSKEYUP EnumWindows DwmSetWindowAttribute -DWM_SYSTEMBACKDROP_TYPE \ No newline at end of file +DWM_SYSTEMBACKDROP_TYPE +DWM_WINDOW_CORNER_PREFERENCE \ No newline at end of file diff --git a/Flow.Launcher.Infrastructure/Win32Helper.cs b/Flow.Launcher.Infrastructure/Win32Helper.cs index 9a8f69a34..7f0c9a287 100644 --- a/Flow.Launcher.Infrastructure/Win32Helper.cs +++ b/Flow.Launcher.Infrastructure/Win32Helper.cs @@ -46,6 +46,33 @@ namespace Flow.Launcher.Infrastructure (uint)Marshal.SizeOf()).Succeeded; } + /// + /// + /// + /// + /// DoNotRound, Round, RoundSmall, Default + /// + public static unsafe bool DWMSetCornerPreferenceForWindow(Window window, string cornerType) + { + var windowHelper = new WindowInteropHelper(window); + windowHelper.EnsureHandle(); + + var preference = cornerType switch + { + "DoNotRound" => DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_DONOTROUND, + "Round" => DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND, + "RoundSmall" => DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUNDSMALL, + "Default" => DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_DEFAULT, + _ => throw new InvalidOperationException("Invalid corner type") + }; + + return PInvoke.DwmSetWindowAttribute( + new(windowHelper.Handle), + DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE, + &preference, + (uint)Marshal.SizeOf()).Succeeded; + } + #endregion #region Backdrop