2009-09-01 11 views
1

[OK]を、私は、これは前に百万回を頼まれました(そして、人々はまた、非常に同じようXDで自分のStackOverflowの質問を始める)知っているが、私はどのように知っていただきたいと思います以下を実現:リサイズアプリでスプラッシュ画面ごとに異なる要件

  1. アプリケーションは、最初のログインに成功すると、その後、スプラッシュ画面が(別のスレッドで)示さなければならない
  2. ログインボックスを起動します。
  3. スプラッシュ画面が表示されている間に、クラスオブジェクトは、それが初期化する。例えば(何をしているか、ユーザーに戻って表示しながら、DBからユーザー固有のデータの大量で(つまり、Singletonパターンに準拠)満たされていなければなりません。 ..loading data ... loading preferences ... rendering workspace ... done!)
  4. スプラッシュ画面は、メインフォームがメインスレッドで初期化を完了するのを待ってから最後に処理されます。アプリケーションのための欲求の流れである

。メインフォームを閉じると、ユーザーはログインボックスに戻されます。

私はリサイズのものの多くにアップcluedそのすべてではないけど、質問のこれらの種類を尋ねて、私はゆっくりと学んだことを先行述べなければなりません。私はスレッディングに関するいくつかの読書をしており、スプラッシュ画面は独自のスレッドで生成され、メインスレッドからデリゲート(UIへのクロススレッド更新を処理するため)を使用してフィードを更新する必要があり、これはすべてProgram.cs "Main()"サブルーチンで実行する必要があります。私も原因(メインフォームが閉じているときに、次に続く)最初のログインフォームショーを持つの追加要件に、開始する場所がわからないよう

私は、ここに手を差し伸べるしています。この点について私は確かに援助を大切にしています。

多くの感謝! sha

答えて

2

これを行う方法の簡単な例を示します。トリックは、最初に開いて最後に閉じるものなので、ログインボックスをメインフォームにすることです。この例で

、LoginScreen形態は、一つのボタン、クリックしたときOnOK呼び出し()メソッドを呼び出すOKボタンを有しています。

public partial class LoginScreen : System.Windows.Forms.Form 
{ 
    ApplicationWindow window; 

    public LoginScreen() 
    { 
     InitializeComponent(); 
    } 
    private void OnFormClosed(object sender, FormClosedEventArgs e) 
    { 
     this.Show(); 
    } 
    private void OnOK(object sender, EventArgs e) 
    { 
     this.Hide(); 

     window = new ApplicationWindow(); 
     window.FormClosed += OnFormClosed; 
     window.Show(); 
    } 
} 

ApplicationWindowフォームは、「メイン」フォームと呼ばれるものと同じです。それはSplashFormを起動するものです。

public partial class ApplicationWindow : System.Windows.Forms.Form 
{ 
    public ApplicationWindow() 
    { 
     SplashForm.Show(500); 

     InitializeComponent(); 
    } 

    private void OnLoad(object sender, EventArgs e) 
    { 
     // Simulate doing a lot of work here. 
     System.Threading.Thread.Sleep(1000); 

     SplashForm.Hide(); 

     Show(); 
     Activate(); 
    } 
} 

ここに私が使用するスプラッシュフォームのコピーがあります。静的なShow()メソッドで指定したミリ秒数に基づいてフェードインおよびフェードアウトします。

public partial class SplashForm : Form 
{ 
    #region Public Methods 

    /// <summary> 
    /// Shows the splash screen with no fading effects. 
    /// </summary> 
    public new static void Show() 
    { 
     Show(0); 
    } 

    /// <summary> 
    /// Shows the splash screen. 
    /// </summary> 
    /// <param name="fadeTimeInMilliseconds">The time to fade 
    /// in the splash screen in milliseconds.</param> 
    public static void Show(int fadeTimeInMilliseconds) 
    { 
     // Only show the splash screen once. 
     if (_instance == null) { 
      _fadeTime = fadeTimeInMilliseconds; 
      _instance = new SplashForm(); 

      // Hide the form initially to avoid any pre-paint flicker. 
      _instance.Opacity = 0; 
      ((Form) _instance).Show(); 

      // Process the initial paint events. 
      Application.DoEvents(); 

      if (_fadeTime > 0) { 
       // Calculate the time interval that will be used to 
       // provide a smooth fading effect. 
       int fadeStep = (int) Math.Round((double) _fadeTime/20); 
       _instance.fadeTimer.Interval = fadeStep; 

       // Perform the fade in. 
       for (int ii = 0; ii <= _fadeTime; ii += fadeStep) { 
        Thread.Sleep(fadeStep); 
        _instance.Opacity += 0.05; 
       } 
      } else { 
       // Use the Tag property as a flag to indicate that the 
       // form is to be closed immediately when the user calls 
       // Hide(); 
       _instance.fadeTimer.Tag = new object(); 
      } 

      _instance.Opacity = 1; 
     } 
    } 

    /// <summary> 
    /// Closes the splash screen. 
    /// </summary> 
    public new static void Hide() 
    { 
     if (_instance != null) { 
      // Invoke the Close() method on the form's thread. 
      _instance.BeginInvoke(new MethodInvoker(_instance.Close)); 

      // Process the Close message on the form's thread. 
      Application.DoEvents(); 
     } 
    } 

    #endregion Public Methods 

    #region Constructors 

    /// <summary> 
    /// Initializes a new instance of the SplashForm class. 
    /// </summary> 
    public SplashForm() 
    { 
     InitializeComponent(); 

     Size = BackgroundImage.Size; 

     // If transparency is ever needed, set the color of the desired 
     // transparent portions of the bitmap to fuschia and then 
     // uncomment this code. 
     //Bitmap bitmap = new Bitmap(this.BackgroundImage); 
     //bitmap.MakeTransparent(System.Drawing.Color.Fuchsia); 
     //this.BackgroundImage = bitmap; 
    } 

    #endregion Constructors 

    #region Protected Methods 

    protected override void OnClosing(CancelEventArgs e) 
    { 
     base.OnClosing(e); 

     // Check to see if the form should be closed immediately. 
     if (fadeTimer.Tag != null) { 
      e.Cancel = false; 
      _instance = null; 
      return; 
     } 

     // Only use the timer to fade if the form is running. 
     // Otherwise, there will be no message pump. 
     if (Application.OpenForms.Count > 1) { 
      if (Opacity > 0) { 
       e.Cancel = true; // prevent the form from closing 
       Opacity -= 0.05; 

       // Use the timer to iteratively call the Close method. 
       fadeTimer.Start(); 
      } else { 
       fadeTimer.Stop(); 

       e.Cancel = false; 
       _instance = null; 
      } 
     } else { 
      if (Opacity > 0) { 
       Thread.Sleep(fadeTimer.Interval); 
       Opacity -= 0.05; 
       Close(); 
      } else { 
       e.Cancel = false; 
       _instance = null; 
      } 
     } 
    } 

    #endregion Protected Methods 

    #region Private Methods 

    private void OnTick(object sender, EventArgs e) 
    { 
     Close(); 
    } 

    #endregion Private Methods 

    #region Private Fields 

    private static SplashForm _instance = null; 
    private static int _fadeTime = 0; 

    #endregion Private Fields 
} 

SplashForm次のプロパティ値を持つだけで空白のフォームは次のとおりです。

  • 背景画像=(お好みの画像)
  • BackgroundImageLayout =センター
  • DoubleBuffered =真
  • FormBorderStyle = None
  • ShowInTaskbar = False
  • 開始位置=画面の中央
  • TopMostの=真

また、デフォルトのプロパティでfadeTimerという名前のSystem.Windows.Forms.Timerコントロールが含まれています。 Tickイベントは、OnTick()メソッドを呼び出すように構成されています。

これが行わないのは、読み込みプロセスのステータスを更新することです。多分、他の誰かがあなたのためにその部分を記入することができます。

+0

これは、スプラッシュフォームを別のスレッドで実行するのではなく、もう1つのリンクがそれに役立つはずです。 –

+0

スプラッシュ画面と通信するには、ApplicationWindowでイベントをリッスンするか、読み込みプロセスによってスプラッシュ画面のメソッドが定期的に呼び出されます。スプラッシュ画面が別のスレッドにある場合は、BeginInvokeを使用してください。 –

+0

あなたは正しいです。私はこのコードを見てからしばらくしています。私は正しいように投稿を更新します。ありがとう。 –

関連する問題