Thursday, February 15, 2007

Creating splash screens with fading effects in Windows Forms

All those cool splash screens that you see during application-startup can be created using Windows Forms in .NET 2.0/1.1 easily. Read my previous post on creating visually attractive custom shaped forms in .NET. You can enhance these forms with fading effects that could be "cool" and appealing.

The trick is with the Form's Opacity property which governs its overall transparency levels. A double value of 1.00, which is its default, sets the Form to be Opaque. Whereas, any value less than that would make it transparent. In mathematical terms, a form is 60% opaque when its opacity value is 0.60. And it is completely invisible when the value is 0.00.

Fading Effect

The code below does the trick of fading-out. Insert this code in the InitializeComponent method or in a button-click event wherever you would want a "fading-effect" to be fired.

for (int i = 100; i >= 0; i -= 10)
    this.Opacity = (double)i / 100;

As you can see, between each levels of transparency, I'm specifying a delay of 100 milliseconds. This would give the real fading effect to a form. The Application.DoEvents() method forces a repaint without blocking.

No comments: