程序启动界面

声明:代码照搬自SharpDevelop源码。

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

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

然后编辑SplashScreen.cs代码

  1. /// <summary> 
  2. /// 启动画面 
  3. /// </summary> 
  4. public partial class SplashScreen : Form 
  5.     /// <summary> 
  6.     /// 启动画面本身 
  7.     /// </summary> 
  8.     static SplashScreen instance; 
  9.  
  10.     /// <summary> 
  11.     /// 显示的图片 
  12.     /// </summary> 
  13.     Bitmap bitmap; 
  14.  
  15.     public static SplashScreen Instance 
  16.     { 
  17.         get 
  18.         { 
  19.             return instance; 
  20.         } 
  21.         set 
  22.         { 
  23.             instance = value; 
  24.         } 
  25.     } 
  26.  
  27.     public SplashScreen() 
  28.     { 
  29.         InitializeComponent(); 
  30.  
  31.         // 设置窗体的类型 
  32.         const string showInfo = "启动画面:我们正在努力的加载程序,请稍后..."
  33.         FormBorderStyle = FormBorderStyle.None; 
  34.         StartPosition = FormStartPosition.CenterScreen; 
  35.         ShowInTaskbar = false
  36.         bitmap = new Bitmap(Properties.Resources.SplashScreen); 
  37.         ClientSize = bitmap.Size; 
  38.  
  39.         using (Font font = new Font("Consoles", 10)) 
  40.         { 
  41.             using (Graphics g = Graphics.FromImage(bitmap)) 
  42.             { 
  43.                 g.DrawString(showInfo, font, Brushes.White, 130, 100); 
  44.             } 
  45.         } 
  46.  
  47.         BackgroundImage = bitmap; 
  48.     } 
  49.  
  50.     protected override void Dispose(bool disposing) 
  51.     { 
  52.         if (disposing && (components != null)) 
  53.         { 
  54.             if (bitmap != null
  55.             { 
  56.                 bitmap.Dispose(); 
  57.                 bitmap = null
  58.             } 
  59.             components.Dispose(); 
  60.         } 
  61.         base.Dispose(disposing); 
  62.     } 
  63.  
  64.     public static void ShowSplashScreen() 
  65.     { 
  66.         instance = new SplashScreen(); 
  67.         instance.Show(); 
  68.     } 
	/// <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();
		}
	}


然后在主程序启动时调用

  1. static class Program 
  2.     /// <summary> 
  3.     /// 应用程序的主入口点。 
  4.     /// </summary> 
  5.     [STAThread] 
  6.     static void Main() 
  7.     { 
  8.         Application.EnableVisualStyles(); 
  9.         Application.SetCompatibleTextRenderingDefault(false); 
  10.         // 启动 
  11.         SplashScreen.ShowSplashScreen(); 
  12.  
  13.         // 进行自己的操作:加载组件,加载文件等等 
  14.         // 示例代码为休眠一会 
  15.         System.Threading.Thread.Sleep(3000); 
  16.  
  17.         // 关闭 
  18.         if (SplashScreen.Instance != null
  19.         { 
  20.             SplashScreen.Instance.BeginInvoke(new MethodInvoker(SplashScreen.Instance.Dispose)); 
  21.             SplashScreen.Instance = null
  22.         } 
  23.         Application.Run(new FormMain()); 
  24.     } 
	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());
		}
	}


效果如下图所示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值