2016-09-12 300 views
1

我有一個.Net Winform應用程序,需要一些時間來加載。初始屏幕Winforms

我希望我的應用程序通過閃屏up.Basically打開時創建一個閃屏,當我點擊我的應用程序將啓動,並關閉我想我的箴言報應用

閃屏的初始化過程中應一個不同的winform,並應該在單獨的線程中運行。

我想在啓動畫面上有一個叫做「停止等待」的按鈕,用戶可以點擊它來取消打開應用程序,我應該可以關閉應用程序。

同樣在啓動畫面中,我需要顯示應用程序初始化的進度。

我在Splash Screen上的知識非常有限。請給我幾個例子來繼續我的要求嗎?

實現開機畫面後一個問題: 我的應用程序初始化發生在不同的項目中主要Screen.Is不同的項目中使用它,所以我已經創建了一個閃屏一個單獨的EXE,它很好的做法,添加啓動畫面(EXE )作爲主屏幕中的參考,並使用Splash Screen中的方法?

在此先感謝您的幫助。

+0

你可以使用[這樣的解決方案](http://stackoverflow.com/questions/32418695/show-splash-screen-during-loading-the-main-form)。然後在啓動畫面中,點擊時有足夠的按鈕調用'Application.Exit();'。 –

+0

你知道你的應用程序的主要形式的哪部分代碼需要長時間完成嗎? – Niklas

回答

2

這是很多問題,我會回答「如何創建啓動畫面」,然後您可以嘗試進一步取消並顯示初始化進度。

您基本上創建了另一個表單作爲Splash形式。

設置屬性:

  • 中StartPosition = FormStartPosition.CenterScreen
  • MaximizeBox =假
  • FormBorderStyle = FormBorderStyle.None
  • 如果你想

然後加載的BackgroundImage在Main顯示主窗口之前,先創建啓動畫面,顯示它並傳遞參考主窗口對象

Splash splash = new Splash(); 
splash.Show(); 

MainWindow wnd = new MainWindow(splash); 

而且主窗口的你MainWindowLoad_Load函數內部:

public MainWindow(Splash splash) 
{ 
    mSplash = splash; 
} 
private void MainWindow_Load(object sender, System.EventArgs e) 
{ 
    // do long initialisation, and when done.... 

    mSplash.Close(); 
    mSplash = null; 

} 
+0

這是如何在不同的線程中運行的,以及在長時間初始化發生時如何阻止mSplash窗體上的UI線程? – Niklas

+0

@ServéLaurijssen我的應用程序初始化發生在不同的項目中,所以我爲Splash Screen創建了一個獨立的exe文件,並在主Screen的不同項目中使用它。在主屏幕中添加啓動畫面(exe)作爲參考並使用方法在閃屏? – reddy

0

由於閃屏是關於外觀和感覺,我會建議你讓UI線程處理它,並將你的代碼放到另一個執行線程中,否則改變啓動屏幕顯示的任何內容都需要BeginInvoke。因此
這是我做的方式:

private void MainForm_Load(object sender, EventArgs e) 
{ 
    LoadingForm _loading = new LoadingForm(); 
    BackgroundWorker _worker = new BackgroundWorker { WorkerReportsProgress = true }; 
    _worker.ProgressChanged += (o, z) => 
    { 
     _loading.progressBar1.Minimum = 0; 
     _loading.progressBar1.Maximum = 100; 
     _loading.progressBar1.Value = (z as ProgressChangedEventArgs).ProgressPercentage; 
    }; 
    _worker.RunWorkerCompleted += (fWorker, fargs) => 
    { 
     _loading.Dispose();// this is executed when all the code in dowork event is completed,disposing the loading form and continuing your code 
    }; 
    _worker.DoWork += (workerO, workerArgs) => 
    { 
     // execute your code here , update progressbar as you go: 
     int yourProgress = 0;//need to generate this in your code as percentage 0-100 
     _worker.ReportProgress(yourProgress); 


     //////////////////////////just to show progress change////////////////////////////// 
     for (int i = 0; i < 5; i++) 
     { 
      Thread.Sleep(1500); 
      yourProgress += 20; 
      _worker.ReportProgress(yourProgress); 
     } 
     Thread.Sleep(3000); 
     //////////////////////////////////////////////////////////////////////////////// 

    }; 
    _worker.RunWorkerAsync(); 



    _loading.ShowDialog(this); 


} 

注意,你需要讓你的進度控制的修飾符public,爲了讓您的BackgroundWorker改變它的比例。

+0

我的應用程序初始化發生在不同的項目中,所以我爲Splash Screen創建了一個獨立的exe文件,並在主屏幕的不同項目中使用它。在主屏幕中添加啓動屏幕(exe)作爲參考並在啓動屏幕中使用方法? – reddy