程序启动界面

声明:代码照搬自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());
		}
	}


效果如下图所示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在PyQt6中,可以通过添加程序启动画面来提升用户体验。以下是一种实现方式: 1. 首先,你需要创建一个启动画面的窗口类。可以使用QMainWindow或者QWidget作为基类,根据你的需求选择适合的类。 2. 在启动画面窗口类中,可以添加一些控件和布局,以展示你想要显示的内容。例如,可以添加一个QLabel来显示应用程序的名称或者Logo图片。 3. 在主程序中,创建一个启动画面窗口的实例,并设置其为程序启动画面。可以使用QApplication类的setSplashScreen方法来设置启动画面。 4. 在主程序中进行其他初始化操作,例如加载资源、创建主窗口等。 5. 当主窗口准备好显示时,可以关闭启动画面窗口,并显示主窗口。可以使用QApplication类的closeSplashScreen方法来关闭启动画面。 下面是一个简单的示例代码: ```python from PyQt6.QtWidgets import QApplication, QMainWindow, QSplashScreen, QLabel from PyQt6.QtGui import QPixmap import sys class SplashScreen(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("启动画面") label = QLabel(self) pixmap = QPixmap("logo.png") # 设置Logo图片 label.setPixmap(pixmap) label.setAlignment(Qt.AlignmentFlag.AlignCenter) self.setCentralWidget(label) if __name__ == "__main__": app = QApplication(sys.argv) splash = QSplashScreen() splash.setPixmap(QPixmap("splash.png")) # 设置启动画面图片 splash.show() # 创建启动画面窗口实例 splash_window = SplashScreen() # 设置启动画面 app.setSplashScreen(splash) # 其他初始化操作... # 关闭启动画面,显示主窗口 app.closeSplashScreen() sys.exit(app.exec()) ``` 请注意,上述代码仅为示例,你需要根据自己的需求进行适当的修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值