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


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


    有不少示例介绍了如何将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(
    thisnew 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版本
  • 相关阅读:
    SpringCloud项目中使用Nacos作为配置中心
    SpringCloud项目中使用Nacos作为注册中心
    idea创建maven多模块Spring Boot项目
    Java代码中对IP进行白名单验证
    idea配置jdk
    win10配置jdk1.8环境变量
    shell 提示符样式设置
    整数转换成中文读法的字符串
    比较三段式软件版本号大小
    Windows7安装 nginx+php 后访问.php文件出现 “No input file specified.” 的解决办法
  • 原文地址:https://www.cnblogs.com/zhouyinhui/p/785920.html
Copyright © 2020-2023  润新知