Aero Glass WPF Window
Aero Glass WPF Window
Join the DZone community and get the full member experience.Join For Free
Using Windows Vista and Windows 7 for a while, I mentioned the fact that some applications implement the glass UI in a very interesting manner. If Aero is enabled, the window title bar looks like this:
It is a semi-transparent unit, through which the user is able to partially see what is located behind it, be that a static window, a movie or whatnot. This is made possible with the composite desktop. Let’s start with a bit of theory.
The new Windows Vista/7 UI is built around the Desktop Window Manager. Although DWM works differently in Windows Vista and 7, the basic idea behind it remains the same. Each program has a separate buffer to which it writes. Later on, DWM collects the data from the separated buffers to build the final window image and integrate it with the existing ones. This is exactly why it is easier to implement transparency and other visual effects across multiple applications when using DWM rather than using the old-style Windows XP (and prior) window stacking mechanism (that can also be triggered in Windows Vista/7 if Aero is disabled).
With this being said, the DWM allows a window to be customized, while rebuilding the entire window buffer, so that other windows can be properly rendered depending on the state of other windows. For more information on the internals of DWM and its use in this specific case, I would highly recommend reading this article.
Now, let’s get to the code. First of all, there is currently no managed library that is distributed with .NET Framework that would allow direct access to DWM, therefore Win32 API calls will be invoked directly. To start, I added these declarations to my code:
public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMargins);
public static extern bool DwmIsCompositionEnabled();
The first function is actually performing the glass UI extension in the client area (window) and the second function is verifying whether the desktop composition is enabled. Worth mentioning is the fact that a developer can directly use DwmExtendFrameIntoClientArea without DwmIsCompositionEnabled. The application will work fine, as long as it runs on Windows Vista or Windows 7. But once it is launched on a machine with an older version of Windows, it will cause an error. Therefore, it is worth checking the system major version and whether desktop composition is enabled. Also an important note – even in Windows Vista or Windows 7 the desktop composition might be disabled (with Aero disabled – working with the Basic theme).
I need an additional class declaration, as you see from the code I showed above – the MARGINS class. In fact, it is an implementation of the MARGINS struct required for the API call. Here it is:
public class MARGINS
public int cxLeftWidth, cxRightWidth,
In the Window_Loaded event handler I am using the following code:
if (Environment.OSVersion.Version.Major >= 6 && DwmIsCompositionEnabled())
// Get the current window handle
IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;
HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent;
this.Background = Brushes.Transparent;
// Set the proper margins for the extended glass part
MARGINS margins = new MARGINS();
margins.cxLeftWidth = -1;
margins.cxRightWidth = -1;
margins.cyTopHeight = -1;
margins.cyBottomHeight = -1;
int result = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
if (result < 0)
MessageBox.Show("An error occured while extending the glass unit.");
First of all, I need to get the handle of the current window. In WPF this process is a bit different for those who developed using WinForms (there is no more this.Handle). Settign the background to transparent is required for the glass effect to work. Then, I am setting the margins for the glass effect, that is a required parameter and I cannot skip it. The result integer gets the return value for DwmExtendFrameIntoClientArea. If it is less than zero, this means that an error occurred when the function was executed.
I can also set the DwmExtendFrameIntoClientArea to void, so it won’t return a value, however for testing purposes, I recommend keeping an eye on that int.
Once you run the application, the end result should look like this:
The controls that will be inserted on the canvas won’t inherit the transparency, so if needed, the Opaque property should be adjusted for proper transparency level (the values range between 0 and 1).
Opinions expressed by DZone contributors are their own.