C#欢迎界面的两种形式

C#中欢迎界面的两种形式,一种为设置透明度,即透明度由浅到深;另一中为文字显示的形式,如正在加载···。

先说第一种形式:

新建两个窗体,Form1和Form2,其中Form1为欢迎窗体,Form2为主窗体。

Form1中,设置FromBorderStyle为None,BackgroundImage为自定义欢迎界面的图片,BackgroundImageLayout为Stretch,添加Timer控件。

From1中的代码如下所示:

 1         private void Form1_Load(object sender, EventArgs e)
 2         {
 3             this.Opacity = 0.0;
 4             timer1.Interval = 50;
 5             timer1.Start();
 6         }
 7 
 8         private void timer1_Tick(object sender, EventArgs e)
 9         {
10             if (this.Opacity < 1.0)
11             {
12                 this.Opacity += 0.01;
13             }
14             else
15             {
16                 timer1.Stop();
17                 System.Threading.Thread.Sleep(3000);
18                 this.Close();
19             }
20         }

刚开始,From1的Opacity为0.0,即透明度为0,然后每隔50毫秒透明度增加0.01,5秒后透明度为1,然后等待3秒后,窗体关闭。

Form2中代码如下所示:

1         public Form2()
2         {
3             InitializeComponent();
4             Form1 fm1 = new Form1();
5             fm1.StartPosition = FormStartPosition.CenterScreen;
6             fm1.ShowDialog();
7         }

还有因为Form2为主窗体,故Program.cs中代码应为:

1         static void Main()
2         {
3             Application.EnableVisualStyles();
4             Application.SetCompatibleTextRenderingDefault(false);
5             Application.Run(new Form2());
6         }

运行结果如图1和图2所示。

 

图1 透明度为0.3时Form1状态

图2 透明度为1时From1状态

 

第二种形式:

Form1的设置,在第一种形式的基础上,添加Label控件。

Form1代码如下:

 1     public partial class Form1 : Form
 2     {
 3         int i = 0;
 4         int dots = 0;
 5         public Form1()
 6         {
 7             InitializeComponent();
 8         }
 9 
10         private void Form1_Load(object sender, EventArgs e)
11         {
12             timer1.Interval = 1000;
13             timer1.Start();
14         }
15 
16         private void timer1_Tick(object sender, EventArgs e)
17         {
18             i++;
19             if( i <= 5 )
20             {
21                 label1.Text = ShowWait("正在初始化"); 
22             }
23             else if (i > 5 && i <= 12 )
24             {
25                 label1.Text = ShowWait("正在加载中");
26             }
27             else if (i > 12 && i <= 14)
28             {
29                 label1.Text = "完成加载!";
30             }
31             else if (i > 14)
32             {
33                 timer1.Stop();
34                 this.Close();
35             }
36         }
37 
38         private string ShowWait(string str)
39         {
40             string output = string.Empty;
41             dots++;
42             if (dots >= 10)
43             {
44                 dots = 0;
45             }
46             for (int i = 0; i <= dots; i++)
47             {
48                 output += ".";
49             }
50             return str + output;
51         }
52     }

From2和Program.cs的代码不变。

运行效果如图3所示。

图3 正在初始化状态

图4 正在加载中状态

图5 完成加载状态

完成以后,Form1窗体关闭,From2主窗体显示。

原文地址:https://www.cnblogs.com/zhzhx/p/3187285.html