Introduction

As is known, the first noticeable thing when moving from Windows XP to Windows Vista or Windows 7 is the enhanced graphical user interface, and if your hardware is good enough to support the Windows Aero Theme, you'll get great looks for the window title bars (see the figure below):

But if this theme is not enabled due to lack of good enough hardware, or is manually disabled by the user, the window titles will appear like this:

And here, we are going to get some control of this feature (the Glass Effect of the window titles and frames).

Window Desktop Manager

In the old days of Windows XP and earlier, each window was responsible to draw directly to the screen or the primary display device, but starting from Windows Vista, this is no longer happening, and a new feature has been introduced. This feature, called the Window Desktop Manager, is a service responsible for Desktop Composition that changes the way applications display pixels on the screen, so all the drawing of the individual windows will be redirected into an off-screen surface in the video memory, then this surface rendered into a desktop image and displayed on the screen.

This feature of desktop compositing enables the developers of the Operating System to add many different visual effects to the desktop windows such as:

3D animations

3D flip of Desktop windows

Glass Effect of window title bars and frames (the scope of this article)

Interactive thumbnails on the Task Bar

etc...

This new feature comes with a rich set of APIs that can be called to manage, control, and make use of the new possible visual effects available in these Operating Systems, and here we are going to use some of them (actually just two of the WDM API functions):

DwmIsCompositionEnabled()

DwmExtendFrameIntoClientArea()

Note: You can find the Windows Desktop Manager as a service (or process) called wdm.exe in the [Processes Tab] of the Windows Task Manager.

Windows Glass (Blur) Effect

The Glass or Blur Effect is an effect of the title bars and frame borders of the windows that display the items behind these title bars and frame borders in a semi-transparent way (see the Recycle Bin icon indicated by the arrow):

Actually, it is not just a transparency, but it displays the objects behind as blurry and just like what a glass does (and that is why it is known as the Glass Effect). This feature is cool and gives our windows a great look (I still remember the question of some friend when he saw a snapshot of a small program that I made previously "How did you do this effect of the title bar?" he said. And my answer was: "I did nothing! This is done by the Operating System itself and this is how the window titles look like on Windows Vista!") .... He was shocked just because he had not seen Windows Vista before. Anyhow, we are going here to add some control to this Glass Effect of the windows and make it look better and as we need.

Our Glass Effect Extender Library

What I'm going to introduce here is to add some flexibility to the Glass Effect and make it possible to:

Extend the area of the glass effect to any side of the form (make it not just limited by the title bar and frames).

Make it possible to show an image between the glass effect area and the other client area.

Draw texts on the glass with a glow around, just like the OS does on the title bars.

These three features are shown in the figure below:

Now, to use this library in your application, just add a reference to the library file rtaGlassEffectsLib.dll which can be found in the attachments of this article.

And then, simply declare a variable of type rtaGlassEffect as follows:

' Declare a variable of our effect that can be used later.Dim glass AsNew rtaGlassEffectsLib.rtaGlassEffect
' Set the size of glass effect from the top to 100 pixels.
glass.TopBarSize = 100' Show the effect on the form.
glass.ShowEffect(Me)

Now, if you have a Label called Label1 and you want to draw it with glowing text on a glass effect area of 60 pixels from the bottom of the form, just locate the Label on the position that it should be displayed at and use the following code:

Again, if you have a Label called Label1 and a PictureBox that contains an image PictureBox1 that should be displayed on a form with a glass effect so that its width from the left side of the form is 120 pixels and from the top side is 60 pixels, just put the Label and PictureBox on the locations you want and use the following code:

Shared Methods of our Library

Our object rtaGlassEffect contains some shared methods that can be used in addition to the already used non-shared method in previous examples: ShowEffect(). The first shared method is DrawTextGlow(), which can be used to draw a text with a glow on the passed Graphics object on the specified location, and its code is as follows:

As shown above, this method uses the API function DrawThemeTextEx() to draw the glowing text to the off-screen surface and then copy it to the surface of the Graphics object passed to the method using the Windows Vista Visual Style Renderer.

The second shared method of our library is SetGlassEffect() that shows the glass effect on the form passed to the method with the required areas from the top, left, bottom, and right sides of the form:

From the code, you can see that this method uses a property called GlassEnabled to check if the glass effect is supported and enabled on the Operating System before using the API function DwmExtendFrameIntoClientArea() to extend the effect of glass to the client area of the form.

Properties of the Library

Our library contains several properties holding the options of our glass effect, and I'm going to explain two of them here. The first one is the GlassEnabled that returns a boolean value indicating whether the Glass Effect is supported and enabled by the system or not. This property uses the API function DwmIsCompositionEnabled() to check this feature of the system, as follows:

Another property of our library is UseHandCursorOnTitle, that can be used to determine if the hand cursor should be displayed or not while the mouse is over the top area of the glass effect to show that the form can be dragged by hand.

Events Added to the Library

As you may have noticed, the GlassEnabled property is a great one for testing if this feature is enabled by your system and then start using our library and call ShowEffect() function to show the glass effect, Now suppose the user has disabled/enabled the Aero Theme while your application was running !! This may cause a problem in the appearance of your application.

To solve this problem, two events were added to the library to keep track of the changes happening to the system while your application is running, these two events are GlassEffectEnabled event and GlassEffectDisabled.

Now for people who like to know how stuff works .. I can tell that these events were implemented by monitoring the messages coming from the system to the windows to our application looking for the WM_SYSCOLORCHANGE message that indicates that the system colors have been changed. So by doing a quick compare between the state of the GlassEnabled property before and after this message, we can decide whether this feature has been enabled or not.

For doing this, I make use of the NativeWindow class for hooking the messages of the system as follows: