原文:WPF中,如何将Vista Aero效果扩展到整个窗口

  WPF中,如何将Vista Aero效果扩展到整个窗口
                                                                                           
周银辉
效果图:


有不少示例介绍了如何将Vista Aero效果扩展到整个窗口,但大都是针对Windows Form应用程序,而不是WPF(即前者针对的是Form类,后者是针对的Window类),比如http://www.cnblogs.com/zhouyinhui/archive/2007/05/30/765416.html
其实与其类似,都是调用dwmapi,只不过Window类没有直接给我们提供句柄,我们需要这样的代码来找到其句柄:

IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle;

然后将窗口的背景设置为透明:

window.Background = Brushes.Transparent;
            HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;

最后调用

 DwmApi.DwmExtendFrameIntoClientArea(hwnd, margins);

注意,我们应该在窗口被显示之后(SourceInitialized之后)再调用我们的函数否则会引发异常。

参考代码:

 public partial class Window1 : System.Windows.Window
    {

        public Window1()
        {
            InitializeComponent();

        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            DWMLib.AeroHelper.ExtendGlassFrame(this, new Thickness(-1));

        }

    }
public class AeroHelper
    {
        public static bool ExtendGlassFrame(Window window, Thickness margin)
        {
            if (!DwmApi.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;

            DWMLib.DwmApi.MARGINS margins = new DWMLib.DwmApi.MARGINS((int)margin.Left, (int)margin.Top, (int)margin.Right, (int)margin.Bottom);
            DwmApi.DwmExtendFrameIntoClientArea(hwnd, margins);

            return true;
        }
public class DwmApi
    {
        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern void DwmEnableBlurBehindWindow(IntPtr hWnd, DWM_BLURBEHIND pBlurBehind);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, MARGINS pMargins);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern bool DwmIsCompositionEnabled();

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern void DwmGetColorizationColor(
            out int pcrColorization,
            [MarshalAs(UnmanagedType.Bool)]out bool pfOpaqueBlend);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern void DwmEnableComposition(bool bEnable);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern IntPtr DwmRegisterThumbnail(IntPtr dest, IntPtr source);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern void DwmUnregisterThumbnail(IntPtr hThumbnail);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern void DwmUpdateThumbnailProperties(IntPtr hThumbnail, DWM_THUMBNAIL_PROPERTIES props);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern void DwmQueryThumbnailSourceSize(IntPtr hThumbnail, out Size size);

        [StructLayout(LayoutKind.Sequential)]
        public class DWM_THUMBNAIL_PROPERTIES
        {
            public uint dwFlags;
            public RECT rcDestination;
            public RECT rcSource;
            public byte opacity;
            [MarshalAs(UnmanagedType.Bool)]
            public bool fVisible;
            [MarshalAs(UnmanagedType.Bool)]
            public bool fSourceClientAreaOnly;

            public const uint DWM_TNP_RECTDESTINATION = 0x00000001;
            public const uint DWM_TNP_RECTSOURCE = 0x00000002;
            public const uint DWM_TNP_OPACITY = 0x00000004;
            public const uint DWM_TNP_VISIBLE = 0x00000008;
            public const uint DWM_TNP_SOURCECLIENTAREAONLY = 0x00000010;
        }

        [StructLayout(LayoutKind.Sequential)]
        public class MARGINS
        {
            public int cxLeftWidth, cxRightWidth, cyTopHeight, cyBottomHeight;

            public MARGINS(int left, int top, int right, int bottom)
            {
                cxLeftWidth = left;
                cyTopHeight = top;
                cxRightWidth = right;
                cyBottomHeight = bottom;
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        public class DWM_BLURBEHIND
        {
            public uint dwFlags;
            [MarshalAs(UnmanagedType.Bool)]
            public bool fEnable;
            public IntPtr hRegionBlur;
            [MarshalAs(UnmanagedType.Bool)]
            public bool fTransitionOnMaximized;

            public const uint DWM_BB_ENABLE = 0x00000001;
            public const uint DWM_BB_BLURREGION = 0x00000002;
            public const uint DWM_BB_TRANSITIONONMAXIMIZED = 0x00000004;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left, top, right, bottom;

            public RECT(int left, int top, int right, int bottom)
            {
                this.left = left;
                this.top = top;
                this.right = right;
                this.bottom = bottom;
            }
        }
    }

Demo, 要正确运行Demo,你应该使用开启Aero效果的Vista版本

WPF中,如何将Vista Aero效果扩展到整个窗口的更多相关文章

  1. WPF中利用RadialGradient模拟放大镜效果

    原文:WPF中利用RadialGradient模拟放大镜效果 --------------------------------------------------------------------- ...

  2. WPF中,如何屏蔽WebBrowser弹出的脚本错误窗口?

    WPF没有自带屏蔽这些窗口的方法或属性,可以使用反射的方法来屏蔽弹出脚本错误窗口: public void SuppressScriptErrors(WebBrowser wb, bool Hide) ...

  3. wpf实现IE菜单栏自动隐藏效果

    IE菜单栏默认为隐藏状态,按下键盘Alt键后显示,菜单失去焦点则自动隐藏.下面说说WPF中如何实现这样的效果. 第一步:Menu默认设置为隐藏(Visibility="Collapsed&q ...

  4. 扩展ArcGIS API for Silverlight/WPF 中的TextSymbol支持角度标注

    原文 http://blog.csdn.net/esricd/article/details/7587136 在ArcGIS API for Silverlight/WPF中原版的TextSymbol ...

  5. WPF中查找控件的扩展类

    在wpf中查找控件要用到VisualTreeHelper类,但这个类并没有按照名字查找控件的方法,于是搜索网络,整理出下面这个类,感觉用起来很是方便. 贴出来,供大家参考. /// <summa ...

  6. 在WPF中使用PlaneProjection模拟动态3D效果

    原文:在WPF中使用PlaneProjection模拟动态3D效果 虽然在WPF中也集成了3D呈现的功能,在简单的3D应用中,有时候并不需要真实光影的3D场景.毕竟使用3D引擎会消耗很多资源,有时候使 ...

  7. 浏览器扩展系列————在WPF中定制WebBrowser快捷菜单

    原文:浏览器扩展系列----在WPF中定制WebBrowser快捷菜单 关于如何定制菜单可以参考codeproject上的这篇文章:http://www.codeproject.com/KB/book ...

  8. wpf中xaml的类型转换器与标记扩展

    原文:wpf中xaml的类型转换器与标记扩展 这篇来讲wpf控件属性的类型转换器 类型转换器 类型转换器在asp.net控件中已经有使用过了,由于wpf的界面是可以由xaml组成的,所以标签的便利也需 ...

  9. WPF中制作立体效果的文字或LOGO图形(续)

    原文:WPF中制作立体效果的文字或LOGO图形(续) 上篇"WPF中制作立体效果的文字或LOGO图形"(http://blog.csdn.net/johnsuna/archive/ ...

随机推荐

  1. github desktop项目版本控制

    [git版本控制-笔记]by lijun   0.推荐学习网址:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b ...

  2. HDU 6034 6038

    6034:给每个字母26进制的贪心.例如一个字母 c = 7*26^89 + 6*26^50.... 这个字符串有10^5长度.普通的大整数会超时,这里要稀疏这个大数一个pair<int,int ...

  3. Oracle文本导入器

    将文本格式化为Tab分隔的数据行,可以用Excel搞定 如果新增列需要从前面的列获取数据,比如用身份证号计算年龄 需要保证新增列在导入的文件中实际存在列,否则无法使用相对位置和绝对位置进行取值 相对位 ...

  4. 一、安装 IntelliJ IDEA

    首先,双击打开 IntelliJ IDEA 的快捷方式: 在此,需要说明: 如果咱们的电脑曾经安装过 IntelliJ IDEA,并且你在卸载 IntelliJ IDEA 的时候没有删除其配置文件目录 ...

  5. 课时91.CSS元素显示模式(掌握)

    在HTML中HTML将所有的标签分为两类,分别是容器级和文本级 在CSS中CSS也将所有的标签分为两类,分别是块级元素和行内元素 1.什么是块级元素,什么是行内元素? 块级元素会独占一行 行内元素不会 ...

  6. SpringBoot非官方教程 | 第八篇:springboot整合mongodb

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot8-mongodb/ 本文出自方志朋的博客 这篇文 ...

  7. Java笔试--代码纠错

    package practice.javase; public abstract class Name { private String name; public abstract boolean i ...

  8. CentOS 7安装Oracle (CentOS Linux release 7.5.1804)

    从安装操作系统到完成oracle安装 1.安装centos7 下载CentOS7 iso安装包,配置虚拟机,由于只进行oracle安装练习,随便配置20G空间.选择安装文件. 开机,开始安装系统: 直 ...

  9. Struts2中期(这框架目前正处于淘汰状态)

    Struts2的第二天 Struts2的第二天的内容 1. Struts2框架中的Servlet的API的使用 2. Struts2中Action接收请求参数 3. Struts2中自定义拦截器 案例 ...

  10. Xtrabackup备份与恢复MySQL

    1.innobackupex备份原理 .innobackupex启动并fork一个进程启动xtrabackup,然后等待xtrabackup备份InnoDB文件; .xtrabackup备份时存在两个 ...