2011-11-12 46 views
0

在加載應用程序時,是否可以使用.Net中的SplashScreen類顯示動態消息?啓動畫面和自定義消息

有點像。
模塊一加載...
模塊二加載...
等等。

回答

2

不,您需要自己編寫此功能。內置的啓動畫面只能顯示靜態圖像。

2

您可以使用'System.Theading'來做到這一點。下面的代碼在單獨的線程上啓動一個「啓動畫面」,而你的應用程序(在我的下面的例子中稱爲MainForm())加載或初始化。首先在你的「main()」方法中(在你的program.cs文件中,除非你重命名了它),你應該顯示你的啓動畫面。這將是一個WinForm或WPF表單,您希望在啓動時向用戶顯示。這是主要推出()如下:

[STAThread] 
    static void Main() 
    { 
     // Splash screen, which is terminated in MainForm.  
     SplashForm.ShowSplash(); 

     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 

     // Run UserCost. 
    Application.Run(new MainForm()); 
    } 

在你的閃屏的代碼,你需要像下面這樣:

public partial class SplashForm : Form 
{ 

    // Thredding. 
    private static Thread _splashThread; 
    private static SplashForm _splashForm;  

    public SplashForm() 
    { 
     InitializeComponent(); 
    } 

    // Show the Splash Screen (Loading...)  
    public static void ShowSplash()  
    {   
     if (_splashThread == null)   
     {    
      // show the form in a new thread    
      _splashThread = new Thread(new ThreadStart(DoShowSplash));    
      _splashThread.IsBackground = true;    
      _splashThread.Start();   
     }  
    }  

    // Called by the thread  
    private static void DoShowSplash()  
    {   
     if (_splashForm == null)    
      _splashForm = new SplashForm();   
     // create a new message pump on this thread (started from ShowSplash)   
     Application.Run(_splashForm); 
    }  

    // Close the splash (Loading...) screen  
    public static void CloseSplash()  
    {   
     // Need to call on the thread that launched this splash   
     if (_splashForm.InvokeRequired)    
      _splashForm.Invoke(new MethodInvoker(CloseSplash));   
     else    
      Application.ExitThread();  
    } 

} 

這將啓動飛濺的形式在一個單獨的後臺線程允許你繼續同時呈現您的主應用程序。要顯示關於加載的消息,您必須從主UI線程獲取信息,或者以純粹的審美性質工作。爲了完成並關閉閃屏下來的時候你的應用程序已被初始化你把默認的構造函數中以下(你可以重載的構造函數,如果你想):

上面的代碼應該是比較容易跟隨。

我希望這會有所幫助。