宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

原文如下:

    本代码可以依据主程序加载进度来显示Splash。
    static class Program
    {
        /// <summary>
        /// 主程序的入口点在此设置,包括一些初始化操作,启动窗体等
        /// </summary>
        private static ApplicationContext context;
        [STAThread]      
        static void Main)
        {
            Application.EnableVisualStyles);                       //样式设置
            Application.SetCompatibleTextRenderingDefaultfalse);   //样式设置
            Splash sp = new Splash);                               //启动窗体
            sp.Show);                                              //显示启动窗体
            context = new ApplicationContext);
            context.Tag = sp;
            Application.Idle += new EventHandlerApplication_Idle); //注册程序运行空闲去执行主程序窗体相应初始化代码
            Application.Runcontext);
        }
        //初始化等待处理函数
        private static void Application_Idleobject sender, EventArgs e)
        {
            Application.Idle -= new EventHandlerApplication_Idle);
            if context.MainForm == null)
            {
                Main mw = new Main);
                context.MainForm =mw;
                mw.init);                                  //主窗体要做的初始化事情在这里,该方法在主窗体里应该申明为public
                Splash sp = Splash)context.Tag;
                sp.Close);                                 //关闭启动窗体 
                mw.Show);                                  //启动主程序窗体
            }
        }
    }
        Splash窗体的相关属性设置:
        BackgroundImage:载入你想作为启动画面的图片;
        ControlBox:False;
        FormBorderStyle:None;
        ShowInTaskbar:False;
        StartPositon:CenterScreen.

[转] 
http://www.lordong.cn/blog/post/18.html 
当程序在启动过程中需要花一些时间去加载资源时,我们希望程序能显示一个欢迎界面,能简单介绍软件功能的同时还能告知用户该程序还在加载中,使得用户体验更友好。 
实现如下: 

1. 添加欢迎界面的窗体比如SlpashForm),做以下调整: 
将FormBorderStyle属性设成None,即没有窗体边框 
将StartPosition属性设成CenterScreen,即总是居中 
将TopMost属性设成True,即总是在顶部 
将UseWaitCursor属性设成Ture,即显示等待光标,让人感觉后台还在运行 
增加一个PictureBox控件,与欢迎图片大小一致,窗体的大小也设成一致 
增加一个ProgressBar控件,将Style设成Marquee,将MarqueeAnimationSpeed设成50 

2. 主界面的构造函数改成以下代码: 
// Create thread to show splash window 
Thread showSplashThread = new Threadnew ThreadStartShowSplash)); 
showSplashThread.Start); 

// Time consumed here 
InitializeFrame); // 把原来构造函数中的所有代码移到该函数中 

// Abort show splash thread 
showSplashThread.Abort); 
showSplashThread.Join); // Wait until the thread aborted 
showSplashThread = null; 

3. 显示SplashForm的线程函数 
/// 
/// Thread to show the splash. 
/// 
private void ShowSplash) 

SplashForm sForm = null; 
try 

sForm = new SplashForm); 
sForm.ShowDialog); 

catch ThreadAbortException e) 

// Thread was aborted normally 
if _log.IsDebugEnabled) 

_log.Debug”Splash window was aborted normally: ” + e.Message); 


finally 

sForm = null; 

4. 在主窗体的Load事件加激活自己的代码 
SetForegroundWindowProcess.GetCurrentProcess).MainWindowHandle); 

在使用SetForegroundWindow之前先声明一下 
// Uses to active the exist window 
[DllImport”User32.dll”)] 
public static extern void SetForegroundWindowIntPtr hwnd);   

对于需要加载很多组件的应用程序来说,在启动的时候会非常的缓慢,可能会让用户误以为程序已经死掉,这显然不是我们希望看到的。如果能够在启动的时候动态的给用户一些反馈信息(比如当前正在加载的项),那么就可以有效的避免这一问题,并且可以给我们的应用程序增色不少。下边的图片是此代码的效果图。
WinForm下Splash启动画面制作-风君子博客 
下面是部分代码:
AppStart 类,包含Main方法
WinForm下Splash启动画面制作-风君子博客public class AppStart
WinForm下Splash启动画面制作-风君子博客
{
WinForm下Splash启动画面制作-风君子博客    
public AppStart)
WinForm下Splash启动画面制作-风君子博客    
{
WinForm下Splash启动画面制作-风君子博客    }

WinForm下Splash启动画面制作-风君子博客    [STAThread]
WinForm下Splash启动画面制作-风君子博客    
static void Mainstring[] args)
WinForm下Splash启动画面制作-风君子博客    
{
WinForm下Splash启动画面制作-风君子博客        
//    显示Splash窗体
WinForm下Splash启动画面制作-风君子博客
        Splash.Show);
WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客        DoStartupargs);
WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客        
//    关闭Splash窗体
WinForm下Splash启动画面制作-风君子博客
        Splash.Close);
WinForm下Splash启动画面制作-风君子博客    }

WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客    
static void DoStartupstring[] args)
WinForm下Splash启动画面制作-风君子博客    
{
WinForm下Splash启动画面制作-风君子博客        
//    做需要的事情
WinForm下Splash启动画面制作-风君子博客
        frmMain f = new frmMain);
WinForm下Splash启动画面制作-风君子博客        Application.Runf);
WinForm下Splash启动画面制作-风君子博客    }

WinForm下Splash启动画面制作-风君子博客}

Splash功能类:
WinForm下Splash启动画面制作-风君子博客public class Splash
WinForm下Splash启动画面制作-风君子博客
{
WinForm下Splash启动画面制作-风君子博客    
static frmSplash MySplashForm = null;
WinForm下Splash启动画面制作-风君子博客    
static Thread MySplashThread = null;
WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客    
static void ShowThread) 
WinForm下Splash启动画面制作-风君子博客    
{
WinForm下Splash启动画面制作-风君子博客        MySplashForm 
= new frmSplash);
WinForm下Splash启动画面制作-风君子博客        Application.RunMySplashForm);
WinForm下Splash启动画面制作-风君子博客    }

WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客    
static public void Show) 
WinForm下Splash启动画面制作-风君子博客    
{
WinForm下Splash启动画面制作-风君子博客        
if MySplashThread != null)
WinForm下Splash启动画面制作-风君子博客            
return;
WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客        MySplashThread 
= new Threadnew ThreadStartSplash.ShowThread));
WinForm下Splash启动画面制作-风君子博客        MySplashThread.IsBackground 
= true;
WinForm下Splash启动画面制作-风君子博客        MySplashThread.ApartmentState 
= ApartmentState.STA;
WinForm下Splash启动画面制作-风君子博客        MySplashThread.Start);
WinForm下Splash启动画面制作-风君子博客    }

WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客    
static public void Close) 
WinForm下Splash启动画面制作-风君子博客    
{
WinForm下Splash启动画面制作-风君子博客        
if MySplashThread == nullreturn;
WinForm下Splash启动画面制作-风君子博客        
if MySplashForm == nullreturn;
WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客        
try 
WinForm下Splash启动画面制作-风君子博客        
{
WinForm下Splash启动画面制作-风君子博客            MySplashForm.Invoke
new MethodInvokerMySplashForm.Close));
WinForm下Splash启动画面制作-风君子博客        }

WinForm下Splash启动画面制作-风君子博客        
catch Exception) 
WinForm下Splash启动画面制作-风君子博客        
{
WinForm下Splash启动画面制作-风君子博客        }

WinForm下Splash启动画面制作-风君子博客        MySplashThread 
= null;
WinForm下Splash启动画面制作-风君子博客        MySplashForm 
= null;
WinForm下Splash启动画面制作-风君子博客    }

WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客    
static public string Status 
WinForm下Splash启动画面制作-风君子博客    
{
WinForm下Splash启动画面制作-风君子博客        
set 
WinForm下Splash启动画面制作-风君子博客        
{
WinForm下Splash启动画面制作-风君子博客            
if MySplashForm == null
WinForm下Splash启动画面制作-风君子博客            
{
WinForm下Splash启动画面制作-风君子博客                
return;
WinForm下Splash启动画面制作-风君子博客            }

WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客            MySplashForm.StatusInfo 
= value;
WinForm下Splash启动画面制作-风君子博客        }

WinForm下Splash启动画面制作-风君子博客        
get 
WinForm下Splash启动画面制作-风君子博客        
{
WinForm下Splash启动画面制作-风君子博客            
if MySplashForm == null
WinForm下Splash启动画面制作-风君子博客            
{
WinForm下Splash启动画面制作-风君子博客                
throw new InvalidOperationExceptionSplash Form not on screen);
WinForm下Splash启动画面制作-风君子博客            }

WinForm下Splash启动画面制作-风君子博客            
return MySplashForm.StatusInfo;
WinForm下Splash启动画面制作-风君子博客        }

WinForm下Splash启动画面制作-风君子博客    }

}

Splash 界面类:

WinForm下Splash启动画面制作-风君子博客public class frmSplash : System.Windows.Forms.Form
WinForm下Splash启动画面制作-风君子博客
{
WinForm下Splash启动画面制作-风君子博客    
private string _StatusInfo = “”;
WinForm下Splash启动画面制作-风君子博客    
WinForm下Splash启动画面制作-风君子博客    
public frmSplash)
WinForm下Splash启动画面制作-风君子博客    
{
WinForm下Splash启动画面制作-风君子博客        InitializeComponent);
WinForm下Splash启动画面制作-风君子博客    }

WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客    
private void InitializeComponent)
WinForm下Splash启动画面制作-风君子博客    
{
WinForm下Splash启动画面制作-风君子博客        
// WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客
        this.pictureBox1.Image = System.Drawing.Image)resources.GetObjectpictureBox1.Image)));        
WinForm下Splash启动画面制作-风君子博客        
//WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客

WinForm下Splash启动画面制作-风君子博客    }

WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客    
public string StatusInfo 
WinForm下Splash启动画面制作-风君子博客    
{
WinForm下Splash启动画面制作-风君子博客        
set 
WinForm下Splash启动画面制作-风君子博客        
{
WinForm下Splash启动画面制作-风君子博客            _StatusInfo 
= value;
WinForm下Splash启动画面制作-风君子博客            ChangeStatusText);
WinForm下Splash启动画面制作-风君子博客        }

WinForm下Splash启动画面制作-风君子博客        
get 
WinForm下Splash启动画面制作-风君子博客        
{
WinForm下Splash启动画面制作-风君子博客            
return _StatusInfo;
WinForm下Splash启动画面制作-风君子博客        }

WinForm下Splash启动画面制作-风君子博客    }

WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客    
public void ChangeStatusText) 
WinForm下Splash启动画面制作-风君子博客    
{
WinForm下Splash启动画面制作-风君子博客        
try 
WinForm下Splash启动画面制作-风君子博客        
{
WinForm下Splash启动画面制作-风君子博客            
if this.InvokeRequired) 
WinForm下Splash启动画面制作-风君子博客            
{
WinForm下Splash启动画面制作-风君子博客                
this.Invokenew MethodInvokerthis.ChangeStatusText));
WinForm下Splash启动画面制作-风君子博客                
return;
WinForm下Splash启动画面制作-风君子博客            }

WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客            labStatus.Text 
= _StatusInfo;
WinForm下Splash启动画面制作-风君子博客        }

WinForm下Splash启动画面制作-风君子博客        
catch Exception e) 
WinForm下Splash启动画面制作-风君子博客        
{
WinForm下Splash启动画面制作-风君子博客            
//    异常处理
WinForm下Splash启动画面制作-风君子博客
        }

WinForm下Splash启动画面制作-风君子博客    }

}

主界面类:

WinForm下Splash启动画面制作-风君子博客public class frmMain : System.Windows.Forms.Form
WinForm下Splash启动画面制作-风君子博客
{
WinForm下Splash启动画面制作-风君子博客    
public frmMain)
WinForm下Splash启动画面制作-风君子博客    
{
WinForm下Splash启动画面制作-风君子博客        InitializeComponent);
WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客    Splash.Status 
= 状态:载入初始化模块WinForm下Splash启动画面制作-风君子博客;
WinForm下Splash启动画面制作-风君子博客            System.Threading.Thread.Sleep
1000);
WinForm下Splash启动画面制作-风君子博客            
WinForm下Splash启动画面制作-风君子博客    Splash.Status 
= 状态:载入管理模块WinForm下Splash启动画面制作-风君子博客;
WinForm下Splash启动画面制作-风君子博客            System.Threading.Thread.Sleep
1000);
WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客    Splash.Status 
= 状态:载入打印模块WinForm下Splash启动画面制作-风君子博客;
WinForm下Splash启动画面制作-风君子博客            System.Threading.Thread.Sleep
1000);
WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客    Splash.Status 
= 状态:载入插件模块WinForm下Splash启动画面制作-风君子博客;
WinForm下Splash启动画面制作-风君子博客            System.Threading.Thread.Sleep
1000);
WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客    Splash.Status 
= 状态:连接数据库WinForm下Splash启动画面制作-风君子博客;
WinForm下Splash启动画面制作-风君子博客            System.Threading.Thread.Sleep
1000);
WinForm下Splash启动画面制作-风君子博客
WinForm下Splash启动画面制作-风君子博客    Splash.Close);
WinForm下Splash启动画面制作-风君子博客    }

}