zoukankan      html  css  js  c++  java
  • 在Winform和WPF中启用Aero效果

    API导入:

    View Code
     public class DwmApi
        {
            
            [DllImport("dwmapi.dll", PreserveSig = false)]
            public static extern void DwmEnableBlurBehindWindow(IntPtr hWnd, DWM_BLURBEHIND pBlurBehind);
    
            /// <summary>
            /// 启用Areo效果
            /// </summary>
            /// <param name="hWnd"></param>
            /// <param name="pMargins"></param>
            [DllImport("dwmapi.dll", PreserveSig = false)]
            public static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, MARGINS pMargins);
    
            /// <summary>
            /// 系统是否支持Areo效果
            /// </summary>
            /// <returns></returns>
            [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;
                }
            }
        }

    在Winform中使用方法:

    直接在窗体的onload事件中加入

    if (DwmApi.DwmIsCompositionEnabled())
    {
    DwmApi.DwmExtendFrameIntoClientArea(this.Handle, new DwmApi.MARGINS(0, 0, 0, 0));
    }

    并重载OnPaintBackground

    protected override void OnPaintBackground(PaintEventArgs e)
    {
    base.OnPaintBackground(e);
    if (DwmApi.DwmIsCompositionEnabled())
    {
    e.Graphics.Clear(Color.Black);
    }
    }

    在WPF中使用方法:

    WPF无法直接得到窗体句柄,可以使用下面辅助类

    View Code
        class AeroHelper
        {
            public static bool ExtendGlassFrame(Window window, Thickness margin)
            {
               
                if (!DwmApi.DwmIsCompositionEnabled())
                    return false;
                //WPF中不能直接得到Window句柄,要借助WindowInteropHelper
                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;
    
                DwmApi.MARGINS margins = new DwmApi.MARGINS((int)margin.Left, (int)margin.Top, (int)margin.Right, (int)margin.Bottom);
                DwmApi.DwmExtendFrameIntoClientArea(hwnd, margins);
    
                return true;
            }
        }

    在Loaded事件中加入 AeroHelper.ExtendGlassFrame(this, new Thickness(-1));就可以了

    代码下载

  • 相关阅读:
    JSON就是名值对 name/value pair
    AjaxXMLHttpRequest
    英语单词分类记
    委托和事件的理解
    用float设置主页的左右两边菜单
    OCS通讯路径
    测试用Word写Blog
    第一课 C#入门
    nginx虚拟目录设置 alias 和 root
    vsftp 出错,无法创建文件的解决方法
  • 原文地址:https://www.cnblogs.com/FlyCat/p/2707767.html
Copyright © 2011-2022 走看看