Windows 7 操作系统默认具有一款玻璃效果主题(Aero Glass)。如果选择了该款主题,所有的应用程序标题栏都会处于玻璃透明效果(如下图)。这个功能是由Desktop Window Manager(DWM)服务支持的。
那我们自己怎么实现呢,其实很简单
新建一个类,这里我们给他取名为AeroGlassHelper.cs
代码如下:
using System; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; using System.Windows.Media; [StructLayout(LayoutKind.Sequential)] public struct MARGINS { public MARGINS(Thickness t) { Left = (int)t.Left; Right = (int)t.Right; Top = (int)t.Top; Bottom = (int)t.Bottom; } public int Left; public int Right; public int Top; public int Bottom; } public class GlassHelper { [DllImport("dwmapi.dll", PreserveSig = false)] static extern void DwmExtendFrameIntoClientArea( IntPtr hWnd, ref MARGINS pMarInset); [DllImport("dwmapi.dll", PreserveSig = false)] static extern bool DwmIsCompositionEnabled(); public static bool ExtendGlassFrame(Window window, Thickness margin) { if (!DwmIsCompositionEnabled()) return false; IntPtr hwnd = new WindowInteropHelper(window).Handle; if (hwnd == IntPtr.Zero) throw new InvalidOperationException( "The Window must be shown before extending glass."); // Set the background to transparent from both the WPF and Win32 perspectives window.Background = Brushes.Transparent; HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent; MARGINS margins = new MARGINS(margin); DwmExtendFrameIntoClientArea(hwnd, ref margins); return true; } }
在你需要的开启的地方添加如下代码:
protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); GlassHelper.ExtendGlassFrame(this, new Thickness(-1)); }
发表回复