Skip to content

Commit 41b05b3

Browse files
committed
Replace dll import with PInvoke for code quality
1 parent db5036a commit 41b05b3

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

Flow.Launcher.Infrastructure/NativeMethods.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ WM_KEYUP
1616
WM_SYSKEYDOWN
1717
WM_SYSKEYUP
1818

19-
EnumWindows
19+
EnumWindows
20+
21+
DwmSetWindowAttribute
22+
DWM_SYSTEMBACKDROP_TYPE

Flow.Launcher.Infrastructure/Win32Helper.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,30 @@
22
using System.Runtime.InteropServices;
33
using System.Windows.Interop;
44
using System.Windows;
5+
using Windows.Win32;
6+
using Windows.Win32.Graphics.Dwm;
57

68
namespace Flow.Launcher.Infrastructure
79
{
810
public static class Win32Helper
911
{
10-
private enum DwmSystemBackdropType
11-
{
12-
DWMSBT_AUTO = 0,
13-
DWMSBT_NONE = 1,
14-
DWMSBT_MICA = 2,
15-
DWMSBT_ACRYLIC = 3,
16-
DWMSBT_TABBED = 4
17-
}
18-
19-
private const int DWMWA_SYSTEMBACKDROP_TYPE = 38;
20-
21-
[DllImport("dwmapi.dll")]
22-
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int dwAttribute, ref DwmSystemBackdropType pvAttribute, int cbAttribute);
12+
#region Mica
2313

24-
public static void SetMicaForWindow(Window window, bool enableMica)
14+
public static unsafe bool SetMicaForWindow(Window window, bool enableMica)
2515
{
2616
var windowHelper = new WindowInteropHelper(window);
2717
windowHelper.EnsureHandle();
2818

29-
DwmSystemBackdropType backdropType = enableMica ? DwmSystemBackdropType.DWMSBT_MICA : DwmSystemBackdropType.DWMSBT_NONE;
30-
DwmSetWindowAttribute(windowHelper.Handle, DWMWA_SYSTEMBACKDROP_TYPE, ref backdropType, sizeof(int));
19+
DWM_SYSTEMBACKDROP_TYPE backdropType = enableMica ? DWM_SYSTEMBACKDROP_TYPE.DWMSBT_MAINWINDOW : DWM_SYSTEMBACKDROP_TYPE.DWMSBT_NONE;
20+
return PInvoke.DwmSetWindowAttribute(
21+
new(windowHelper.Handle),
22+
DWMWINDOWATTRIBUTE.DWMWA_SYSTEMBACKDROP_TYPE,
23+
&backdropType,
24+
(uint)Marshal.SizeOf<int>()).Succeeded;
3125
}
26+
27+
#endregion
28+
3229
#region Blur Handling
3330

3431
/*

0 commit comments

Comments
 (0)