wpf玻璃效果,WPF 扩展玻璃效果(Aero Glass)

Windows 7 操作系统默认具有一款玻璃效果主题(Aero Glass)。如果选择了该款主题,所有的应用程序标题栏都会处于玻璃透明效果(如下图)。这个功能是由Desktop Window Manager(DWM)服务支持的。
GlassExamplewpf玻璃效果,WPF 扩展玻璃效果(Aero Glass)
默认情况下,我们编写的应用程序在Windows 7 中也只有标题栏和窗口框架会具备玻璃效果,其他区域仍是不透明状态(如下图)。如果想将程序整体都改为上图IE 窗口的效果,可以使用DWM API 将玻璃区域进行扩展。
NotepadGlassExamplewpf玻璃效果,WPF 扩展玻璃效果(Aero Glass)
首先,从dwmapi.dll 中调取DwmExtendFrameIntoClientArea 方法。
[StructLayout(LayoutKind.Sequential)] public struct MARGINS { public int cxLeftWidth; public int cxRightWidth; public int cyTopHeight; public int cyBottomHeight; }; [DllImport("DwmApi.dll")] public static extern int DwmExtendFrameIntoClientArea( IntPtr hwnd, ref MARGINS pMarInset);
创建方法ExtendAeroGlass 方法,可将WPF Window窗口的Aero Glass 区域扩展。
private void ExtendAeroGlass(Window window) { try { // 为WPF程序获取窗口句柄 IntPtr mainWindowPtr = new WindowInteropHelper(window).Handle; HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr); mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent; // 设置Margins MARGINS margins = new MARGINS(); // 扩展Aero Glass margins.cxLeftWidth = -1; margins.cxRightWidth = -1; margins.cyTopHeight = -1; margins.cyBottomHeight = -1; int hr = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins); if (hr < 0) { MessageBox.Show("DwmExtendFrameIntoClientArea Failed"); } } catch (DllNotFoundException) { Application.Current.MainWindow.Background = Brushes.White; } }
简单制作一个WPF 界面。

最新评论

发表评论