BlurGlassUtil.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. var windowHelper = new WindowInteropHelper(window);
  47. var accent = new AccentPolicy();
  48. accent.AccentState = AccentState.ACCENT_ENABLE_BLURBEHIND;
  49. var accentStructSize = Marshal.SizeOf(accent);
  50. var accentPtr = Marshal.AllocHGlobal(accentStructSize);
  51. Marshal.StructureToPtr(accent, accentPtr, false);
  52. var data = new WindowCompositionAttributeData();
  53. data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY;
  54. data.SizeOfData = accentStructSize;
  55. data.Data = accentPtr;
  56. SetWindowCompositionAttribute(windowHelper.Handle, ref data);
  57. Marshal.FreeHGlobal(accentPtr);
  58. }
  59. }
  60. }