c# Splasher类
时间: 2023-10-10 22:09:28 浏览: 97
Splasher类是一个在Windows Forms应用程序中显示启动画面的类。它可以在应程序启动时显示定义的欢迎界,以提高用户体验。
以下是一个简单的C# Splasher类的示例代码:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MyApplication
{
public class Splasher : IDisposable
{
private Form splashForm;
private Label messageLabel;
private Timer timer;
public Splasher(string message, Font font, Color backColor, Color foreColor)
{
splashForm = new Form();
splashForm.FormBorderStyle = FormBorderStyle.None;
splashForm.StartPosition = FormStartPosition.CenterScreen;
splashForm.BackgroundImage = Properties.Resources.splash;
splashForm.BackgroundImageLayout = ImageLayout.Stretch;
messageLabel = new Label();
messageLabel.Font = font;
messageLabel.ForeColor = foreColor;
messageLabel.BackColor = backColor;
messageLabel.AutoSize = false;
messageLabel.Size = new Size(splashForm.Width, 20);
messageLabel.TextAlign = ContentAlignment.MiddleCenter;
messageLabel.Text = message;
messageLabel.Location = new Point(0, splashForm.Height - messageLabel.Height);
timer = new Timer();
timer.Interval = 500;
timer.Tick += Timer_Tick;
}
public void Show()
{
splashForm.Controls.Add(messageLabel);
splashForm.Show();
timer.Start();
Application.DoEvents();
}
public void Close()
{
timer.Stop();
splashForm.Dispose();
}
public void Dispose()
{
Close();
}
private void Timer_Tick(object sender, EventArgs e)
{
messageLabel.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Application.DoEvents();
}
}
}
```
使用时,可以在应用程序启动时创建一个Splasher实例,并调用它的Show方法显示欢迎界面,然后在应用程序初始化完成后调用Close方法关闭欢迎界面。
```csharp
using System;
using System.Windows.Forms;
namespace MyApplication
{
static class Program
{
[STAThread]
static void Main()
{
using (Splasher splash = new Splasher("Loading...", new Font("Arial", 12), Color.White, Color.Black))
{
splash.Show();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// 初始化应用程序...
splash.Close();
Application.Run(new MainForm());
}
}
}
}
```
阅读全文