欢迎界面的目的一方面是为了让界面好看,另外一方面可以让主界面的数据准备。
一、用timer实现
欢迎界面中控制界面的显示方式并使用 timer 控制欢迎界面的消失时间
1 staticclassProgram
2 {
3 ///<summary>4 ///应用程序的主入口点。
5 ///</summary>6 [STAThread]
7 staticvoidMain()
8 {
9 Application.EnableVisualStyles();
10 Application.SetCompatibleTextRenderingDefault(false);
11 Application.Run(newMainForm());
12 }
13 }
14 15 namespaceWindowsFormsApplication1
16 {
17 publicpartialclassMainForm : Form
18 {
19 publicMainForm()
20 {
21 22 SplashForm form =newSplashForm();
23 form.ShowDialog();
24 InitializeComponent();
25 }
26 }
27 }
28 29 namespaceWindowsFormsApplication1
30 {
31 publicpartialclassSplashForm : Form
32 {
33 publicSplashForm()
34 {
35 InitializeComponent();
36 }
37 38 privatevoidSplashForm_Load(objectsender, EventArgs e)
39 {
40 this.FormBorderStyle =FormBorderStyle.None;
41 this.timer1.Start();
42 this.timer1.Interval =5000;
43 }
44 45 privatevoidtimer1_Tick(objectsender, EventArgs e)
46 {
47 this.Close();
48 }
49 50 privatevoidSplashForm_FormClosed(objectsender, 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,运行。
1 form1:
2 namespaceWindowsFormsApplication1
3 {
4 publicpartialclassForm1 : Form
5 {
6 publicForm1()
7 {
8 9 InitializeComponent();
10 }
11 12 privatevoidForm1_Load(objectsender, 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 namespaceWindowsFormsApplication1
28 {
29 publicpartialclassMainForm : Form
30 {
31 publicMainForm()
32 {
33 34 Form1 form =newForm1();
35 form.ShowDialog();
36 InitializeComponent();
37 }
38 }
39 }