程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WPF中將Vista Aero效果擴展到整個窗口

WPF中將Vista Aero效果擴展到整個窗口

編輯:關於.NET

效果圖:

有不少示例介紹了如何將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;
      }
    }
  }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved