Advertisement
Guest User

WindowBlur

a guest
Dec 30th, 2020
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows;
  4. using System.Windows.Interop;
  5.  
  6. namespace MakeWindowBlur
  7. {
  8.     internal enum AccentState
  9.     {
  10.         ACCENT_DISABLED = 1,
  11.         ACCENT_ENABLE_GRADIENT = 0,
  12.         ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
  13.         ACCENT_ENABLE_BLURBEHIND = 3,
  14.         ACCENT_INVALID_STATE = 4
  15.     }
  16.  
  17.     [StructLayout(LayoutKind.Sequential)]
  18.     internal struct AccentPolicy
  19.     {
  20.         public AccentState AccentState;
  21.         public int AccentFlags;
  22.         public int GradientColor;
  23.         public int AnimationId;
  24.     }
  25.  
  26.     [StructLayout(LayoutKind.Sequential)]
  27.     internal struct WindowCompositionAttributeData
  28.     {
  29.         public WindowCompositionAttribute Attribute;
  30.         public IntPtr Data;
  31.         public int SizeOfData;
  32.     }
  33.  
  34.     internal enum WindowCompositionAttribute
  35.     {
  36.         // ...
  37.         WCA_ACCENT_POLICY = 19
  38.         // ...
  39.     }
  40.  
  41.     internal class WindowBlur
  42.     {
  43.         [DllImport("user32.dll")]
  44.         internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
  45.  
  46.         internal void EnableBlur(Window window)
  47.         {
  48.             var windowHelper = new WindowInteropHelper(window);
  49.  
  50.             var accent = new AccentPolicy
  51.             {
  52.                 AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND
  53.             };
  54.  
  55.             var accentStructSize = Marshal.SizeOf(accent);
  56.  
  57.             var accentPtr = Marshal.AllocHGlobal(accentStructSize);
  58.             Marshal.StructureToPtr(accent, accentPtr, false);
  59.  
  60.             var data = new WindowCompositionAttributeData
  61.             {
  62.                 Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY,
  63.                 SizeOfData = accentStructSize,
  64.                 Data = accentPtr
  65.             };
  66.  
  67.             SetWindowCompositionAttribute(windowHelper.Handle, ref data);
  68.  
  69.             Marshal.FreeHGlobal(accentPtr);
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement