Constant Learner
Constant Learner

Reputation: 525

Window form opacity .. How to control?

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????)

Upvotes: 6

Views: 28808

Answers (6)

Hasan Uddin
Hasan Uddin

Reputation: 492

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();
    }

Upvotes: 1

Hasan Uddin
Hasan Uddin

Reputation: 492

increasing Opacity Animation while Application start

        ///////\\\\\\ Coded by Error X Tech ///////\\\\\\
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
void IncreaseOpacity(object sender, EventArgs e)
    {
        if (this.Opacity <= 1)  //replace 0.88 with whatever you want
        {
            this.Opacity += 0.01;  //replace 0.01 with whatever you want
        }
        if (this.Opacity == 1) //replace 0.88 with whatever you want
            timer.Stop();
    }
private void Form1_Load(object sender, EventArgs e)
    {
        this.Opacity = .01;
        timer.Interval = 10; //replace 10 with whatever you want
        timer.Tick += IncreaseOpacity;
        timer.Start();
     }

Upvotes: 0

Robert Rouhani
Robert Rouhani

Reputation: 14668

In the constructor, set the opacity to 0 and start a timer with an interval of something small like 10 or 100 milliseconds. In the timer_Tick event, you simply need to run this.Opacity += 0.01;

This will make it so that the opacity starts at 0 and increase by .01 every few milliseconds until it's 1 (opacity is a double, when it reaches a value of 1 it's fully opaque)

Upvotes: 0

FIre Panda
FIre Panda

Reputation: 6637

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(); 
  }

Upvotes: 1

Maheep
Maheep

Reputation: 5605

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();
}

Upvotes: 7

Ry-
Ry-

Reputation: 224855

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.

Upvotes: 4

Related Questions