views:

1918

answers:

5

In a C# windows forms application. I have a splash screen with some multi-threaded processes happening in the background. What I would like to do is when I display the splash screen initially, I would like to have it appear to "fade in". And then, once all the processes finish, I would like it to appear as though the splash screen is "fading out". I'm using C# and .NET 2.0. Thanks.

+3  A: 

You can use the Opacity property for the form to alter the fade (between 0.0 and 1.0).

RichS
+8  A: 

You could use a timer to modify the Form.Opacity level.

sixlettervariables
A: 
While(this.Opacity !=0)
{
    this.Opacity -= 0.05;
    Thread.Sleep(50);//This is for the speed of the opacity... and will let the form redraw
}
Daok
so you're "fading" out in 2 iterations? I can hardly call that a fade out... more so, I believe your Thread.Sleep will block the UI thread, thus preventing it from updating.
Idan K
Idan calm down. First I had wrote -0.5 and it was a typo, in fact 0.05 could be a better value. But you can use the one you want... The Thread.Sleep() doesn't block the UI since it gives room to the Redraw. Not the best solution maybe but it does work.
Daok
+3  A: 

When using Opacity property have to remember that its of type double, where 1.0 is complete opacity, and 0.0 is completely transparency.

   private void fadeTimer_Tick(object sender, EventArgs e)
    {
        this.Opacity -= 0.01;

        if (this.Opacity <= 0)
        {
            this.Close();
        }            
    }
This is fade-out, not fade-in.
Joe Hildebrand
If the timer were set to a fast 50ms, and Opacity is a percentage, then wouldn't this fade-out take about 8 minutes?
mackenir
@mackenir If your timer is 50ms/tick then it will take 5 seconds. (1.0 / 0.01) = 100 * 50ms = 5000ms => 5sec
Hi @aarontfoley, I've taken the liberty of editing your answer to remove the mention of Opacity being a 'percentage', as it is contradicted by the text that follows (Opacity being a value between 0 and 1). Feel free to revert and make the change yourself, or not. I know how annoying it can be to have your answer edited by somebody else. Cheers.
mackenir
@mackenir np, thanks for the edit.