WinForm下Splash(启动画面)制作
原文如下:
本代碼可以依據主程序加載進度來顯示Splash。
static class Program
{
/// <summary>
/// 主程序的入口點在此設置,包括一些初始化操作,啟動窗體等
/// </summary>
private static ApplicationContext context;
[STAThread]
static void Main()
{
Application.EnableVisualStyles(); //樣式設置
Application.SetCompatibleTextRenderingDefault(false); //樣式設置
Splash sp = new Splash(); //啟動窗體
sp.Show(); //顯示啟動窗體
context = new ApplicationContext();
context.Tag = sp;
Application.Idle += new EventHandler(Application_Idle); //注冊程序運行空閑去執行主程序窗體相應初始化代碼
Application.Run(context);
}
//初始化等待處理函數
private static void Application_Idle(object sender, EventArgs e)
{
Application.Idle -= new EventHandler(Application_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 Thread(new ThreadStart(ShowSplash));
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事件加激活自己的代碼
SetForegroundWindow(Process.GetCurrentProcess().MainWindowHandle);
在使用SetForegroundWindow之前先聲明一下
// Uses to active the exist window
[DllImport("User32.dll")]
public static extern void SetForegroundWindow(IntPtr hwnd);
對于需要加載很多組件的應用程序來說,在啟動的時候會非常的緩慢,可能會讓用戶誤以為程序已經死掉,這顯然不是我們希望看到的。如果能夠在啟動的時候動態的給用戶一些反饋信息(比如當前正在加載的項),那么就可以有效的避免這一問題,并且可以給我們的應用程序增色不少。下邊的圖片是此代碼的效果圖。
下面是部分代碼:
AppStart 類,包含Main方法
publicclassAppStart
{
publicAppStart()
{
}
[STAThread]
staticvoidMain(string[]args)
{
//顯示Splash窗體
Splash.Show();
DoStartup(args);
//關閉Splash窗體
Splash.Close();
}
staticvoidDoStartup(string[]args)
{
//做需要的事情
frmMainf=newfrmMain();
Application.Run(f);
}
}
Splash功能類:
publicclassSplash
{
staticfrmSplashMySplashForm=null;
staticThreadMySplashThread=null;
staticvoidShowThread()
{
MySplashForm=newfrmSplash();
Application.Run(MySplashForm);
}
staticpublicvoidShow()
{
if(MySplashThread!=null)
return;
MySplashThread=newThread(newThreadStart(Splash.ShowThread));
MySplashThread.IsBackground=true;
MySplashThread.ApartmentState=ApartmentState.STA;
MySplashThread.Start();
}
staticpublicvoidClose()
{
if(MySplashThread==null)return;
if(MySplashForm==null)return;
try
{
MySplashForm.Invoke(newMethodInvoker(MySplashForm.Close));
}
catch(Exception)
{
}
MySplashThread=null;
MySplashForm=null;
}
staticpublicstringStatus
{
set
{
if(MySplashForm==null)
{
return;
}
MySplashForm.StatusInfo=value;
}
get
{
if(MySplashForm==null)
{
thrownewInvalidOperationException("SplashFormnotonscreen");
}
returnMySplashForm.StatusInfo;
}
}
}
Splash 界面類:
publicclassfrmSplash:System.Windows.Forms.Form
{
privatestring_StatusInfo="";
publicfrmSplash()
{
InitializeComponent();
}
privatevoidInitializeComponent()
{
//
this.pictureBox1.Image=((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
//
}
publicstringStatusInfo
{
set
{
_StatusInfo=value;
ChangeStatusText();
}
get
{
return_StatusInfo;
}
}
publicvoidChangeStatusText()
{
try
{
if(this.InvokeRequired)
{
this.Invoke(newMethodInvoker(this.ChangeStatusText));
return;
}
labStatus.Text=_StatusInfo;
}
catch(Exceptione)
{
//異常處理
}
}
}
主界面類:
publicclassfrmMain:System.Windows.Forms.Form
{
publicfrmMain()
{
InitializeComponent();
Splash.Status="狀態:載入初始化模塊";
System.Threading.Thread.Sleep(1000);
Splash.Status="狀態:載入管理模塊";
System.Threading.Thread.Sleep(1000);
Splash.Status="狀態:載入打印模塊";
System.Threading.Thread.Sleep(1000);
Splash.Status="狀態:載入插件模塊";
System.Threading.Thread.Sleep(1000);
Splash.Status="狀態:連接數據庫";
System.Threading.Thread.Sleep(1000);
Splash.Close();
}
}
總結
以上是生活随笔為你收集整理的WinForm下Splash(启动画面)制作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 如何修改百度导航诱导界面
- 下一篇: android使用桢布局,Android