[Update - 9/13/06]  This seems to not work using RC1 - using DwmEnableBlurBehindWindow seems to work fine creating areas of glass, but DwmExtendFrameIntoClientArea does not - you get nothing but a black rectangle.   I'm trying to figure out why and I'll create another post if I do...  if you do, please feel free to comment here or point me to your posting! 

 

I've seen a fair number of questions on using Glass in Vista forms, and I've seen a few answers as well.  However, the answers I've seen involve using Windows.Forms transparent forms support [1].  There's a big downside to this- when you click a "glassy" area the mouse click is never sent to your form, and is instead passed to whatever's behind it.  Not intuitive!

Also, there's more to glass in Vista than allowing the border to creep in - you can have the whole window look like a sheet of glass, or you can even add little interior bits to be glass.  Why not take advantage of all of it?

This sample does that, and provides a pre-build assembly you can use as well.  If you want to figure out what it's doing, read below and follow along in the source.  If you just want to use it to get glass forms, in c# or vb.net, then :

Get the Zip file

  1. Create a new Windows Forms project
  2. Reference the "GlassForms.dll" assembly.
  3. Create a new form and open it in code view (or open Form1.cs in code view)
  4. Change the class it inherits from from "Form" to "GlassForms.GlassForm"
  5. Mess with your form's new properties "GlassFrameInset", "ShowAsSheetOfGlass", and "FullClientBlur"

You can do the last step in code as well. In code, you can also call AddBlurRect and RemoveBlurRect methods to add blurry areas to the interior of your form.

Some important notes!

Windows Forms doesn't really support a non-painted tranparent background to the Form (without specifying TransparancyKey, which has the "click-through" problem).  So any controls placed on a glassy portion of your form will probably look funky.  No, I don't know how to solve this yet.  Yes, I am working on it (but time is limited, so I'm not working on it much).  Your best bet is to draw any UI on the glass yourself.  It's a pain, I know.  For this reason as well, "ShowAsSheetOfGlass" and "FullClientBlur" are of extremely limited usefulness.

Text on Glass

I'd like to add support for DrawThemeTextEx, but haven't.  What that would do is allow you to draw text that has that neat white glow around it - making it easier to read on glass against any background.  Coming some day.

What's really going on

It's actually pretty easy.  Look into NativeMethods, and notice three important calls: DwmIsCompositionEnabled(..),  DwmExtendFrameIntoClientArea(...), and DwmEnableBlurBehindWindow(...).  That's it!  really.

The tricky part in Windows forms is painting the area black you set glass to be in.  I do that by keeping a Region around that I fill with black in the OnPaint of the base class.

What, you thought it would be more complicated?  It's really not.  The dwmapi calls are quite straightforward, and with a bit of p/invoke knowledge, quite easy to use.  I hope to find a solution to the windows forms "black paint" problem (see the important notes above), but haven't yet. 

[1] Thereby setting the WS_LAYERED attribute.