Use PInvoke.DwmSetWindowAttribute instead of DllImport

This commit is contained in:
Jack251970 2025-03-16 14:39:56 +08:00
parent e4ade45d83
commit db37ab7373
3 changed files with 35 additions and 50 deletions

View file

@ -12,8 +12,6 @@ using System.Windows.Shell;
using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Infrastructure.UserSettings;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using Microsoft.Win32; using Microsoft.Win32;
using Flow.Launcher.Plugin; using Flow.Launcher.Plugin;
using TextBox = System.Windows.Controls.TextBox; using TextBox = System.Windows.Controls.TextBox;
@ -70,43 +68,11 @@ namespace Flow.Launcher.Core.Resource
} }
#region Blur Handling #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() public void RefreshFrame()
{ {
Application.Current.Dispatcher.Invoke(() => 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 // Remove OS minimizing/maximizing animation
// Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 3); // Methods.SetWindowAttribute(new WindowInteropHelper(mainWindow).Handle, DWMWINDOWATTRIBUTE.DWMWA_TRANSITIONS_FORCEDISABLED, 3);
@ -124,7 +90,7 @@ namespace Flow.Launcher.Core.Resource
}, DispatcherPriority.Normal); }, DispatcherPriority.Normal);
} }
public void AutoDropShadow() private void AutoDropShadow()
{ {
SetWindowCornerPreference("Default"); SetWindowCornerPreference("Default");
RemoveDropShadowEffectFromCurrentTheme(); RemoveDropShadowEffectFromCurrentTheme();
@ -153,24 +119,15 @@ namespace Flow.Launcher.Core.Resource
} }
} }
public void SetWindowCornerPreference(string cornerType) private void SetWindowCornerPreference(string cornerType)
{ {
Application.Current.Dispatcher.Invoke(() => Application.Current.Dispatcher.Invoke(() =>
{ {
System.Windows.Window mainWindow = GetMainWindow(); Window mainWindow = Application.Current.MainWindow;
if (mainWindow == null) if (mainWindow == null)
return; return;
DWM_WINDOW_CORNER_PREFERENCE preference = cornerType switch Win32Helper.DWMSetCornerPreferenceForWindow(mainWindow, cornerType);
{
"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);
}, DispatcherPriority.Normal); }, DispatcherPriority.Normal);
} }
@ -189,7 +146,7 @@ namespace Flow.Launcher.Core.Resource
if (windowBorderStyle == null) if (windowBorderStyle == null)
return; return;
Window mainWindow = GetMainWindow(); Window mainWindow = Application.Current.MainWindow;
if (mainWindow == null) if (mainWindow == null)
return; return;
@ -309,7 +266,7 @@ namespace Flow.Launcher.Core.Resource
}, DispatcherPriority.Render); }, DispatcherPriority.Render);
} }
public void ColorizeWindow(string Mode) private void ColorizeWindow(string mode)
{ {
Application.Current.Dispatcher.Invoke(() => Application.Current.Dispatcher.Invoke(() =>
{ {

View file

@ -19,4 +19,5 @@ WM_SYSKEYUP
EnumWindows EnumWindows
DwmSetWindowAttribute DwmSetWindowAttribute
DWM_SYSTEMBACKDROP_TYPE DWM_SYSTEMBACKDROP_TYPE
DWM_WINDOW_CORNER_PREFERENCE

View file

@ -46,6 +46,33 @@ namespace Flow.Launcher.Infrastructure
(uint)Marshal.SizeOf<int>()).Succeeded; (uint)Marshal.SizeOf<int>()).Succeeded;
} }
/// <summary>
///
/// </summary>
/// <param name="window"></param>
/// <param name="cornerType">DoNotRound, Round, RoundSmall, Default</param>
/// <returns></returns>
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<int>()).Succeeded;
}
#endregion #endregion
#region Backdrop #region Backdrop