Glass on WPF

Complete sample available on MSDN: Extend Glass Frame into a WPF application

Enabling glass and transparency

First of all, you must tell the composition renderer to use a black background for your application (same goes for WinForms) and then enable the glass border. Since the window will be drawn on top of your black composition background, you'll also need to tell WPF to use a transparent background (otherwise, the white background of the window will simply be rendered on top of the glass). We also define an event handler for the Loaded event:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded" Background="Transparent">


The event handler creates a WindowInteropHelper in order to get the window's Handle. This is needed to set the composition target's background to black and to enable the "glass sheet" effect on the whole window.

private void Window_Loaded(object sender, RoutedEventArgs e) {
	IntPtr hwnd = new WindowInteropHelper(this).Handle;

	//Set backgroung to black
	HwndSource source = HwndSource.FromHwnd(hwnd);
	source.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);

	//Set glass
	VistaControls.DWM.DWMManager.EnableGlassSheet(hwnd);
}


Since WPF controls are rendered with alpha blending enabled (whereas WinForms controls are not), you can put controls on glass areas or fade the opacity of certain elements:

glass_wpf1.png

Partial glass effect

If you prefer the glass not to be over the whole window and to extend on only one side (or two), you can simply change the call to the Windows Vista Controls library:

//Set glass
VistaControls.DWM.DWMManager.EnableGlassFrame(hwnd, new VistaControls.DWM.Margins(50, 0, 0, 0));


The result is this:

glass_wpf2.png

The problem is, we told WPF to draw a transparent background and that's what is being done: the window has no background and the only "painted" area is the actual extended glass border. In order to draw the background correctly, you'll need to draw it manually on the areas which are not transparent:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="50" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    
    <Grid Grid.Column="1">
        <Rectangle Fill="{x:Static SystemColors.WindowBrush}" />
        
        <!-- CONTENT OF YOUR WINDOW -->
    </Grid>
</Grid>


In this case we simply define a Grid with columns and rows matching the structure of the window's glass and then draw a rectangle on the non-glassy area (using the default WindowBrush as color). This is the result:

glass_wpf3.png

Last edited Feb 28, 2008 at 11:37 AM by Lck, version 2

Comments

Lck Dec 27, 2013 at 11:17 AM 
The namespace changed from VistaControls to WindowsFormsAero in the more recent versions.

SridharChanti Dec 27, 2013 at 10:03 AM 
I'm Getting this error
Error 1: The name 'VistaControls' does not exist in the current context

VistaControls.DWM.DWMManager.EnableGlassSheet(hwnd);
........
What to do?