Tweak DWM from your programs, Part 2
Published August 27th, 2008 in Windows VistaFollowing the first part of the Tweak DWM from your programs series, we are going to explore the actual usage of the Desktop Window Manager API in a Windows Forms-based program. The test application–included in the source package–also makes use of
GlassBar, an extension to the existing control set.
Using GlassLib in your program
GlassLib is going to be main focus of this part of the series. It supersedes
DwmWrapper by implementing a framework around
GlassBar which can be used as a complement to your visually-rich forms. In order to use this library in your project, you have to do the following:
- Include a) a reference to the DLL in your project from Visual Studio: Project menu => Add reference, or b) the actual code files to your C# project (adding them as links (shortcuts) as opposed to copying them is more suitable if you wish include the files in multiple projects and/or wish to make changes to the code yourself).
- Remember that all of the functionality lies in the
GlassLib namespace. You can use the using or Imports clause in the beginning of your C# or VB.NET code file, respectively, or the global imports if you code exclusively in VB.NET.
Exploring the GlassLib Test app
Let’s begin exploring the framework by opening the project file for
GlassLibTest. Included in the GlassLib solution, it depends on the
GlassLib project reference, which on its part has all the
DwmWrapper code. Open the
frmMain.cs file which is the form which is to be executed right at start up. As described in the first part, the first thing the class constructor public frmMain() should do is to set Dwm.ThrowExceptionTypes to DwmExceptionTypes.None in order to avoid any exceptions from being raised when DWM is not enabled or available on the OS, or if any function failed. The next thing the program does is to enable dragging from the Glass-extended area.
Dwm.Glass[this].DragExtendedArea = true; is useful if you want the extended area to act as the caption bar does, like in Windows Media Player. This is what the
GlassBar toolbar does as well (more on this later in this part).
Switch to Designer mode (View => Designer) and you can see the layout of the form: the usual Windows Forms buttons, combo and text boxes et cetera, but also
GlassBar. To add the latter to your own forms, open the Toolbox (View => Toolbox) and drag it to your form (see figure on right). The toolbar will automatically dock to the bottom of it and extend Glass from this side of the form. You are free to dock it to the whole form (or container for that matter) or one of the other sides. Because DWM only supports Glass on top-level windows, in designer mode
GlassBar falls back to UXTheme API in order to get the theme-specific background image or brush which is suitable for all non-Aero Glass themes such as Aero Basic, Luna (on Windows XP) or Windows Classic on any supported Windows version. On top of this, you can choose to write your own
GlassBarRenderer.
This feature provides two key benefits to your application’s design:
- For Windows Vista systems and future OS with support for Glass: rich, seamless Glass UI
- For Windows Vista systems where DWM is disabled or unavailable, and all previous Windows releases: seamless UI deriving from the appropriate Windows theme, including custom themes created with software such as WindowBlinds.
Managing the GlassBar items
Adding your items to GlassBar is quite easy. While in Design Mode, make sure that the Properties pane is visible (View menu => Properties Window to invoke it), select Items and expand the Collection Editor. You can add, remove and rearrange the items. For each one of them you can customize their:
| Used in code only (does not appear in UI) | ||
| Enables/disables the item from the UI | ||
| Preferably translucent PNG image 25×25 pixels in size | ||
| Denotes whether the item is a separator (its Icon will be disregarded) | ||
| Image, useful for ‘hover’ effects | ||
| Alternative text shown when the item is hovered | ||
| Shows/hides the item |
You can of course manage items programmatically using the
Items property, for example adding one: Image img = Image.FromFile(“someFile.png”); GlassBarItem gbi = new GlassBarItem(“Sample Text” + i, img); glassBar.Items.Add(gbi); When the logic of your program requires an item be disabled or hidden, e.g. an unavailable feature, you can use
Enabled or
Visible to prevent users from clicking it.
Custom renderers
Internally GlassBar defines a standard renderer which on Windows Vista with Aero Glass enables draws on a translucent surface and otherwise falls back to the system-defined theme. If you wish to code a custom renderer for your needs, what you have to do is:
- Create a class deriving from (inheriting) from one of the following classes:
GlassLib.Rendering.DefaultGlassBarRenderer if you wish to customize only specific parts of the default renderer (images, background etc)
GlassLib.Rendering.GlassBarRenderer if you wish to build one from the ground up. This requires you to implement all of the methods and properties and is unlikely to be your choice.
- Decide which functions or images you would like to change. You can assign any image to
MainPiece, RightSecondaryStates, AnimationStrip etc. - The
DrawBarBackground function is called every time the UI changes and needs to be redrawn. It is here where you can change most of the behavior of the default renderer and implement your own features. - Assign an instance of your custom renderer to
GlassBar
- glassBar.Renderer = new CustomRenderer(); or to revert to default:
- glassBar.Renderer = new GlassLib.Rendering.DefaultGlassBarRenderer();
The test app
There is a test program added to the package,
GlassLibTest, whose code and compiled binary you can explore and experiment with GlassBar and DWM. If you have any comments, questions, wishes or just an opinion, I would be happy to hear it.
Search
About |

Stanimir Stoyanov is a programmer, Microsoft MVP, and Windows enthusiast. Read More...
He's currently working on an array of projects using Visual Studio 2010 on Windows 7.
Latest
- Blocking unwanted advertisements and malware with a HOSTS file
- Multithreading with Windows Forms in C#
- Честита Коледа (и поздравления на спечелилите)!
- A simple backup solution using ImageX, a Windows Imaging tool
- Soon: MSDN Ultimate Subscriptions Giveaway
- Decoding FLAC audio files in C#
- Inline Tweet Translator
- Resizing forms while keeping aspect ratio
- Encoding uncompressed audio with FLAC in C#
- Indicating progress in console windows

One Response to “Tweak DWM from your programs, Part 2”
Please Wait
Leave a Reply