首页 > 代码库 > WPF实现毛玻璃效果
WPF实现毛玻璃效果
1和2需要Microsoft.WindowsAPICodePack.Shell.dll 和引用using System.Windows.Interop,并只能在有DwmApi.dll 版本的Windows操作系统下使用。这两种方法的共同缺点是:在启动窗体时会一闪。
一、
[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); 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; } } private void Window_Loaded(object sender, RoutedEventArgs e) { this.Background = Brushes.Transparent; ExtendAeroGlass(this); }
二、
[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)); }
三、
这个方法 需要Microsoft.Windows.Shell.dll 没有一闪的缺陷!
xmlns:shell="clr-namespace:Microsoft.Windows.Shell;assembly=Microsoft.Windows.Shell"
<shell:WindowChrome.WindowChrome> <shell:WindowChrome GlassFrameThickness="-1" ResizeBorderThickness="1" CaptionHeight="15" CornerRadius="0" /> </shell:WindowChrome.WindowChrome>
<Window.Template> <ControlTemplate TargetType="{x:Type Window}"> <Border BorderThickness="1"> <DockPanel> <Grid x:Name="WindowHeader" DockPanel.Dock="Top" Height="15"> <TextBlock Text="{TemplateBinding Title}"/> </Grid> <ContentPresenter Margin="5,5,5,5"/> </DockPanel> </Border> </ControlTemplate> </Window.Template>
转载地址:http://blog.csdn.net/kingscrown/article/details/7771094
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。