GlassLib is a wrapper library for the Desktop Window Manager API and is to be used by developers which would like their applications to take advantage of .NET Framework and Windows Presentation Foundation in particular.

It can be used to control the Desktop Composition due to application requirements, create and reference Live Thumbnails of arbitrary windows, manipulate the Glass client area of windows and receive events which occur for them (Figure). Developers who target their applications for Far East users can also use the wrapper library to control the flow direction of controls and caption bar.

While the library wraps a big portion of the API exposed by DWM, the timing and performance counter part is to follow up shortly.

Documentation and a test application are provided. The usage of the wrapper library is pretty much straight-forward. Following is a list of several key facts:

  • Add a reference to the DwmWrapper.dll library to your project (from the Solution Explorer, select your project’s node, right click it and select Add Reference. From the new dialog box, browse the library file). Please note that the library is compiled against the .NET Framework 2.0 therefore it can only be used in projects compiled against it, or a newer version.
  • Include the GlassLib namespace so that the Dwm-related classes can be accessed more easily.
  • When used in a System.Windows.Window, override the OnSourceInitialized method and then set up the event handlers.
  • Per-window options can be set using the Dwm.Glass, Blur, Events, NonClientArea and Flip3D. This is available by using the indexer (default property) of these properties. The value passed to the indexer can be an IntPtr - handle to a window or a System.Windows.Media.Visual whose window handle will be manipulated. E.g:
    • In VB .NET: Dwm.Glass(Me).Enabled = False
    • In C# .NET: Dwm.Glass[this].Enabled = false;
      • Note: this and Me refer to the calling instance, which is a System.Windows.Window in the example.
  • The Dwm* structures have implicit operators. This allows for casting Windows Presentation Foundation Rect and Thickness implicitely to DwmRect and DwmMargins, respectively. For instance, the following expression will be valid:
    • Thickness foo = new Thickness(5, 30, 5, 20);
      // Assign foo to a Visual’s margin.
      Dwm.Glass[this].Margins = foo; // foo is implicitely casted to DwmMargins.?????

GlassLib Test Application GlassLib Test Application - Right-To-Left Layout

If you have any questions, suggestions or would like to report an issue, please feel free to use the Contact form.

Written in C# .NET. Binaries and the sources of a test application are available here; only binaries.


3 Responses to “GlassLib for WPF”  

  1. 1 Li Bing

    Dear Stanimir Stoyanov,

    I tried your GlassLib in C# in the following code.

    public partial class Window1 : Window
    public Window1()
    Thickness foo = new Thickness(-1, 30, 5, 20);
    Dwm.Glass[this].Enabled = true;
    Dwm.Glass[this].Margins = foo;

    But it does not work. My application is crashed and it could not be debugged. What’s the reason?

    Thanks so much!
    Li Bing

  2. 2 Mr.X

    Li Bing, try to do it there
    protected override void OnSourceInitialized(EventArgs e)
    I’ve read somewhere, this must be done before something and blah blah…this works for me.

    This is just great lib! Thx very much!

  3. 3 christoffer


    I love your library and have been using it in my new music application. However, I have one concern: when the computer is locked, resumes from hibernation/sleep or I play a game, the glass area goes black.

    What should I do?

Leave a Reply