c# - WinForms 中的计时器

标签 c# winforms timer

我正在使用计时器来创建启动画面。 我想做的是让表格淡入淡出。我首先在表单的构造函数中将表单的不透明度设置为 0,然后在表单加载方法中触发计时器。 现在,在我的 Timer_Tick 方法中,我不断增加不透明度,比如 0.2。 我认为一旦计时器达到其间隔的一半,我就会开始降低不透明度,但我无法做到这一点。

我不是很清楚计时器是如何工作的,但我想实现这样的东西:

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

那么..有没有办法随时获取定时器的值?还是有其他方法可以做到这一点? 请保持简单。我只是一个业余爱好者。 X(

最佳答案

尝试此 postServy 建议的方法.我修改了 Form Fade-Out 的方法来隐藏表单。

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

关于c# - WinForms 中的计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24720319/

相关文章:

WPF:在从 WinForms 启动的 Windows 上使用 Snoop?

.net - ASP.NET 验证控件和 Javascript 确认框

c++ - 由于英国夏令时, boost 计时器停止工作

java - 使用设定数量的线程写入设定数量的进度条

c# - 将所有 DLL 嵌入 win app 和 C#

c# - 当 ListView Windows 窗体中没有项目时显示空文本

C# LibVLCSharp 播放器直接馈送媒体

Python - time.sleep 的替代品

c# - 如何为每个 AppDomain 配置一次 AutoMapper

c# - ASP.NET 5 类库可以面向 .NET 3.5 吗?