WPF中,如何将Vista Aero效果扩展到整个窗口
原文: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版本

posted on 2019-04-03 23:55 NET未来之路 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/10652528.html

WPF中,如何将Vista Aero效果扩展到整个窗口相关推荐

  1. 如何开启Vista Aero效果

    点击订阅RSS 问题描述: 如何开启Vista的透明效果? 解决方案: 1. 什么是透明效果? 透明效果就是Windows Aero,即Windows Vista 的完美视觉体验.它采用透明玻璃式设计 ...

  2. 让低端显卡运行Vista Aero效果更流畅

    <script language='javascript' src='http://www.shiqiaotou.com/donetk/Header.js'></script> ...

  3. 在VirtualBox下安装win7实现aero效果的图文教程

     转自:http://www.ipc.me/virtualbox-win7-aero.html 默认情况下,在VirtualBox中安装好Windows 7后是没有aero效果的,对于追求视觉效果 ...

  4. 有关远程连接后aero效果消失的问题

    由于使用了向日葵远程连接自己的电脑,结果发现电脑的aero效果消失,而且窗口拖动时有幻影,显得很慢.没办法,只有处理一下! 尝试了一些方法之后都没有效果,甚至想重装显卡驱动!最后灵光一闪,让我看到了向 ...

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

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

  6. WPF中制作立体效果的文字或LOGO图形

    WPF中制作立体效果的文字或LOGO图形 原文:WPF中制作立体效果的文字或LOGO图形 较久之前,我曾写过一篇:"WPF绘制党徽(立体效果,Cool) "的博文.有感兴趣的朋友来 ...

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

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

  8. 修复WIN7中无法应用Aero效果的问题

    一台不能连接互联网的电脑,显卡驱动一直都是新的,但是无法应用Aero效果的桌面.任务栏属性中启用Aero Peak预览桌面是灰色的. 我解决的方法:http://bbs.ithome.com/thre ...

  9. WPF中GDI+图形图像的绘制:(五)绘制图像——蒙板效果

    这里要实现的效果指的是对图片像素点区域进行颜色叠加,首先要做的是得到图片非透明部分的区域,效果如图: 实现步骤:1.在主窗体添加控制控件: <Label Grid.Row="4&quo ...

最新文章

  1. 服务器何时取得客户端信息,HttpRequest获得服务端和客户端的详细信息
  2. AS升级3.1 编译报错:The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin.
  3. hadoop自定义类型注意问题
  4. [论文泛读]Web服务组合综述相关论文1(3篇)
  5. python错误提示库没有注册_SpringBoot实现登录注册常见问题解决方案
  6. Python——数组(列表)的基本操作
  7. ubuntu执行python脚本_在启动时运行python脚本作为守护进程(Ubuntu)
  8. paip.提升用户体验以及效率----编程语言趋势以及第四代语言4GL
  9. Windows 启动jar程序
  10. C语言/c++:实验报错[Error] ld returned 1 exit status的解决方案
  11. numpy.pad对图片进行填充
  12. 优盘里文件夹变成html,U盘里的文件全部变成了快捷方式,怎么破?
  13. 控制搜索引擎蜘蛛的爬行收录
  14. python根据掩模坐标提取数据
  15. 程序员,到底要不要去外包公司?
  16. python的pandas库题1
  17. python机器人编程教程入门_机器人操作系统(ROS)入门必备:机器人编程一学就会...
  18. (.*?)匹配的是什么
  19. 元宵节营销活动策划,轻松拿下用户
  20. scrapy 小项目——爬取豆瓣排行榜250

热门文章

  1. NFS共享储文件系统
  2. 超图学习之-数据导入
  3. simulink仿真及代码生成技术入门到精通_行星排混动从入门到精通(结构)
  4. 六元均匀直线阵的各元间距为_微波技术与天线复习题
  5. 一起来做NES开发(2)-反汇编
  6. 丰炜(VIGOR)PLC感受:
  7. html驱动怎么安装,打印机驱动安装方法
  8. 复苏的魔女服务器维护中,《复苏的魔女》「初醒」测试定档10.15,详细新手指南奉上!...
  9. 前端base64发给后端图片太大怎么办
  10. android 按钮带图标 阴影_Android实现图片添加阴影效果的2种方法