Click here to Skip to main content
6,594,088 members and growing! (17,072 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » General     Intermediate License: The Code Project Open License (CPOL)

Adding Glass Effect to WPF using Attached Properties

By rudigrobler

An example of how to add glass effect to your WPF applications using attached properties with only 1 line of code!!!
C# (C# 1.0, C# 2.0, C# 3.0), Windows (WinXP, Vista), WPF, Dev, Design
Posted:10 Jan 2008
Updated:28 Jan 2008
Views:38,330
Bookmarked:38 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 4.35 Rating: 4.35 out of 5

1

2
1 vote, 10.0%
3
3 votes, 30.0%
4
6 votes, 60.0%
5

Notes

A more detailed explanation of attached properties can be found here.

Introduction

After reviewing Blendables, I decided to investigate how difficult it would be to also allow a window to turn on its glass effect using attached properties. (The review of Blendables can be found here.)

Attached properties are used all over WPF (DockPanel.Dock, Canvas.Left, etc.) but they also have the potential of simplifying loads of other tasks (setting glass effects on Windows, adding drag & drop functionality to ListBoxes, enabling spell checking on controls).

I want to be able to turn on the Aero glass effect by just one line of code:

<Window x:Class="GlassEffectDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace: GlassEffectDemo"
    src:GlassEffect.IsEnabled="True"
    Title="GlassEffect demo" Height="300" Width="300">
    <Grid>
    </Grid>
</Window>  

Aero Glass Effect

I am not going into too much detail on how to enable the glass effect. (It has been covered in great detail on the Web already.) This article will focus more on how to use attached properties to turn the glass effect on!

Here is the definition of my attached property IsEnabled:

public static readonly DependencyProperty IsEnabledProperty =
    DependencyProperty.RegisterAttached("IsEnabled",
    typeof(Boolean), typeof(GlassEffect),
        new FrameworkPropertyMetadata(OnIsEnabledChanged));

public static void SetIsEnabled(DependencyObject element, Boolean value)
{
    element.SetValue(IsEnabledProperty, value);
}

public static Boolean GetIsEnabled(DependencyObject element)
{
    return (Boolean)element.GetValue(IsEnabledProperty);
}

public static void OnIsEnabledChanged
    (DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
    if ((bool)args.NewValue == true)
    {
        try
        {
            Window wnd = (Window)obj;
            wnd.Loaded +=new RoutedEventHandler(wnd_Loaded);
        }
        catch (Exception)
        {
            //Attached property is not set for a Window, do nothing!!!
        }
    }
} 

It is important to notice here that all attached properties MUST be registered with the DependencyProperty.RegisterAttached and that they MUST also have a Get<PropertyName> and Set<PropertyName>!!!

The next step is to attach an event handler to alert me if the attached property changes. Inside this event handler, I can check if the attached property is changed to True or False (IsEnabled).

If IsEnabled is changed to true, then I add an event handler to the Windows Loaded event. Here is the Loaded event handler... All it does is add the glass effect:

 static void wnd_Loaded(object sender, RoutedEventArgs e)
{
    Window wnd = (Window)sender;
    Brush originalBackground = wnd.Background;
    wnd.Background = Brushes.Transparent;
    try {
        IntPtr mainWindowPtr = new WindowInteropHelper(wnd).Handle;
        HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
        mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);
        System.Drawing.Graphics desktop = 
            System.Drawing.Graphics.FromHwnd(mainWindowPtr);
        MARGINS margins = new MARGINS();
        margins.cxLeftWidth = -1;
        margins.cxRightWidth = -1;
        margins.cyTopHeight = -1;
        margins.cyBottomHeight = -1;
        DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
    }
    catch (DllNotFoundException)
    {
        wnd.Background = originalBackground;
    }
}                

Now the glass effect can be turned on by just using one line of code!!!

As always, please comment on how I can improve my articles and also rate this article (even if you thought it was utterly useless!)

History

  • 10 January 2008: Initial release

Links

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

rudigrobler


Member

Location: South Africa South Africa

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
GeneralDLL not found error PinmemberRahul598421:16 10 Feb '09  
GeneralDoes this work on XP? Pinmemberdawmail33311:51 8 Feb '08  
GeneralRe: Does this work on XP? Pinmemberrudigrobler4:47 23 Mar '08  
GeneralRe: Does this work on XP? Pinmemberkapil bhavsar21:39 4 May '08  
GeneralRe: Does this work on XP? Pinmemberro_xxx0:11 5 May '08  
GeneralRe: Does this work on XP? Pinmemberrudigrobler0:25 5 May '08  
Generalhi PinmemberRalph_ai2x20:15 5 Feb '08  
GeneralRe: hi Pinmemberrudigrobler4:48 23 Mar '08  
GeneralCool.... Pinmembermarlongrech2:08 11 Jan '08  
GeneralRe: Cool.... Pinmemberrudigrobler2:28 11 Jan '08  
GeneralRe: Cool.... Pinmembermarlongrech2:35 11 Jan '08  
Generalnice article on the use of DPs PinmvpSacha Barber5:28 10 Jan '08  
GeneralRe: nice article on the use of DPs Pinmemberrudigrobler5:43 10 Jan '08  
GeneralRe: nice article on the use of DPs PinmvpSacha Barber6:17 10 Jan '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Jan 2008
Editor: Deeksha Shenoy
Copyright 2008 by rudigrobler
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project