c#制作簡單啟動畫面的方法
本文實例講述了c#制作簡單啟動畫面的方法。分享給大家供大家參考。具體分析如下:
啟動畫面是程序啟動加載組件時一個讓用戶稍微耐心等待的提示框。一個好的軟件在有啟動等待需求時必定做一個啟動畫面。啟動畫面可以讓用戶有心理準備來接受程序加載的緩慢,還可以讓用戶知道加載的進度和內(nèi)容。本文只是記錄最簡單的構(gòu)架。
VS2010創(chuàng)建一個C# Windows窗體應用程序,將主窗體改名為FormMain,再創(chuàng)建一個窗體起名為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();
// 設(shè)置窗體的類型
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();
}
}
然后在主程序啟動時調(diào)用
static class Program
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// 啟動
SplashScreen.ShowSplashScreen();
// 進行自己的操作:加載組件,加載文件等等
// 示例代碼為休眠一會
System.Threading.Thread.Sleep(3000);
// 關(guān)閉
if (SplashScreen.Instance != null)
{
SplashScreen.Instance.BeginInvoke(new MethodInvoker(SplashScreen.Instance.Dispose));
SplashScreen.Instance = null;
}
Application.Run(new FormMain());
}
}
效果如下圖所示:

希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
詳解如何獲取C#類中發(fā)生數(shù)據(jù)變化的屬性信息
這篇文章主要介紹了詳解如何獲取C#類中發(fā)生數(shù)據(jù)變化的屬性信息,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05

