Jerry Nixon on Windows: Use opacity to "fade in" a WinForm

Jerry Nixon on Windows

Friday, March 14, 2008

Use opacity to "fade in" a WinForm

When the form opens it starts transparent and then slowly gains opacity. It's cute. I have used an anonymous method here for demonstration purposes. In all, this technique is a simple slam dunk and makes your forms look slick - you can add it to a custom form base if you like. But BEWARE - do not overuse this technique! Don't be one of THOSE developer that makes the whole web page <blink>Hello World!</blink>. You know what I mean.

Here it is:

class MyForm: Form
{
Timer m_TimerFadeIn = new Timer()
{
Interval = 50,
Enabled = true
};
public MyForm()
{
this.Opacity = 0;
m_TimerFadeIn.Tick += delegate(object sender, EventArgs e)
{
this.Opacity += .09;
if (this.Opacity >= 100)
timer1.Stop();
};
}
}