Playing with Glass windows forms in C#

I’ve mentioned before that I’m a little behind the current technology, in that I am still running Windows 7. Since Aero Glass fell by the wayside in favour of what looks like a mobile user interface, this may not be useful to anybody, but it is useful to  me.

Honestly, I’ve never even looked at any of the glass or other Desktop Window Management related features in Windows 7, but since work is slow at the moment, I decided to play around with it a little. I’d always assumed that the glass features would “just work”, being as it is a Windows system feature. But it turns out that you can’t have a form like the one in the screenshot shown below without a little effort.

Here’s a glass dialog against a light background:


And here it’s against a mostly dark background:


I revisited my tiny utility application that customizes the Windows logon background image.

I used code from the Windows API Code Pack to enable glass in a Windows Forms project. However, I didn’t want to include the whole code pack just for one small feature in such a small application (but I wanted to include the code), so I copied only the files I needed into a new class library I added to my solution, called DesktopWindowManager. It’s just the directory with that name from the API Code Pack source code, as well as whatever else was required so that it can compile.

I haven’t touched the code, but I did change a couple of namespace names, for the simple reason that I can not figure why a code pack should identify itself as a code pack. I always thought the name used was a really bad choice.

Using the code is as easy as the glass forms sample in the code pack download: just derive your form from their type that implements the glass features. What isn’t clear is how to create a form with a look like my example above… The actual glass design is somewhat short-sighted in that they only expect the glass part of the form to be around the outside.

There is a way to get the form to look like mine though, and it is really easy. Add a Panel to the form, and dock it to Fill the whole form. Then set its BackgroundColor to a colour – any that you like (and will not be in any of the controls you use or manually paint as I did when the selected image changes) and set the TransparencyKey of the form to that same colour.

In case that last part is not clear: When the whole form uses glass, any control you drop onto it will get the glass look. In other words, any control you drop onto it, especially a label, will look like crap. Normally you can only exclude one region from getting the glass look, but when you use the workaround explained above, you effectively get multiple regions excluded.

Bare in mind my comment that getting your form to look like this is as easy as deriving from the GlassForm type relates to using glass and having multiple regions excluded. I wasn’t considering that the most impressive part of the example form is probably the monitors image. I know we don’t all have Photoshop skills. Anyway, my monitors.psd file is included with everything else in the source code download.


About Jerome

I am a senior C# developer in Johannesburg, South Africa. I am also a recovering addict, who spent nearly eight years using methamphetamine. I write on my recovery blog about my lessons learned and sometimes give advice to others who have made similar mistakes, often from my viewpoint as an atheist, and I also write some C# programming articles on my programming blog.
This entry was posted in Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s