With 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.
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.
- DwmWrapper 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.
- GlassLib is a WF-powered layer above DwmWrapper and provides a set of classes, including the GlassBar control for use in your forms
- The Test Projects you can use to try the various features that the two other projects have. They will be used here ad hoc.
Here 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 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.
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.
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 DwmMargins.EntireWindow instead to extend glass in the whole window client area, like Mobility Center does in Windows Vista.
Receiving 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)
Dwm.Glass[this].Margins = new DwmMargins(100, 4, 8, 20);
private void OnColorizationChanged(object sender, DwmEventArgs e)
Although the public DWM API does not make it easy for one to change the colorization programmatically, DwmWrapper exposes this functionality through the Dwm.Colorization property. Controlling the Desktop Window Manager is also as easy as setting Dwm.Composition.Enabled to the desired value, either true or false. Other useful “one-liners” include invoking Windows Flip and Flip 3D: Dwm.Flip3D.EnterAltTab(); and Dwm.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.