小弟在写WINFORM的欢迎界面时,想使用淡入淡出的效果,于是用了opacity语句和timer控件.在运行的时候出现了问题.当运行时,欢迎窗口先要正常弹出,然后消失,再弹出第二次,只有第二次的弹出是按照程序的淡入方式进行的.第一次的弹出应该怎么消去掉?
附上小弟的程序,请各位指教:
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 200;//定时200毫秒
timer1.Enabled = true;//激活计时
} private double winshow = 0;//定义窗体透明变量 private void timer1_Tick(object sender, EventArgs e)
{
this.Opacity = winshow;//初始化窗体不透明度
winshow += 0.1;//在定时内做逐渐淡入循环
if (winshow >=1)
{
timer1.Enabled = false;//停止计时
this.Hide();//隐藏欢迎界面
Form2 f2 = new Form2();//激活主界面
f2.ShowDialog();
}
}
附上小弟的程序,请各位指教:
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 200;//定时200毫秒
timer1.Enabled = true;//激活计时
} private double winshow = 0;//定义窗体透明变量 private void timer1_Tick(object sender, EventArgs e)
{
this.Opacity = winshow;//初始化窗体不透明度
winshow += 0.1;//在定时内做逐渐淡入循环
if (winshow >=1)
{
timer1.Enabled = false;//停止计时
this.Hide();//隐藏欢迎界面
Form2 f2 = new Form2();//激活主界面
f2.ShowDialog();
}
}
{
this.Opacity = winshow; timer1.Interval = 200;//定时200毫秒
timer1.Enabled = true;//激活计时
}
private void Form1_Load(object sender, EventArgs e)里面看看
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
if (this.Opacity < 1.0)
{
this.Opacity += 0.1;
}
else
{
timer1.Stop();
MessageBox.Show("欢迎!");
this.Hide();
Form2 f2 = new Form2();//激活主界面
f2.ShowDialog();
}
}
占用资源,而且从结构上来说不够合理。建议这样:修改主程序Program.cs static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); //把这句改一下 Application.Run(new Form1()); Form1 fm1=new Form1();
if (fm1.ShowDialog() == DialogResult.OK)
{
fm1.Dispose();
Application.Run(new Form2());
}
}
}然后把欢迎窗体的Timer的Tick方法处理程序改一下 private void timer1_Tick(object sender, EventArgs e)
{
if (this.Opacity < 1.0)
{
this.Opacity += 0.1;
}
else
{
timer1.Stop();
this.DialogResult = DialogResult.OK;
}
}这样就可以实现你要的功能,而且Form2是应用程序的主程序,欢迎窗体被及时释放掉了。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//this.Opacity = 0;//淡入
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 200;//定时
timer1.Enabled = true;//激活计时
} private double winshow = 1;//窗体不透明度变量 private void timer1_Tick(object sender, EventArgs e)
{
this.Opacity = winshow;//初始化窗体不透明度
winshow -= 0.1;//窗体淡出计算
if (winshow <=0)
{
timer1.Enabled = false;//停止计时
Form2 f2 = new Form2();//激活主界面
f2.ShowDialog();
this.Close();//这里的改动
}
}
}