Window form opacity .. How to control?

24,614

Solution 1

in constructor of the form you can write something like this.

this.Opacity = .1;
timer.Interval = new TimeSpan(0, 0, intervalinminutes);
timer.Tick += ChangeOpacity;
timer.Start();

And then define a method like this

void ChangeOpacity(object sender, EventArgs e)
{
    this.Opacity += .10; //replace.10 with whatever you want
    if(this.Opacity == 1)
        timer.Stop();
}

Solution 2

To fade forms in and out, I usually do this:

for(double opacity = 0.0; opacity <= 1.0; opacity += 0.2) {
    DateTime start = DateTime.Now;
    this.Opacity = opacity;

    while(DateTime.Now.Subtract(start).TotalMilliseconds <= 30.0) {
        Application.DoEvents();
    }
}

It's a nice, simple solution if you'll be doing it very infrequently. Otherwise, I would recommend using threads.

Solution 3

In the constructor, start the timer control that will call a method at each tick.

timer.Interval = 1000; 
timer.Tick += new EventHandler(TimerEventProcessor);
timer.Start(); 

............

 private static void TimerEventProcessor(Object myObject,
                                            EventArgs myEventArgs) 
  {
       if(this.Opacity < 1)
         this.Opacity += .1;
       else
           timer.Stop(); 
  }

Solution 4

For Exit Decreasing Opacity Animation

       ///////\\\\\\ Coded by Error X Tech ///////\\\\\\

 System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
 private void DecreaseOpacity(object sender, EventArgs e)
    {
        if (this.Opacity >= 0.1)
        {
            this.Opacity -= 0.04; //replace 0.04 with whatever you want
        }
        if (this.Opacity <= 0.0)
            timer.Stop();
        if (this.Opacity <= 0.1)
        {
            System.Environment.Exit(1);
            Process.GetCurrentProcess().Kill();
        }
    }

private void Exit_Click(object sender, EventArgs e)
    {
        
        timer.Interval = 47; //replace 47 with whatever you want
        timer.Tick += DecreaseOpacity;
        timer.Start();
    }
Share:
24,614
Constant Learner
Author by

Constant Learner

Updated on July 16, 2020

Comments

  • Constant Learner
    Constant Learner almost 4 years

    I have a single form window application now I want to change the form opacity when application runs. Means when application run it will show low opacity form and as time increse it will show complete form with 100 opacity. So how to do that. (should I use timer control to control opacity, if yes then how????)

  • Constant Learner
    Constant Learner over 12 years
    ok but do you think it will work as form loads... Means when application run it will show low opacity form and as time increse it will show complete form with 100 opacity.???
  • Maheep
    Maheep over 12 years
    Yes it will work. For this initially you will have to set the opacity to low value. I have updated the answer.
  • Ivo
    Ivo over 12 years
    I would add a check if the value is 1 and then stop the timer.
  • Maheep
    Maheep over 12 years
    @ivowiblo: Thanks. That's a must. Updated the answer.
  • Cody Gray
    Cody Gray over 12 years
    Probably a good idea to avoid creating superfluous timers. But I think the next step up from here is to use a timer, not try to manipulate the GUI from multiple threads...
  • Constant Learner
    Constant Learner over 12 years
    Can u pls suggest me , how to do same when form close??? Means when i close it will down opacity
  • FIre Panda
    FIre Panda over 12 years
    On form closing event handler, you could cancel the event that start the timer, when opacity gets 0, close the form yourself.
  • Harag
    Harag over 12 years
    @minitech - Nice, I put this in the form_load event and it works well, but when I put it in the form1_FormClosing event (with changes to the loop to go from 1.0 to 0, the form just simply closes and doesn't fade out. Any ideas why?
  • Ry-
    Ry- over 12 years
    @harag: It's because the FormClosing event happens right before the form is closed, i.e. it will be closed if you don't cancel it. This is because Application.DoEvents() processes waiting messages, like those to close a form. So for closing, set e.Cancel to true, fade out, then close again; but make sure to keep a flag inside your form so that you don't end up in an endless loop.
  • Harag
    Harag over 12 years
    @minitech thanks for the explanation, much appreciated. I'll give it a try to see what happens.
  • Ry-
    Ry- over 12 years
    @CodyGray: A timer does exactly that :)