Timer in WinForms

2019-05-17 08:59发布

I'm using a timer to create a Splash Screen. What I want to do is to make the form fade-in and fade-out. I started with making the form opacity 0 in the form's constructor, and by triggering the timer in the Form load method. Now in my Timer_Tick method, I keep increasing the opacity by, say, 0.2. I figured that I'd start decreasing the opacity once the timer hits half its interval, but I'm unable to do that.

I'm not very clear with how the timer works as it is, but I wanted to implement something like this:

if(Whatever_Timer_Value_Is <= Interval/2)  //Can't achieve this :s
this.Opacity += 2;
else
this.Opacity -=2 ;

So..is there a way to get the value of the Timer at any instant? Or is there any other way to do this? Please keep it simple. I'm just an amateur. X(

4条回答
时光不老,我们不散
2楼-- · 2019-05-17 09:31

have a look to this, a sample for a splash screen in c#: http://www.codeproject.com/Articles/5454/A-Pretty-Good-Splash-Screen-in-C

查看更多
Anthone
3楼-- · 2019-05-17 09:47

Try this method suggested by Servy in this post. I have modified method for Form Fade-Out to hide form.

public Form1()
{
    InitializeComponent();
    this.Opacity = 0;
}


private void Form1_Load(object sender, EventArgs e)
{            
    ShowMe();
}


private void button1_Click(object sender, EventArgs e)
{
    HideMe();

}

private void ShowMe()
{
    int duration = 1000;//in milliseconds
    int steps = 100;
    Timer timer = new Timer();
    timer.Interval = duration / steps;

    int currentStep = 0;
    timer.Tick += (arg1, arg2) =>
    {
        Opacity = ((double)currentStep) / steps;
        currentStep++;

        if (currentStep >= steps)
        {
            timer.Stop();
            timer.Dispose();
        }
    };

    timer.Start();
}
private void HideMe()
{
    int duration = 1000;//in milliseconds
    int steps = 100;
    Timer timer = new Timer();
    timer.Interval = duration / steps;

    int currentStep = 100;
    timer.Tick += (arg1, arg2) =>
    {
        Opacity = ((double)currentStep) / steps;
        currentStep--;

        if (currentStep <= 0)
        {
            timer.Stop();
            timer.Dispose();
            this.Close();
        }
    };

    timer.Start();
}
查看更多
贪生不怕死
4楼-- · 2019-05-17 09:52

Try creating a second form for splash:

Form splash = new Form();

public Form1()
{
    InitializeComponent();
    this.Visible = false;
    splash.Opacity = 0;
    splash.Show();
    _timerShow();
    _timerHide();
    this.Visible = true;
}

private async void _timerShow()
{
    while(splash.opacity!=1)
    {
        await Task.Delay(50);
        splash.opacity +=.01;
    }
}

private async void _timerHide()
{
    while(splash.opacity!=0)
    {
        await Task.Delay(50);
        splash.opacity -=.01;
    }
}
查看更多
Explosion°爆炸
5楼-- · 2019-05-17 09:55

Just remember the time when you start the timer. That way you can always tell how much time has passed.

You can use Environment.TickCount for that. This is a monotonic clock.

Incremental computations (like Opacity += 0.2;) are to be avoided in timers because it is not guaranteed to receive all ticks or too receive them at the right points in time. You better calculate how much time has passed and calculate the right opacity value from that.

查看更多
登录 后发表回答