c#制作简单启动画面

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

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

 

然后编辑SplashScreen.cs代码

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


然后在主程序启动时调用

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


效果如下图所示:

posted on 2017-02-10 23:00  带刀侍卫++i  阅读(961)  评论(0编辑  收藏  举报

导航