Transparent blurred background to Canvas

前端 未结 4 1878
礼貌的吻别
礼貌的吻别 2021-01-31 12:33

No idea how to create transparent blurred background to canvas without bluring the canvas childrens too.

This is the result I want - Blur the background

4条回答
  •  臣服心动
    2021-01-31 13:17

    My Excuse

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

    I didn't make this code, however seen as how this code isn't readily available to most people, here it is:

    using System;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Interop;
    
    namespace BlurBehindDemo
    {
    internal enum AccentState
    {
        ACCENT_DISABLED = 1,
        ACCENT_ENABLE_GRADIENT = 0,
        ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
        ACCENT_ENABLE_BLURBEHIND = 3,
        ACCENT_INVALID_STATE = 4
    }
    
    [StructLayout(LayoutKind.Sequential)]
    internal struct AccentPolicy
    {
        public AccentState AccentState;
        public int AccentFlags;
        public int GradientColor;
        public int AnimationId;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    internal struct WindowCompositionAttributeData
    {
        public WindowCompositionAttribute Attribute;
        public IntPtr Data;
        public int SizeOfData;
    }
    
    internal enum WindowCompositionAttribute
    {
        // ...
        WCA_ACCENT_POLICY = 19
        // ...
    }
    
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        [DllImport("user32.dll")]
        internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
    
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            EnableBlur();
        }
    
        internal void EnableBlur()
        {
            var windowHelper = new WindowInteropHelper(this);
    
            var accent = new AccentPolicy();
            accent.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND;
    
            var accentStructSize = Marshal.SizeOf(accent);
    
            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);
        }
    
        private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DragMove();
        }
    }
    

    }

    Result

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

    Whole window is blurred

    After a little tweaking

    Final Window

    XAML

    This was a project I made a few months back, so it won't look the same as yours, but with some tweaking you could easily make it into whatever you want it to be. The XAML of my design is as follows:

    
    
        
        
        
            
            
            
            
            
            
            
            
        
        
        
        
            
                
                    
                    
                    
                    
                
            
        
        
            
            
            
            
            
            
            
            
            
        
    
    

    I hope this helps!

提交回复
热议问题