我们在开发桌面应用程序的时候,由于程序启动比较慢,往往为了提高用户的体验,增加一个闪屏,也就是SplashScreen,好处有:1、让用户看到加载的过程,提高程序的交互响应;2.可以简短展示或者介绍程序的功能或者展示Logo,给客户较深的印象。

本人在开发的共享软件中,对于启动比较慢的程序,也倾向于引入这个控件来展示下,先看看软件启动的时候的效果

中间的那些文字“正在初始化应用程序......”可以根据加载的进度显示不同的内容,当然最好简单扼要了,其他的内容你也可以视需要做相应变化,因为这个是一个Form,你想改变什么就改变什么的。

看看闪屏代码如何使用先,首先我们在入口的Main函数中开始

    static class Program{/// <summary>/// 应用程序的主入口点。/// </summary>
        [STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);//登陆界面FrmLogin dlg = new FrmLogin();dlg.StartPosition = FormStartPosition.CenterScreen;if (DialogResult.OK == dlg.ShowDialog()){SplashScreen.Splasher.Show(typeof(SplashScreen.FrmSplashScreen));Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);Application.Run(new FrmMain());}}private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs ex){//LogHelper.Error(ex.Exception);string message = string.Format("{0}\r\n操作发生错误,您需要退出系统么?", ex.Exception.Message);if (DialogResult.Yes ==MessageBox.Show(message,"系统错误",MessageBoxButtons.YesNo)){Application.Exit();}}}

上面代码中:SplashScreen.Splasher.Show(typeof(SplashScreen.frmSplash));主要为启动闪屏类代码,

其中 Splasher 这个类当中使用后台线程,反射来实例化窗体,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;namespace SplashScreen
{public class Splasher{private static Form m_SplashForm = null;private static ISplashForm m_SplashInterface = null;private static Thread m_SplashThread = null;private static string m_TempStatus = string.Empty;/// <summary>/// Show the SplashForm/// </summary>public static void Show(Type splashFormType){if (m_SplashThread != null)return;if (splashFormType == null){throw (new Exception("splashFormType is null"));}m_SplashThread = new Thread(new ThreadStart(delegate(){CreateInstance(splashFormType);Application.Run(m_SplashForm);}));m_SplashThread.IsBackground = true;m_SplashThread.SetApartmentState(ApartmentState.STA);m_SplashThread.Start();}/// <summary>/// set the loading Status/// </summary>public static string Status{set{if (m_SplashInterface == null || m_SplashForm == null){m_TempStatus = value;return;}m_SplashForm.Invoke(new SplashStatusChangedHandle(delegate(string str) { m_SplashInterface.SetStatusInfo(str); }),new object[] { value });}}/// <summary>/// Colse the SplashForm/// </summary>public static void Close(){if (m_SplashThread == null || m_SplashForm == null) return;try{m_SplashForm.Invoke(new MethodInvoker(m_SplashForm.Close));}catch (Exception){}m_SplashThread = null;m_SplashForm = null;}private static void CreateInstance(Type FormType){//利用反射创建对象object obj = Activator.CreateInstance(FormType);m_SplashForm = obj as Form;m_SplashInterface = obj as ISplashForm;if (m_SplashForm == null){throw (new Exception("Splash Screen must inherit from System.Windows.Forms.Form"));}if (m_SplashInterface == null){throw (new Exception("must implement interface ISplashForm"));}if (!string.IsNullOrEmpty(m_TempStatus))m_SplashInterface.SetStatusInfo(m_TempStatus);}private delegate void SplashStatusChangedHandle(string NewStatusInfo);}
}

为了增加启动界面的说明,在启动界面上增加了一个文字接口,可以在外部来修改启动界面上的说明:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace SplashScreen
{/// <summary>/// interface for Splash Screen/// </summary>public interface ISplashForm{void SetStatusInfo(string NewStatusInfo);}
}

然手在闪屏的窗体上继承接口,并实现相关的接口代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace SplashScreen
{public partial class FrmSplashScreen : Form,ISplashForm{public FrmSplashScreen(){InitializeComponent();}//实现接口方法,主要用于接口的反射调用#region ISplashFormvoid ISplashForm.SetStatusInfo(string NewStatusInfo){lbStatusInfo.Text = NewStatusInfo;}#endregion}
}

然后程序在点击“登录”按钮后,就可以在主界面上做闪屏界面等待了,frmMain窗体代码:

namespace SplashScreen
{public partial class FrmMain : Form{public FrmMain(){InitializeComponent();System.Threading.Thread.Sleep(800);Splasher.Status = "正在展示相关的内容......";System.Threading.Thread.Sleep(800);//.......此处加载耗时的代码
Splasher.Status = "初始化完毕............";System.Threading.Thread.Sleep(800);Splasher.Close();}}
}

转载于:https://www.cnblogs.com/gzskys/p/5661302.html

WinForm界面开发之 启动界面相关推荐

  1. java 程序启动界面_程序启动界面java代码

    最近写了个程序启动界面,分享一下 import javax.swing.*; import java.awt.*; import java.net.*; //Download by http://ww ...

  2. python可视化界面开发实例-python界面编程,python可视化窗口编程

    看了你的问题,几个回答似乎都不完全对.给你补充一下吧. 你的问题与flask基本上没有关系.flask是一个简单的微型编程框架,很优美,也很简单好用.不过它仅仅实现了基于HTTP协议的服务端交互部分. ...

  3. python界面开发-python 图形界面编程(GUI)

    1. Tkinter 在开始GUI编程之前,需要先了解这几个概念:窗口和控件.事件驱动处理.布局管理器. 窗体控件: 窗体.标签.按钮.列表框.滚动条等. 事件驱动:按下按钮及释放.鼠标移动.按回车键 ...

  4. ​开机启动界面splashActivity​

    1.什么是Splash?(是否使用splash根据实际需求来,一般不用Splash) 比较成熟一点的应用会在进入应用之前显示一个启动界面.这个启动界面或简单,或复杂,或简陋,或华丽,用意不同,风格也不 ...

  5. BOLT.NET 学习笔记(一) 开篇 用.net winform 快速开发 炫酷的界面

    BOLT.NET 学习笔记(一) 开篇 用.net winform 快速开发 炫酷的界面 bolt 基本介绍 Bolt界面引擎是迅雷公司从2009年开始开发的第四代界面库.迅雷7是首个采用该引擎成功开 ...

  6. CAD二次开发--初始欢迎界面无法加载展示问题解决办法【启动欢迎窗体在CAD界面下层无法展示问题】

    今天在调试CAD开发插件的时候,发现原来的欢迎界面并不能每次都加载!这肯定是不行的啊!如今想到好办法进行处理,特来记录! 1.问题描述 首先说一下我是如何进行初始欢迎界面设置的,第一步还是初始化一个w ...

  7. WinForm界面开发之布局控件WeifenLuo.WinFormsUI.Docking的使用

    控件下载地址(开源) http://sourceforge.net/projects/dockpanelsuite/ 原文:http://www.cnblogs.com/wuhuacong/archi ...

  8. 分享-WinForm界面开发之布局控件WeifenLuo.WinFormsUI.Docking的使用

    分享自伍华聪的-WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用 本篇介绍Winform程序开发中的布局界面的设计,介绍如何在我的 ...

  9. winform改变启动界面

    我们知道,有时做个小项目什么的,一般从登录开始,再到主页,再到其他业务,如果做到其他页面功能,调试时还要从登录页面一个个点进去,明显的降低开发进度. 这时,我们可以直接将目标界面改为启动页面即可. 1 ...

最新文章

  1. 派生类的构造【C++继承】
  2. 童继龙:论ERP顾问的创新分享与专业精神
  3. c语言标准差怎么计算,一个关于文段统计的问题,怎么在运行过程中计算标准差?...
  4. 教你在Zabbix环境下快速升级nginx版本!
  5. Visual C# .NET2003语言的改变
  6. 我国政务大数据政策的文本分析:推进逻辑与未来进路
  7. VAR模型与VECM模型
  8. Android免root字体,字体大师免root
  9. WSO2 ——(10)ESB功能:服务编排
  10. 海思EC6108V9盒子DIY固件打包方法
  11. How to learn Japanese ?
  12. FOTOO-疫情下,企业如何线上组织活动,输出企业文化
  13. [ML](回归和分类)
  14. PaddleOCR .NET 识别图片中文字
  15. 批量转化py2topy3脚本
  16. oracle删除重复数据-百万级别数据以上情况
  17. 区块链技术在银行业的运用
  18. 自动升降压5-40V多串超级电容充电芯片和解决方案
  19. 解决matlab2018A中文乱码问题
  20. PyCharm - Optimizing Imports (优化导入)

热门文章

  1. Exception in thread main java.lang.OutOfMemoryError: Java heap space
  2. 中华成语故事摄制组大公无私的释义
  3. MyBatis源码骨架分析
  4. ASP.NET Core [1]:Hosting(笔记)
  5. ES6里的修饰器Decorator
  6. LaunchScreen.xib 设置启动图
  7. Django Admin 录入中文错误解决办法
  8. C# List Sort
  9. Android开发之使用Handler封装下载图片工具类(源代码分享)
  10. openfire 打包发布