我们在开发桌面应用程序的时候,由于程序启动比较慢,往往为了提高用户的体验,增加一个闪屏,也就是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 ISplashForm void ISplashForm.SetStatusInfo(string NewStatusInfo)
{
lbStatusInfo.Text = NewStatusInfo;
} #endregion
}
}

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

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

WinForm界面开发之 启动界面的更多相关文章

  1. Android应用--新浪微博客户端新特性滚动视图和启动界面实现

    新浪微博客户端新特性滚动视图和启动界面实现 2013年8月20日新浪微博客户端开发之启动界面实现 前言: 使用过新浪微博客户端的童鞋都清楚,客户端每一次升级之后第一次启动界面就会有新特性的介绍,用户通 ...

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

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

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

    WinForm界面开发之布局控件"WeifenLuo.WinFormsUI.Docking"的使用 转自:http://www.cnblogs.com/wuhuacong/arch ...

  4. Winform开发中对界面的组织布局

    在设计界面的时候,不管是在Web端,还是在Winform端,或者是WPF或者移动界面等应用上,我们对界面的组织布局,一直是比较有趣的话题,而组织界面的好坏从用户的感受来看,可以提供程序可使用性高低,也 ...

  5. winform改变启动界面

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

  6. winform界面开发-HTML内容编辑控件

    参照及推荐博客:伍华聪 http://www.cnblogs.com/wuhuacong/archive/2009/07/07/1518346.html http://www.cnblogs.com/ ...

  7. 关于C#界面开发winform与SharpGL结合鼠标只在OpenGLControl绘图区域显示坐标移动消息响应(鼠标单独在某个控件上的消息响应)

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/11773260.html 因为很多时候我们开发画图之类的工具时,鼠标移动之类的,都只想在绘图区域 ...

  8. Android开发(25)--framebyframe帧动画并实现启动界面到主界面的跳转

    Drawable animation可以加载Drawable资源实现帧动画.AnimationDrawable是实现Drawable animations的基本类.推荐用XML文件的方法实现Drawa ...

  9. winform启动界面+登录窗口

    需求场景:先展示启动界面,然后打开登录界面,如果登录成功就跳转到主界面 首先在程序的入口路径加载启动界面,使用ShowDialog显示界面, 然后在启动界面中添加定时器,来实现显示一段时间的效果,等到 ...

随机推荐

  1. springcloud之config 配置管理中心之配置属性加密解密

    1.为什么要加密解密? 为了维护项目的安全性. 2.配置加密解密的前提是什么? 要进行JCE下载,然后替换掉jdk的security文件: 下载链接:http://www.oracle.com/tec ...

  2. Error: cannot allocate vector of size 88.1 Mb问题

    这几天训练模型运行代码的时候,老是提示我说:Error: cannot allocate vector of size 88.1 Mb,只知道分配空间不足. 下面是查资料看到的一些回答: 一.这个是R ...

  3. 网络中,FIFO、LRU、OPT这三种置换算法的缺页次数

    FIFO.LRU.OPT这三种置换算法的缺页次数 转载  由于要考计算机四级网络,这里遇到了问题,就搜了一些资料来解疑. 考虑下述页面走向: 1,2,3,4,2,1,5,6,2,1,2,3,7,6,3 ...

  4. 1.Linux的发展历史以及 GNUGPL和open source

    发展历史: 20实际60年代:那时候的计算机一般只有在军事,科研以及学术院校才能见到,不是一般人能接触的东西.开始的时候计算机的时候的输入靠卡片阅读器,即程序开发者在卡片上打洞放入卡片阅读器上输入,在 ...

  5. 题解【bzoj1251 序列终结者】

    Description 维护三个操作:区间加,区间翻转,区间求最大值.\(n \leq 50000\) Solution fhqtreap大法好! 模板题(我是不会告诉你这篇题解是用来存个代码的 Co ...

  6. Redis3未授权访问漏洞导致服务器被入侵

    今天在腾讯云上搭的开发环境里的一台机器cpu load飚升老高,然后还能登陆上去,top后发现两个可疑进程./root/目录下有修改过的文件./opt目录被干掉了, 后经分析,这台机器上有redis外 ...

  7. Java基础-IO流对象之内存操作流(ByteArrayOutputStream与ByteArrayInputStream)

    Java基础-IO流对象之内存操作流(ByteArrayOutputStream与ByteArrayInputStream) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.内存 ...

  8. Java基础-面向接口(interface)编程

    Java基础-面向接口(interface)编程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的“类 ...

  9. IOS艺术字及简单的图文混排

    NSString* alertText = [NSString stringWithFormat:@" 以下%d节课程(总课酬¥%.02lf)家长们尚未结课并评价,请尽快联系家长,否则无法获 ...

  10. Kafka 0.8 Consumer Rebalance

    1 Rebalance时机 0.10kafka的rebalance条件 条件1:有新的consumer加入 条件2:旧的consumer挂了 条件3:coordinator挂了,集群选举出新的coor ...