欢迎界面的目的一方面是为了让界面好看,另外一方面可以让主界面的数据准备。
一、用timer实现
欢迎界面中控制界面的显示方式并使用 timer 控制欢迎界面的消失时间
View Code
1 static class Program
2 {
3 /// <summary>
4 /// 应用程序的主入口点。
5 /// </summary>
6 [STAThread]
7 static void Main()
8 {
9 Application.EnableVisualStyles();
10 Application.SetCompatibleTextRenderingDefault(false);
11 Application.Run(new MainForm());
12 }
13 }
14
15 namespace WindowsFormsApplication1
16 {
17 public partial class MainForm : Form
18 {
19 public MainForm()
20 {
21
22 SplashForm form = new SplashForm();
23 form.ShowDialog();
24 InitializeComponent();
25 }
26 }
27 }
28
29 namespace WindowsFormsApplication1
30 {
31 public partial class SplashForm : Form
32 {
33 public SplashForm()
34 {
35 InitializeComponent();
36 }
37
38 private void SplashForm_Load(object sender, EventArgs e)
39 {
40 this.FormBorderStyle = FormBorderStyle.None;
41 this.timer1.Start();
42 this.timer1.Interval = 5000;
43 }
44
45 private void timer1_Tick(object sender, EventArgs e)
46 {
47 this.Close();
48 }
49
50 private void SplashForm_FormClosed(object sender, FormClosedEventArgs e)
51 {
52 this.timer1.Stop();
53 }
54 }
55 }
二、用Thread实现
第一步:准备欢迎界面Form1和主界面MainForm
第二步:在MainForm的初始事件中加入代码:
Form1 form = new Form1();
form.ShowDialog();
在Form1的Load事件中加入代码:
this.Show();
while (this.Opacity < 1)
{
this.Opacity += 0.01;
}
System.Threading.Thread.Sleep(1000);
this.Close();
第三步:在Program.cs中启动MainForm,运行。
View Code
1 form1:
2 namespace WindowsFormsApplication1
3 {
4 public partial class Form1 : Form
5 {
6 public Form1()
7 {
8
9 InitializeComponent();
10 }
11
12 private void Form1_Load(object sender, EventArgs e)
13 {
14 this.Show();
15 while (this.Opacity < 1)
16 {
17 this.Opacity += 0.01;
18 }
19 System.Threading.Thread.Sleep(1000);
20 this.Close();
21
22 }
23 }
24 }
25
26 MainForm:
27 namespace WindowsFormsApplication1
28 {
29 public partial class MainForm : Form
30 {
31 public MainForm()
32 {
33
34 Form1 form = new Form1();
35 form.ShowDialog();
36 InitializeComponent();
37 }
38 }
39 }