Acrtlic blur test

This commit is contained in:
the1812 2019-04-24 12:07:05 +08:00
parent 7557b3da1d
commit aae60e736c
6 changed files with 144 additions and 4 deletions

View File

@ -1,12 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
namespace VideoLinkDownloader
{
class AcrylicBlur
[StructLayout(LayoutKind.Sequential)]
struct AccentPolicy
{
public AccentState AccentState;
public int AccentFlags;
public int GradientColor;
public int AnimationId;
}
enum AccentState
{
Diabled = 0,
Gradient = 1,
TransparentGradient = 2,
BlurBehind = 3,
AcrylicBlurBehind = 4,
InvalidState = 5,
}
[StructLayout(LayoutKind.Sequential)]
struct WindowsCompostionAttributeData
{
public int Attribute;
public IntPtr Data;
public int SizeOfData;
}
static class AcrylicBlur
{
[DllImport("user32.dll")]
private static extern int SetWindowCompositionAttribute(IntPtr hWnd, ref WindowsCompostionAttributeData data);
public static void Apply(Window window, Color backgroundColor, AccentState state = AccentState.AcrylicBlurBehind)
{
int getColorCode(Color color)
{
return color.A << 24 | // DWM uses ABGR format
color.B << 16 |
color.G << 8 |
color.R;
}
var helper = new WindowInteropHelper(window);
var accent = new AccentPolicy();
var accentSize = Marshal.SizeOf(accent);
accent.AccentState = state;
accent.GradientColor = getColorCode(backgroundColor);
var accentPointer = Marshal.AllocHGlobal(accentSize);
Marshal.StructureToPtr(accent, accentPointer, false);
var data = new WindowsCompostionAttributeData
{
Attribute = 19, //WindowCompositionAttribute.AccentPolicy
SizeOfData = accentSize,
Data = accentPointer,
};
SetWindowCompositionAttribute(helper.Handle, ref data);
Marshal.FreeHGlobal(accentPointer);
}
}
}

View File

@ -7,10 +7,10 @@
xmlns:local="clr-namespace:VideoLinkDownloader"
xmlns:fw="clr-namespace:SourceChord.FluentWPF;assembly=FluentWPF"
mc:Ignorable="d"
TintColor="#afff"
FallbackColor="#afff"
TintColor="#0fff"
FallbackColor="#0fff"
Foreground="Black"
NoiseOpacity="0.03"
NoiseOpacity="0.01"
Title="MainWindow"
ShowTitleBar="False"
Height="450"
@ -27,6 +27,7 @@
Text="This is AcrylicWindow"
TextWrapping="Wrap" />
<Button
x:Name="button"
Content="Button"
HorizontalAlignment="Left"
Margin="5"

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -20,6 +21,23 @@ namespace VideoLinkDownloader
public MainWindow()
{
InitializeComponent();
button.Click += (s,e) =>
{
AcrylicBlur.Apply(this, new Color
{
A = 0,
R = 255,
G = 255,
B = 255,
}, AccentState.Diabled);
AcrylicBlur.Apply(this, new Color
{
A = 0,
R = 255,
G = 255,
B = 255,
}, AccentState.AcrylicBlurBehind);
};
}
}
}

View File

@ -0,0 +1,23 @@
<Window
x:Class="VideoLinkDownloader.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:VideoLinkDownloader"
mc:Ignorable="d"
Title="TestWindow"
Background="Transparent"
Height="450"
Width="800">
<WindowChrome.WindowChrome>
<WindowChrome
CaptionHeight="0"
GlassFrameThickness="1"
ResizeBorderThickness="4"
UseAeroCaptionButtons="False" />
</WindowChrome.WindowChrome>
<Grid>
</Grid>
</Window>

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace VideoLinkDownloader
{
/// <summary>
/// TestWindow.xaml 的交互逻辑
/// </summary>
public partial class TestWindow : Window
{
public TestWindow()
{
InitializeComponent();
Loaded += (s, e) => AcrylicBlur.Apply(this, new Color
{
A = 1,
R = 255,
G = 255,
B = 255,
});
}
}
}

View File

@ -64,6 +64,9 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="TestWindow.xaml.cs">
<DependentUpon>TestWindow.xaml</DependentUpon>
</Compile>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@ -81,6 +84,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="TestWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Compile Include="Pages\MainPage.xaml.cs">