KnowDotNet NetRefactor

Making a Form Transparent

by William Ryan
Print this Article Discuss in Forums

Let's face it, .NET is really cool, but creating a compelling UI takes more than a cool framework to program on.  One of the more interesting and relatively unknown features of .NET's Windows.Forms is the Opacity property.  By altering this, you can make your form move anywhere along the visibility spectrum, from invisible, to somewhat transparent, to completely opaque.

One of the annoying features of it though is that the property isn't an Int, surprisingly it's a Double.  So, the allowable values range from 0.00 to 1.00.  At 0.00 the form and all of its controls are totally invisible.  At 1.00 the form is completely opaque (the default)

So, just to get a feel for it, try the following code snippet.  Create a blank form and drag a button onto it.  Behind the button's event handler, add this code...

this.Opacity = 0.00;
Double d = 0.00;
  
while(d <= 1.00)<BR>    {
      
this.Opacity+= d;
       System.Threading.Thread.Sleep(500);
       d+=.1;

   }


This technique works great if you want to make a form see through so the users can look under it (I wrote a creen capture program that takes advantage of this technique) or if you want to make a cool About screen or Splash Screen.

Enjoy!

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing