Native Aero Blur without Glass Effect on Borderless WPF Window

后端 未结 2 1090
面向向阳花
面向向阳花 2021-01-30 15:07

I am aware that similar questions have been asked and answered. Mine, however, is a three-part question.

For the purposes of this question, keep the following in mind:

相关标签:
2条回答
  • 2021-01-30 15:53

    1. Applying Aero Glass to Borderless Window

    Since what you are trying to achieve is not so much a glass effect but more of a transparent+blur you can use the following methods to blur behind the window.

    Windows 7: you can use DwmEnableBlurBehindWindow to blur behind the window.

    Window 8: I havn't found a workable solution since DwmEnableBlurBehindWindow was removed in Windows 8.

    Windows 10: you can use SetWindowCompositionAttribute to blur behind the window.

    2. Keeping Native Blur Effect without Glass

    The above solutions will only apply a blur effect behind the window, it will be up to the window to define transparency and colour.

    3. Customizing Blur Radius and Location

    With these approaches you can only blur underneath the entire window, and it will be up to you to use an alpha channel on portions of the window you want to be transparent. I don't think you can define the blur radius either.

    0 讨论(0)
  • 2021-01-30 15:57

    My Excuse

    Pardon my inexperience with Stackoverflow but I thought I would try and help you out a little.

    By following the link posted by Tom I was able to come across this block of code (originally in c#). So seen as how this code isn't readily availible to most people, here it is:

    Imports System.Runtime.InteropServices
    Imports System.Windows.Interop
    'Import namespace ("name of project" . "name of namespace")
    Imports Blurred_Opacity.BlurBehind
    
    Namespace BlurTest
        Enum AccentState
            ACCENT_DISABLED = 0
            ACCENT_ENABLE_GRADIENT = 1
            ACCENT_ENABLE_TRANSPARENTGRADIENT = 2
            ACCENT_ENABLE_BLURBEHIND = 3
            ACCENT_INVALID_STATE = 4
        End Enum
    
        Structure AccentPolicy
            Public AccentState As AccentState
            Public AccentFlags As Integer
            Public GradientColor As Integer
            Public AnimationId As Integer
        End Structure
    
        Structure WindowCompositionAttributeData
            Public Attribute As WindowCompositionAttribute
            Public Data As IntPtr
            Public SizeOfData As Integer
        End Structure
    
        Enum WindowCompositionAttribute
            WCA_ACCENT_POLICY = 19
        End Enum
    End Namespace
    
    Class MainWindow
        <DllImport("user32.dll")>
        Friend Shared Function SetWindowCompositionAttribute(hwnd As IntPtr, ByRef data As WindowCompositionAttributeData) As Integer
        End Function
    
        Sub Window_Loaded() handles me.loaded
            EnableBlur()
        End Sub
        Sub Window_MouseDown() handles me.MouseLeftButtonDown
            DragMove()
        End Sub
    
        Sub EnableBlur()
            Dim windowHelper = New WindowInteropHelper(Me)
            Dim accent = New AccentPolicy()
            accent.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND
            Dim accentStructSize = Marshal.SizeOf(accent)
            Dim accentPtr = Marshal.AllocHGlobal(accentStructSize)
            Marshal.StructureToPtr(accent, accentPtr, False)
            Dim Data = New WindowCompositionAttributeData()
            Data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY
            Data.SizeOfData = accentStructSize
            Data.Data = accentPtr
            SetWindowCompositionAttribute(windowHelper.Handle, Data)
            Marshal.FreeHGlobal(accentPtr)
        End Sub
    End Class
    

    Result

    Once implemented, this will effect the whole Window as shown:

    Whole window is blurred

    After a little tweaking

    After about 5 mins of trying to copy your design I came up with this:

    Final Window

    XAML

    I'm sure you could do a better job of the design than I have. It is possible to change the blend colour simply by adjusting the background colour (on the window), and the opacity level can also be changed. The XAML of my design is as follows:

    <Window
        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" mc:Ignorable="d" x:Class="MainWindow"
        Title="Blurred Opacity" Height="623" Width="752"
        Background="#727A7A7A"
        AllowsTransparency="True"
        WindowStyle="None"
        BorderThickness="1"
        WindowStartupLocation="CenterScreen"
        Loaded="Window_Loaded" MouseLeftButtonDown="Window_MouseDown" Topmost="True" BorderBrush="#FF1E9EC5">
    <Grid>
        <Rectangle Fill="#FF0143A4" Height="130" VerticalAlignment="Top"/>
        <Rectangle Fill="White" Margin="0,130,0,0" HorizontalAlignment="Right" Width="375"/>
        <StackPanel HorizontalAlignment="Left" Margin="0,130,0,0" Width="375">
            <TextBlock x:Name="textBlock" Height="50" TextWrapping="Wrap" Text="Category 1" d:LayoutOverrides="LeftPosition, RightPosition" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6"/>
            <TextBlock x:Name="textBlock_Copy" Height="50" TextWrapping="Wrap" Text="Category 2" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
            <TextBlock x:Name="textBlock_Copy1" Height="50" TextWrapping="Wrap" Text="Category 3" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
            <TextBlock x:Name="textBlock_Copy2" Height="50" TextWrapping="Wrap" Text="Category 4" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
            <TextBlock x:Name="textBlock_Copy3" Height="50" TextWrapping="Wrap" Text="Category 5" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
            <TextBlock x:Name="textBlock_Copy4" Height="50" TextWrapping="Wrap" Text="Category 6" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
            <TextBlock x:Name="textBlock_Copy5" Height="50" TextWrapping="Wrap" Text="Category 7" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
            <TextBlock x:Name="textBlock_Copy6" Height="50" TextWrapping="Wrap" Text="Category 8" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
        </StackPanel>
        <TextBlock x:Name="textBlock_Copy7" Height="90" TextWrapping="Wrap" Text="Example" FontSize="65" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" Margin="222.5,23,152.5,0" VerticalAlignment="Top" Foreground="White"/>
        <Path Data="M705,27.333333 L735.66667,10" Fill="White" HorizontalAlignment="Right" Height="24" Margin="0,7,21,0" Stretch="Fill" VerticalAlignment="Top" Width="24" StrokeThickness="3" Stroke="White"/>
        <Path Data="M705,27.333333 L735.66667,10" Fill="White" HorizontalAlignment="Right" Height="24.083" Margin="0,6.833,20.333,0" Stretch="Fill" VerticalAlignment="Top" Width="24.167" StrokeThickness="3" Stroke="White" RenderTransformOrigin="0.5,0.5">
            <Path.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleX="-1"/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Path.RenderTransform>
        </Path>
        <StackPanel HorizontalAlignment="Right" Margin="0,130,0,0" Width="375">
            <TextBlock x:Name="textBlock1" Height="50" TextWrapping="Wrap" Text="Item 1" d:LayoutOverrides="LeftPosition, RightPosition" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6"/>
            <TextBlock x:Name="textBlock_Copy8" Height="50" TextWrapping="Wrap" Text="Item 2" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"/>
            <TextBlock x:Name="textBlock_Copy9" Height="50" TextWrapping="Wrap" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"><Run Text="Item "/><Run Text="3"/><LineBreak/><Run Text="3"/></TextBlock>
            <TextBlock x:Name="textBlock_Copy10" Height="50" TextWrapping="Wrap" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"><Run Text="Item "/><Run Text="4"/></TextBlock>
            <TextBlock x:Name="textBlock_Copy11" Height="50" TextWrapping="Wrap" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"><Run Text="Item "/><Run Text="5"/></TextBlock>
            <TextBlock x:Name="textBlock_Copy12" Height="50" TextWrapping="Wrap" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"><Run Text="Item "/><Run Text="6"/></TextBlock>
            <TextBlock x:Name="textBlock_Copy13" Height="50" TextWrapping="Wrap" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"><Run Text="Item "/><Run Text="7"/></TextBlock>
            <TextBlock x:Name="textBlock_Copy14" Height="50" TextWrapping="Wrap" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"><Run Text="Item "/><Run Text="8"/></TextBlock>
            <TextBlock x:Name="textBlock_Copy15" Height="50" TextWrapping="Wrap" FontSize="35" Padding="10,0,0,0" TextOptions.TextFormattingMode="Display" LineHeight="6" d:LayoutOverrides="LeftPosition, RightPosition"><Run Text="Item "/><Run Text="9"/></TextBlock>
        </StackPanel>
    </Grid>
    

    I hope this helps!

    0 讨论(0)
提交回复
热议问题