BlurGlassUtil.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Interop;
  9. namespace GeekDesk.Util
  10. {
  11. public class BlurGlassUtil
  12. {
  13. internal enum AccentState
  14. {
  15. ACCENT_DISABLED = 1,
  16. ACCENT_ENABLE_GRADIENT = 0,
  17. ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
  18. ACCENT_ENABLE_BLURBEHIND = 3,
  19. ACCENT_INVALID_STATE = 4
  20. }
  21. [StructLayout(LayoutKind.Sequential)]
  22. internal struct AccentPolicy
  23. {
  24. public AccentState AccentState;
  25. public int AccentFlags;
  26. public int GradientColor;
  27. public int AnimationId;
  28. }
  29. [StructLayout(LayoutKind.Sequential)]
  30. internal struct WindowCompositionAttributeData
  31. {
  32. public WindowCompositionAttribute Attribute;
  33. public IntPtr Data;
  34. public int SizeOfData;
  35. }
  36. internal enum WindowCompositionAttribute
  37. {
  38. // ...
  39. WCA_ACCENT_POLICY = 19
  40. // ...
  41. }
  42. [DllImport("user32.dll")]
  43. internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data);
  44. public static void EnableBlur(Window window)
  45. {
  46. WindowInteropHelper windowHelper = new WindowInteropHelper(window);
  47. AccentPolicy accent = new AccentPolicy
  48. {
  49. AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND
  50. };
  51. int accentStructSize = Marshal.SizeOf(accent);
  52. IntPtr accentPtr = Marshal.AllocHGlobal(accentStructSize);
  53. Marshal.StructureToPtr(accent, accentPtr, false);
  54. WindowCompositionAttributeData data = new WindowCompositionAttributeData
  55. {
  56. Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY,
  57. SizeOfData = accentStructSize,
  58. Data = accentPtr
  59. };
  60. SetWindowCompositionAttribute(windowHelper.Handle, ref data);
  61. Marshal.FreeHGlobal(accentPtr);
  62. }
  63. }
  64. }