欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WPF Aero Glass Window

程序员文章站 2022-07-05 08:43:39
用法 ......
用法 Win7 DwmSetWindowAttribute function Win10 SetWindowCompositionAttribute 代码
  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 
 10 namespace AeroWindow
 11 {
 12     internal static class NativeMethods
 13     {
 14         [DllImport("user32.dll")]
 15         internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttribData data);
 16 
 17         [StructLayout(LayoutKind.Sequential)]
 18         internal struct WindowCompositionAttribData
 19         {
 20             public WindowCompositionAttribute Attribute;
 21             public IntPtr Data;
 22             public int SizeOfData;
 23         }
 24 
 25         [StructLayout(LayoutKind.Sequential)]
 26         internal struct AccentPolicy
 27         {
 28             public AccentState AccentState;
 29             public AccentFlags AccentFlags;
 30             public int GradientColor;
 31             public int AnimationId;
 32         }
 33 
 34         [Flags]
 35         internal enum AccentFlags
 36         {
 37             // ... 
 38             DrawLeftBorder = 0x20,
 39             DrawTopBorder = 0x40,
 40             DrawRightBorder = 0x80,
 41             DrawBottomBorder = 0x100,
 42             DrawAllBorders = (DrawLeftBorder | DrawTopBorder | DrawRightBorder | DrawBottomBorder)
 43             // ... 
 44         }
 45 
 46         internal enum WindowCompositionAttribute
 47         {
 48             // ... 
 49             WCA_ACCENT_POLICY = 19
 50             // ... 
 51         }
 52 
 53         internal enum AccentState
 54         {
 55             ACCENT_DISABLED = 0,
 56             ACCENT_ENABLE_GRADIENT = 1,
 57             ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
 58             ACCENT_ENABLE_BLURBEHIND = 3,
 59             ACCENT_INVALID_STATE = 4
 60         }
 61 
 62         public static void EnableBlur(this Window window)
 63         {
 64             if (SystemParameters.HighContrast)
 65             {
 66                 return; // Blur is not useful in high contrast mode 
 67             }
 68             SetAccentPolicy(window, NativeMethods.AccentState.ACCENT_ENABLE_BLURBEHIND);
 69         }
 70 
 71 
 72         public static void DisableBlur(this Window window)
 73         {
 74             SetAccentPolicy(window, NativeMethods.AccentState.ACCENT_DISABLED);
 75         }
 76 
 77         private static void SetAccentPolicy(Window window, NativeMethods.AccentState accentState)
 78         {
 79             var windowHelper = new WindowInteropHelper(window);
 80             var accent = new NativeMethods.AccentPolicy
 81             {
 82                 AccentState = accentState,
 83                 AccentFlags = GetAccentFlagsForTaskbarPosition(),
 84                   AnimationId = 2
 85             };
 86             var accentStructSize = Marshal.SizeOf(accent);
 87             var accentPtr = Marshal.AllocHGlobal(accentStructSize);
 88             Marshal.StructureToPtr(accent, accentPtr, false);
 89             var data = new NativeMethods.WindowCompositionAttribData
 90             {
 91                 Attribute = NativeMethods.WindowCompositionAttribute.WCA_ACCENT_POLICY,
 92                 SizeOfData = accentStructSize,
 93                 Data = accentPtr
 94             };
 95             NativeMethods.SetWindowCompositionAttribute(windowHelper.Handle, ref data);
 96             Marshal.FreeHGlobal(accentPtr);
 97         }
 98 
 99         private static NativeMethods.AccentFlags GetAccentFlagsForTaskbarPosition()
100         {
101             return NativeMethods.AccentFlags.DrawAllBorders;
102         }
103     }
104  }

 

 1  public MainWindow()
 2         {
 3             RoutedEventHandler handler = null;
 4             handler = (s, e) =>
 5             {
 6                 Loaded -= handler;
 7                 this.EnableBlur();
 8             };
 9             Loaded += handler;
10 
11             InitializeComponent();
12         }
 1 <Window x:Class="AeroWindow.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell"
 7         xmlns:local="clr-namespace:AeroWindow"
 8         mc:Ignorable="d" 
 9         Background="#44E6ECF0" 
10         Title="MainWindow" Height="600" Width="800" >
11     <shell:WindowChrome.WindowChrome>
12         <shell:WindowChrome GlassFrameThickness="1" UseAeroCaptionButtons="False"  NonClientFrameEdges="None"  CornerRadius="10" CaptionHeight="600"   />
13     </shell:WindowChrome.WindowChrome>
14     <Grid/>
15 </Window>

 

效果

  WPF Aero Glass Window