程序开始的 splash 窗体

程序开始的 splash 窗体这个主要用到ApplicationContext

//启动窗体虚基类,继承自ApplicationContext
using System.Windows.Forms;
using System.Threading;
using System;
//启动窗体类(继承自启动窗体虚基类),启动画面会停留一段时间,该时间是设定的时间和主窗体构造所需时间两个的最大值
public class SplashContext : ApplicationContext
{
    private Form _SplashScreenForm;//启动窗体
    private Form _PrimaryForm;//主窗体
    private System.Timers.Timer _SplashScreenTimer;
    private int _SplashScreenTimerInterVal = 3000;//默认是启动窗体显示5秒
    private bool _bSplashScreenClosed = false;
    private delegate void DisposeDelegate();//关闭委托,下面需要使用控件的Invoke方法,该方法需要这个委托
    private string _StrInput = "";

    public SplashContext()
    {
        this.ShowSplashScreen();//这里创建和显示启动窗体
        this.MainFormLoad();//这里创建和显示启动主窗体
    }

    public SplashContext(string StrInput)
    {
        _StrInput = StrInput;
        this.ShowSplashScreen();//这里创建和显示启动窗体
        this.MainFormLoad();//这里创建和显示启动主窗体
    }

    private void ShowSplashScreen()
    {
        this.OnCreateSplashScreenForm();
        this._SplashScreenTimer = new System.Timers.Timer(((double)(this._SplashScreenTimerInterVal)));
        _SplashScreenTimer.Elapsed += new System.Timers.ElapsedEventHandler(new System.Timers.ElapsedEventHandler(this.SplashScreenDisplayTimeUp));
        this._SplashScreenTimer.AutoReset = false;
        Thread DisplaySpashScreenThread = new Thread(new ThreadStart(DisplaySplashScreen));
        DisplaySpashScreenThread.Start();
    }

    private void DisplaySplashScreen()
    {
        this._SplashScreenTimer.Enabled = true;
        Application.Run(this._SplashScreenForm);
    }

    private void SplashScreenDisplayTimeUp(object sender, System.Timers.ElapsedEventArgs e)
    {
        this._SplashScreenTimer.Dispose();
        this._SplashScreenTimer = null;
        this._bSplashScreenClosed = true;
    }

    private void MainFormLoad()
    {
        this.OnCreateMainForm();
        while (!(this._bSplashScreenClosed))
        {
            Application.DoEvents();
        }
        DisposeDelegate SplashScreenFormDisposeDelegate = new DisposeDelegate(this._SplashScreenForm.Dispose);
        this._SplashScreenForm.Invoke(SplashScreenFormDisposeDelegate);
        this._SplashScreenForm = null;


        //必须先显示,再激活,否则主窗体不能在启动窗体消失后出现
        this._PrimaryForm.Show();
        this._PrimaryForm.Activate();
        this._PrimaryForm.Closed += new EventHandler(_PrimaryForm_Closed);

    }

    private void _PrimaryForm_Closed(object sender, EventArgs e)
    {
        base.ExitThread();
    }

    private void OnCreateSplashScreenForm()
    {
        _SplashScreenForm = new SplashForm();//启动窗体
    }

    private void OnCreateMainForm()
    {
        if (_StrInput != null && !_StrInput.Equals(""))
        {
            _PrimaryForm = new PowerReuseForm(_StrInput);//主窗体
        }
        else
        {
            _PrimaryForm = new PowerReuseForm();
        }
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值