Click here to Skip to main content
14,931,069 members
Articles / Desktop Programming / WPF
Posted 10 Jan 2008


60 bookmarked

Adding Glass Effect to WPF using Attached Properties

Rate me:
Please Sign up or sign in to vote.
4.60/5 (17 votes)
28 Jan 2008CPOL2 min read
An example of how to add glass effect to your WPF applications using attached properties with only 1 line of code!!!


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


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:src="clr-namespace: GlassEffectDemo"
    Title="GlassEffect demo" Height="300" Width="300">

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 =
    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)
            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 = 
        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!)


  • 10 January 2008: Initial release



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


About the Author

South Africa South Africa
No Biography provided

Comments and Discussions

QuestionHow To Let The Color Of Glass Not Change Pin
iGod_eleven23-Nov-12 6:42
MemberiGod_eleven23-Nov-12 6:42 
QuestionError Pin
Sheetal Bilewar18-Feb-12 9:56
MemberSheetal Bilewar18-Feb-12 9:56 
GeneralStrange behaviour with WebBrowser Pin
Alexadvance22-Mar-10 6:51
MemberAlexadvance22-Mar-10 6:51 
GeneralDLL not found error Pin
Rahul598410-Feb-09 20:16
MemberRahul598410-Feb-09 20:16 
QuestionDoes this work on XP? Pin
dawmail3338-Feb-08 10:51
Memberdawmail3338-Feb-08 10:51 
AnswerRe: Does this work on XP? Pin
rudigrobler23-Mar-08 3:47
Memberrudigrobler23-Mar-08 3:47 
GeneralRe: Does this work on XP? Pin
kapil bhavsar4-May-08 20:39
Memberkapil bhavsar4-May-08 20:39 
AnswerRe: Does this work on XP? Pin
ro_xxx4-May-08 23:11
Memberro_xxx4-May-08 23:11 
GeneralRe: Does this work on XP? Pin
rudigrobler4-May-08 23:25
Memberrudigrobler4-May-08 23:25 
Generalhi Pin
Ralph_ai2x5-Feb-08 19:15
MemberRalph_ai2x5-Feb-08 19:15 
GeneralRe: hi Pin
rudigrobler23-Mar-08 3:48
Memberrudigrobler23-Mar-08 3:48 
GeneralCool.... Pin
marlongrech11-Jan-08 1:08
Membermarlongrech11-Jan-08 1:08 
GeneralRe: Cool.... Pin
rudigrobler11-Jan-08 1:28
Memberrudigrobler11-Jan-08 1:28 
GeneralRe: Cool.... Pin
marlongrech11-Jan-08 1:35
Membermarlongrech11-Jan-08 1:35 
Generalnice article on the use of DPs Pin
Sacha Barber10-Jan-08 4:28
mvaSacha Barber10-Jan-08 4:28 
GeneralRe: nice article on the use of DPs Pin
rudigrobler10-Jan-08 4:43
Memberrudigrobler10-Jan-08 4:43 
GeneralRe: nice article on the use of DPs Pin
Sacha Barber10-Jan-08 5:17
mvaSacha Barber10-Jan-08 5:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.