MorFans Dev
折腾 - 开发 - 分享

C# 实现 WIN7毛玻璃窗口特效 Win AeroGlass

C# 实现 WIN7毛玻璃窗口特效 Win AeroGlass

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));
        }

大功告成,效果如下
[v_act]win7:
QQ图片20160621232233

QQ图片20160621232139[/v_act]
[v_act]win10:
QQ图片20160621232147[/v_act]

赞赏
魔帆博客,版权所有 | 如未注明,均为原创
本站均采用 BY-NC-ND 协议 (署名-非商业性使用-禁止演绎) 进行授权。
转载请注明来自本站文章:C# 实现 WIN7毛玻璃窗口特效 Win AeroGlass(https://www.morfans.cn/archives/1742)

野小新

文章作者

野小新很野~

发表回复

textsms
account_circle
email

  • FOR日曜

    写点汉字吧,博主外语很捉急!You should type some Chinese word!

    7年前 回复
  • Gorange

    DWM IntoClientArea?
    写点汉字吧,博主外语很捉急!You should type some Chinese word!

    8年前 回复
  • 求下载

    8年前 回复
  • 如果有一天,我潇洒死去,请记得,我来过这里!

    8年前 回复

C# 实现 WIN7毛玻璃窗口特效 Win AeroGlass
Windows 7 操作系统默认具有一款玻璃效果主题(Aero Glass)。如果选择了该款主题,所有的应用程序标题栏都会处于玻璃透明效果(如下图)。这个功能是由Desktop Window Manager(DWM)服…
扫描二维码继续阅读
2016-06-21