Native Aero Blur without Glass Effect on Borderless WPF Window

后端 未结 2 1089
面向向阳花
面向向阳花 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: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
        
        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:

    
    
        
        
        
            
            
            
            
            
            
            
            
        
        
        
        
            
                
                    
                    
                    
                    
                
            
        
        
            
            
            
            
            
            
            
            
            
        
    
    

    I hope this helps!

提交回复
热议问题