Flip 3DWith Windows Vista, Microsoft introduced a whole new window manager which provides a better experience to the user and an independent graphics surface to us developers by taking advantage of graphics acceleration hardware (your graphics card or integrated GPU).

GlassLib is a wrapper around the Desktop Window Manager Application Programming Interface and is created in order to control its state, change colorization, or change the rendering policy of windows from your Windows Forms and .NET 3 Presentation Foundation programs.

Getting Started

Windows Forms and Windows Presentation Foundation will be referred to in this post as WF and WPF respectively.

You can download the full GlassLib source code and the included test projects from here. It is written in C# (as are the code samples in this post). That being said, you can use it in projects of any other .NET language — you can include the compiled libraries as references. Otherwise (if you code in C#), you also have the option to include the files in your project and not distribute a separate library. The solution is for use in Visual Studio 2005 but is also compatible with version 2008.

The whole framework is located under the namespace_GlassLib namespace and is the source to three main types of projects:

  • projectDwmWrapper is a .NET wrapper around the DWM API, which can be compiled in the following flavors:
    • No dependency on WF or WPF. It can be used to avoid DLL clutter when coding command-line applications to manipulate the DWM.
    • Solely WF or WPF
    • Both for mixed-framework projects.
  • projectGlassLib is a WF-powered layer above projectDwmWrapper and provides a set of classes, including the classGlassBar control for use in your forms
  • projectThe Test Projects you can use to try the various features that the two other projects have. They will be used here ad hoc.

DWM test appHere we will explore the architecture of the wrapper, its core functionality and application in Windows Forms programs.

Microsoft provides an API for DWM in the equivocal DwmApi library. Encapsulating it for easier use in .NET programs is the wrapper’s main purpose.

Since all necessary classes are located under the namespace_GlassLib namespace, in order to avoid writing it every time, I would suggest that you insert it in your C# or VB code file’s “imports” or, if you are working exclusively with VB, in your project’s “Imported Namespaces” list in order to avoid doing the first as well.

The most important class is classDwm and can be used to control virtually any aspect of DWM as long as the public API allows it. The various features are available via the following properties of classDwm:

propertyBlur Enables/disables the black background transition when a window (not necessarily your window) is maximized, sets custom blur region
propertyColorization Gets/sets the Aero Glass colorization
propertyComposition Enable/disable DWM, check if running and if the system is DWM-capable
propertyEvents Used to set up notifications for colorization, composition, non-client rendering or window-maximized changes
propertyFlip3D Start/Stop Windows Flip3D or Flip
propertyGlass Enable/disable Glass on a given form (via its instance, e.g. this) or window (via its handle/hWnd, an classIntPtr)
propertyNonClientArea For advanced users only, gets or sets how DWM is handing the painting of a given form or window (see propertyGlass above)

Using the Wrapper

Note that DWM is only available on Windows Vista, so I would suggest that you select how to handle the wrapper’s behavior on earlier operating systems. Otherwise, exceptions will be raised which you will have to catch.

The first option is to limit the exceptions the wrapper will raise by setting propertyDwm.ThrowExceptionTypes to one of the available optionsDwmExceptionTypes

propertyNone
propertyUnsupportedFeatures
propertyDesktopWindowManager

Raising no exception will most likely suit your needs best so adjust this property before you make any other changes to DWM, preferably at program startup or in the form’s constructor.

Dwm.ThrowExceptionTypes = DwmExceptionTypes.None;

Now you can make any changes you want to your form’s state in the DWM, for example enable Glass–it is as easy as writing

Dwm.Glass[this] = new DwmMargins(100, 4, 8, 20);

where this is the instance of the form (you can replace this with any other form instance you would like) and the margins are defined as left-right-top-bottom. You can use propertyDwmMargins.EntireWindow instead to extend glass in the whole window client area, like Mobility Center does in Windows Vista.

Restoring the default borders is done by calling the methodRestore method.

Dwm.Glass[this].Restore();

DWM colorizationsReceiving notifications on the global DWM state or on your forms’ is important in order to know how you should handle painting backgrounds or the non-client area. You can choose to get notified when the Aero Glass colorization is changed or DWM starts up or shuts down. In GlassLib this is straightforward: each form that you wish to be notification-aware, GlassLib will include in its list and you have to attach your handlers to the colorization, composition, non-client rendering or window-maximized changes.

Dwm.Events[this].AddHandlers(); // Notifying GlassLib…

// Add our handlers

Dwm.Events[this].CompositionChanged += new DwmEventHandler(OnCompositionChanged);
Dwm.Events[this].ColorizationChanged += new DwmEventHandler(OnColorizationChanged);

private void OnCompositionChanged(object sender, DwmEventArgs e)
{
// Update the UI on composition change (extend glass if DWM is enabled, otherwise restore)
if (Dwm.Glass[this].Enabled)
Dwm.Glass[this].Margins = new DwmMargins(100, 4, 8, 20);
else
Dwm.Glass[this].Restore();
}

private void OnColorizationChanged(object sender, DwmEventArgs e)
{
MessageBox.Show(e.Colorization.ToString());
}

Flip 3DAlthough the public DWM API does not make it easy for one to change the colorization programmatically, DwmWrapper exposes this functionality through the propertyDwm.Colorization property. Controlling the Desktop Window Manager is also as easy as setting propertyDwm.Composition.Enabled to the desired value, either true or false. Other useful “one-liners” include invoking Windows Flip and Flip 3D: methodDwm.Flip3D.EnterAltTab(); and methodDwm.Flip3D.Enter();, respectively.

You are free to use GlassLib in your freeware programs, but please at least acknowledge it in your release notes/about dialog/website. In case your program is commercial/shareware, I would appreciate it if you could contact me beforehand. Thanks.


2 Responses to “GlassLib for .NET: The Wrapper”  

  1. 1 anonymous

    Looks great, will definately try it!

  2. 2 fuat

    hi nice work.. i have a small problem :) on the demo, add a WebBrowser to the form and set its Url to something like google, and then run it, it should navigate to google. The scroll bars doesn’t draw properly when this ‘glass’ theme is in use. I’ve tried many tutorials on applying aero glass but the WebBrowser scroll-bars gives the same result. Is this a webbrowser issue? Any thoughts?

Leave a Reply