2013-06-20 32 views
0

我有一個SplashScreen,MainForm在加載表單內容時顯示一個閃屏

在我的MainForm_Load我有一個名爲Connect();的方法。這種方法驗證了我的RFID設備與串口的連接,它需要幾秒鐘才能完成。

雖然它經歷了Connect()方法,但我想顯示我的SplashScreen。我嘗試這樣做:

private void Main_Load(object sender, EventArgs e) 
{ 
    Frm_Splash s = new Frm_Splash(); 
    s.Show(); 
    Connect();    
} 

Connect();的方法,示出了使用MessageBox的消息。 但是當SplashScreen結束時,它會自行封閉並關閉MessageBox。

這裏是我的SplashScreen表單代碼:

private void timer1_Tick(object sender, EventArgs e) 
{ 
    if (pbLoad.Value < 100) 
     { 
      pbLoad.Value = pbLoad.Value + 1; 
     } 
    else 
     { 
      timer1.Enabled = false; 
      this.Close(); 
     } 
} 

我知道它有什麼做的this.Close();。我只是不知道如何解決它。

也許如果我使用this.Visible = false,但然後SplashScreen不會關閉,它仍然會處理,只是將隱身......我認爲有一個更好的選擇。

+1

似乎還有更多。如果'SplashScreen'是它自己的形式,那麼關閉它就無法關閉從另一個表單生成的'MessageBox'。 – DonBoitnott

+0

@DonBoitnott我試過'this.visible = false'而不是'this.Close()'並且工作。但我知道SplashScreen仍然處理它只是無形。你有什麼建議嗎? – Ghaleon

+0

它實際上是關閉消息框還是將其拖動到背景上?你可以嘗試最小化一些東西,或者使用tab來查看消息框是否仍然存在但隱藏? – BlargleMonster

回答

4

我想象你正在使用MessageBox.Show()打開你的MessageBox ... 取而代之的是,使用MessageBox.Show(this,"message");

我想象發生了什麼事是你的消息框家長設置爲開機畫面,因爲這是與焦點對話

+0

工作完美!你能向我解釋爲什麼'this'參數? – Ghaleon

+1

[看這](http://msdn.microsoft.com/en-us/library/cked7698.aspx) - 基本上你的MessageBox有錯誤的父母,它必須採取父母從任何對話有焦點在它是顯示爲 – Sayse

+0

+1感謝您的解釋。 – Ghaleon

1

這裏是我的啓動畫面:

namespace MyNamespace 
{ 
    public partial class frmSplashScreen : Form 
    { 
     private static frmSplashScreen splashScreen = null; 
     private static Thread splashThread = null; 
     private Double opacityInc = .03; 
     private Double opacityDec = .1; 
     private const Int32 iTimerInterval = 30; 

     public frmSplashScreen() 
     { 
      InitializeComponent(); 

      Opacity = .0; 
      timer1.Interval = iTimerInterval; 
      timer1.Start(); 
     } 

     private void frmSplashScreen_Load(Object sender, EventArgs e) 
     { 
      CenterToScreen(); 
     } 

     public static void ShowSplashScreen() 
     { 
      if (splashScreen != null) 
       return; 
      splashThread = new Thread(new ThreadStart(frmSplashScreen.ShowForm)); 
      splashThread.IsBackground = true; 
      splashThread.SetApartmentState(ApartmentState.STA); 
      splashThread.Start(); 
     } 

     private static void ShowForm() 
     { 
      splashScreen = new frmSplashScreen(); 
      Application.Run(splashScreen); 
     } 

     public static void CloseForm() 
     { 
      if (splashScreen != null) 
       splashScreen.opacityInc = -splashScreen.opacityDec; 
      splashThread = null; 
      splashScreen = null; 
     } 

     private void timer1_Tick(Object sender, EventArgs e) 
     { 
      if (opacityInc > 0) 
      { 
       if (Opacity < 1) 
        Opacity += opacityInc; 
      } 
      else 
      { 
       if (Opacity > 0) 
        Opacity += opacityInc; 
       else 
        Close(); 
      } 
     } 
    } 
} 

我在窗體的構造函數中調用這個激活它,我希望它彈出來源:

frmSplashScreen.ShowSplashScreen(); 

然後在Shown關閉相同的形式:

frmSplashScreen.CloseForm(); 

注:我提供這個,因爲它僱用static課程,這有助於緩解所有權問題。

+0

謝謝!我會盡力改進你的一些代碼。 +1 – Ghaleon

+0

[Credit credit due due](http://www.codeproject.com/Articles/5454/A-Pretty-Good-Splash-Screen-in-C)Don ...;) – Sayse

+0

@Sayse。我承認,它最初來自互聯網來源,但不是那個。我的猜測是...這輪比賽很有意思。這是很好的代碼。 – DonBoitnott

相關問題