您的位置:首页 > 编程语言 > C#

c#制作简单启动画面的方法

2015-04-29 11:00 776 查看

本文实例讲述了c#制作简单启动画面的方法。分享给大家供大家参考。具体分析如下:

启动画面是程序启动加载组件时一个让用户稍微耐心等待的提示框。一个好的软件在有启动等待需求时必定做一个启动画面。启动画面可以让用户有心理准备来接受程序加载的缓慢,还可以让用户知道加载的进度和内容。本文只是记录最简单的构架。

VS2010创建一个C# Windows窗体应用程序,将主窗体改名为FormMain,再创建一个窗体起名为SplashScreen。向程序中加载一个图片作为启动画面,如下图

然后编辑SplashScreen.cs代码

/// <summary>
/// 启动画面
/// </summary>
public partial class SplashScreen : Form
{
/// <summary>
/// 启动画面本身
/// </summary>
static SplashScreen instance;
/// <summary>
/// 显示的图片
/// </summary>
Bitmap bitmap;
public static SplashScreen Instance
{
get
{
return instance;
}
set
{
instance = value;
}
}
public SplashScreen()
{
InitializeComponent();
// 设置窗体的类型
const string showInfo = "启动画面:我们正在努力的加载程序,请稍后...";
FormBorderStyle = FormBorderStyle.None;
StartPosition = FormStartPosition.CenterScreen;
ShowInTaskbar = false;
bitmap = new Bitmap(Properties.Resources.SplashScreen);
ClientSize = bitmap.Size;
using (Font font = new Font("Consoles", 10))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawString(showInfo, font, Brushes.White, 130, 100);
}
}
BackgroundImage = bitmap;
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
if (bitmap != null)
{
bitmap.Dispose();
bitmap = null;
}
components.Dispose();
}
base.Dispose(disposing);
}
public static void ShowSplashScreen()
{
instance = new SplashScreen();
instance.Show();
}
}

然后在主程序启动时调用

static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// 启动
SplashScreen.ShowSplashScreen();
// 进行自己的操作:加载组件,加载文件等等
// 示例代码为休眠一会
System.Threading.Thread.Sleep(3000);
// 关闭
if (SplashScreen.Instance != null)
{
SplashScreen.Instance.BeginInvoke(new MethodInvoker(SplashScreen.Instance.Dispose));
SplashScreen.Instance = null;
}
Application.Run(new FormMain());
}
}

效果如下图所示:

希望本文所述对大家的C#程序设计有所帮助。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# 启动画面