Extend Vista Aero effects to the entire window in WPF

Source: Internet
Author: User
Tags bool extend

Effect Chart:

There are a number of examples that describe how to extend the Vista Aero effect to the entire window, but most of it is for Windows Form applications, not WPF (that is, the former is for the form class, the latter is for the window class), such as http:// Www.cnblogs.com/zhouyinhui/archive/2007/05/30/765416.html

In fact, it's similar to calling Dwmapi, except that the window class doesn't provide us with a handle directly, and we need code like this to find its handle:

INTPTR hwnd = new System.Windows.Interop.WindowInteropHelper (window). Handle;

Then set the window's background to transparent:

Window. Background = brushes.transparent;

Hwndsource.fromhwnd (HWND). Compositiontarget.backgroundcolor = colors.transparent; Last Call

Dwmapi.dwmextendframeintoclientarea (hwnd, margins);

Note that we should call our function after the window has been displayed (after sourceinitialized) or it will throw an exception.

Reference code:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.