很酷的单独线程splash开机画面C#代码

 

逛逛sourceforge,有时候会有不错的东西能淘到,就比如这个开机画面的Form就不错,(在一个report的项目里,出处又是别的地方。。。)。要用的话,把代码拷过来,在form里改改背景图片,就可以了,对了还得加个timer。这个类用静态方法包装了一个线程,在开机的时候单独执行,渐入渐出也很好控制,尤为好的是在splash的时候就可以向里面实时的写进度条了,而不是先设置好了格子。。。开始的时候用这个方法就可以激活了:

SplashScreen.ShowSplashScreen();

写信息是用这个方法,不会阻塞怎么写都行,嘿嘿:

SplashScreen.SetStatus(moduleName);

不过,为了避免一开始程序的时候执行splash以及主线程会抢,还是用个OnAppIdle在进程空闲的时候开始执行主线程为好。

  static void Main()
  {
   System.Windows.Forms.Application.Idle += new EventHandler(OnAppIdle)

....................................

OnAppIdle里这样写:当然为了达到这点,得把进程的context提出来

  private static System.Windows.Forms.ApplicationContext context;
  private static MainForm mForm  = new MainForm();

  private static void OnAppIdle(object sender, EventArgs e)
  {
   if(context.MainForm == null)
   {
    // first we remove the eventhandler
    System.Windows.Forms.Application.Idle -= new EventHandler(OnAppIdle);

//调用SplashScreen.ShowSplashScreen();写点东西

...................................

     // now we set the main form for the context...
    context.MainForm = mForm;

    // ...show it...调用主form
    context.MainForm.Show();

................................................................

 

接下来就是splash的代码了:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using Microsoft.Win32;

namespace SplashDemo
{
 /// <summary>
 /// Splash screen implemented by Tom Clement, see the CodeProject
 /// http://www.codeproject.com/csharp/PrettyGoodSplashScreen.asp
 /// </summary>
 public class SplashScreen : System.Windows.Forms.Form
 {
  // Threading
  static SplashScreen ms_frmSplash = null;
  static Thread ms_oThread = null;

  // Fade in and out.
  private double m_dblOpacityIncrement = .05;
  private double m_dblOpacityDecrement = .08;
  private const int TIMER_INTERVAL = 50;

  // Status and progress bar
  static string ms_sStatus;
  private double m_dblCompletionFraction = 0;
  private Rectangle m_rProgress;

  // Progress smoothing
  private double m_dblLastCompletionFraction = 0.0;
  private double m_dblPBIncrementPerTimerInterval = .015;

  // Self-calibration support
  private bool m_bFirstLaunch = false;
  private DateTime m_dtStart;
  private bool m_bDTSet = false;
  private int m_iIndex = 1;
  private int m_iActualTicks = 0;
  private ArrayList m_alPreviousCompletionFraction;
  private ArrayList m_alActualTimes = new ArrayList();
  private const string REG_KEY_INITIALIZATION = "Initialization";
  private const string REGVALUE_PB_MILISECOND_INCREMENT = "Increment";
  private const string REGVALUE_PB_PERCENTS = "Percents";

  private System.Windows.Forms.Label lblStatus;
  private System.Windows.Forms.Label lblTimeRemaining;
  private System.Windows.Forms.Timer timer1;
  private System.Window

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值