我正在使用计时器来创建启动画面。
我想做的是让表格淡入淡出。我首先在表单的构造函数中将表单的不透明度设置为 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(
最佳答案
尝试此 post 中 Servy 建议的方法.我修改了 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/