Skip to content

Commit

Permalink
Move EnableBlur method to AcrylicHelper class
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcechord committed May 31, 2019
1 parent 2fdb614 commit 5c0f8f2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 77 deletions.
2 changes: 1 addition & 1 deletion FluentWPF/AcrylicContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected override void OnOpened(RoutedEventArgs e)
base.OnOpened(e);

var hwnd = (HwndSource)HwndSource.FromVisual(this);
AcrylicHelper.EnableBlur(hwnd.Handle);
AcrylicHelper.EnableBlur(hwnd.Handle, AccentFlagsType.Popup);
}
}
}
2 changes: 1 addition & 1 deletion FluentWPF/AcrylicPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override void OnOpened(EventArgs e)
base.OnOpened(e);

var hwnd = (HwndSource)HwndSource.FromVisual(this.Child);
AcrylicHelper.EnableBlur(hwnd.Handle);
AcrylicHelper.EnableBlur(hwnd.Handle, AccentFlagsType.Popup);
}
}
}
73 changes: 3 additions & 70 deletions FluentWPF/AcrylicWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,6 @@ public enum TitleBarMode
Extend,
}

[StructLayout(LayoutKind.Sequential)]
internal struct WindowCompositionAttributeData
{
public WindowCompositionAttribute Attribute;
public IntPtr Data;
public int SizeOfData;
}

internal enum WindowCompositionAttribute
{
// ...
WCA_ACCENT_POLICY = 19
// ...
}

internal enum AccentState
{
ACCENT_DISABLED = 0,
ACCENT_ENABLE_GRADIENT = 1,
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
ACCENT_ENABLE_BLURBEHIND = 3,
ACCENT_ENABLE_ACRYLICBLURBEHIND = 4,
ACCENT_INVALID_STATE = 5
}

[StructLayout(LayoutKind.Sequential)]
internal struct AccentPolicy
{
public AccentState AccentState;
public int AccentFlags;
public uint GradientColor;
public int AnimationId;
}

/// <summary>
/// このカスタム コントロールを XAML ファイルで使用するには、手順 1a または 1b の後、手順 2 に従います。
///
Expand Down Expand Up @@ -98,9 +64,6 @@ internal struct AccentPolicy
/// </summary>
public class AcrylicWindow : Window
{
[DllImport("user32.dll")]
internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);

static AcrylicWindow()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(AcrylicWindow), new FrameworkPropertyMetadata(typeof(AcrylicWindow)));
Expand Down Expand Up @@ -135,40 +98,10 @@ internal static void EnableBlur(Window win)
{
var windowHelper = new WindowInteropHelper(win);

var accent = new AccentPolicy();
var accentStructSize = Marshal.SizeOf(accent);
// ウィンドウ背景のぼかしを行うのはWindows10の場合のみ
// OSのバージョンに従い、AccentStateを切り替える
var currentVersion = SystemInfo.Version.Value;
if (currentVersion >= VersionInfos.Windows10_1809)
{
accent.AccentState = AccentState.ACCENT_ENABLE_ACRYLICBLURBEHIND;
}
else if (currentVersion >= VersionInfos.Windows10)
{
accent.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND;
}
else
{
accent.AccentState = AccentState.ACCENT_ENABLE_TRANSPARENTGRADIENT;
}

accent.AccentFlags = 2;
//accent.GradientColor = 0x99FFFFFF; // 60%の透明度が基本
accent.GradientColor = 0x00FFFFFF; // Tint Colorはここでは設定せず、Bindingで外部から変えられるようにXAML側のレイヤーとして定義

var accentPtr = Marshal.AllocHGlobal(accentStructSize);
Marshal.StructureToPtr(accent, accentPtr, false);

var data = new WindowCompositionAttributeData();
data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY;
data.SizeOfData = accentStructSize;
data.Data = accentPtr;

SetWindowCompositionAttribute(windowHelper.Handle, ref data);

Marshal.FreeHGlobal(accentPtr);
// ウィンドウに半透明のアクリル効果を適用する
AcrylicHelper.EnableBlur(windowHelper.Handle);

// タイトルバーの各種ボタンで利用するコマンドの設定
win.CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, (_, __) => { SystemCommands.CloseWindow(win); }));
win.CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand, (_, __) => { SystemCommands.MinimizeWindow(win); }));
win.CommandBindings.Add(new CommandBinding(SystemCommands.MaximizeWindowCommand, (_, __) => { SystemCommands.MaximizeWindow(win); }));
Expand Down
61 changes: 56 additions & 5 deletions FluentWPF/Utility/AcrylicHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,52 @@

namespace SourceChord.FluentWPF.Utility
{
static class AcrylicHelper
internal enum AccentFlagsType
{
internal static void EnableBlur(IntPtr hwnd)
Window = 0,
Popup,
}

[StructLayout(LayoutKind.Sequential)]
internal struct WindowCompositionAttributeData
{
public WindowCompositionAttribute Attribute;
public IntPtr Data;
public int SizeOfData;
}

internal enum WindowCompositionAttribute
{
// ...
WCA_ACCENT_POLICY = 19
// ...
}

internal enum AccentState
{
ACCENT_DISABLED = 0,
ACCENT_ENABLE_GRADIENT = 1,
ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
ACCENT_ENABLE_BLURBEHIND = 3,
ACCENT_ENABLE_ACRYLICBLURBEHIND = 4,
ACCENT_INVALID_STATE = 5
}

[StructLayout(LayoutKind.Sequential)]
internal struct AccentPolicy
{
public AccentState AccentState;
public int AccentFlags;
public uint GradientColor;
public int AnimationId;
}

internal static class AcrylicHelper
{
[DllImport("user32.dll")]
internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);

internal static void EnableBlur(IntPtr hwnd, AccentFlagsType style = AccentFlagsType.Window)
{
var accent = new AccentPolicy();
var accentStructSize = Marshal.SizeOf(accent);
Expand All @@ -18,7 +61,7 @@ internal static void EnableBlur(IntPtr hwnd)
var currentVersion = SystemInfo.Version.Value;
if (currentVersion >= VersionInfos.Windows10_1809)
{
accent.AccentState = AccentState.ACCENT_ENABLE_ACRYLICBLURBEHIND; //AccentState.ACCENT_ENABLE_ACRYLICBLURBEHIND;
accent.AccentState = AccentState.ACCENT_ENABLE_ACRYLICBLURBEHIND;
}
else if (currentVersion >= VersionInfos.Windows10)
{
Expand All @@ -29,7 +72,15 @@ internal static void EnableBlur(IntPtr hwnd)
accent.AccentState = AccentState.ACCENT_ENABLE_TRANSPARENTGRADIENT;
}

accent.AccentFlags = 0x20 | 0x40 | 0x80 | 0x100;
if (style == AccentFlagsType.Window)
{
accent.AccentFlags = 2;
}
else
{
accent.AccentFlags = 0x20 | 0x40 | 0x80 | 0x100;
}

//accent.GradientColor = 0x99FFFFFF; // 60%の透明度が基本
accent.GradientColor = 0x00FFFFFF; // Tint Colorはここでは設定せず、Bindingで外部から変えられるようにXAML側のレイヤーとして定義

Expand All @@ -41,7 +92,7 @@ internal static void EnableBlur(IntPtr hwnd)
data.SizeOfData = accentStructSize;
data.Data = accentPtr;

AcrylicWindow.SetWindowCompositionAttribute(hwnd, ref data);
SetWindowCompositionAttribute(hwnd, ref data);

Marshal.FreeHGlobal(accentPtr);
}
Expand Down

0 comments on commit 5c0f8f2

Please sign in to comment.