2015-09-05 55 views
0

我想让首次出现的初始屏幕出现在飞溅之后,出现MainForm。但是我在启动画面中的进度条并没有到达酒吧的尽头。程序继续运行,不起作用。加载主表单时显示初始屏幕

如何在加载主窗体时显示启动画面?

我的代码这是类似的东西:

public partial class SplashForm : Form 
{ 
    public SplashForm() 
    { 
     InitializeComponent(); 
    } 
    private void SplashForm_Load(object sender, EventArgs e) 
    { 
     timer1.Enabled = true; 
     timer1.Start(); 
     timer1.Interval = 1000; 
     progressBar1.Maximum = 10; 
     timer1.Tick += new EventHandler(timer1_Tick); 
    } 
    public void timer1_Tick(object sender, EventArgs e) 
    { 
     if (progressBar1.Value != 10) 
     { 
      progressBar1.Value++; 
     } 
     else 
     { 
      timer1.Stop(); 
      Application.Exit(); 
     } 
    }  
} 

这里是MainForm的代码的第一部分:

public partial class MainForm : Form 
{ 
    public MainForm() 
    { 
     InitializeComponent(); 
     Application.Run(new SplashForm()); 
    } 
} 
+0

可以显示创建此表单的代码吗? pbcarrega在哪里初始化? –

+0

pbcarrega是ProgressBar的名称 –

+0

此代码是Form Splash Screen的代码,但您想了解Form Main? –

回答

3

有创建闪屏的方式不同。最好将显示和关闭启动画面的逻辑与主窗体的逻辑分开。

为此,您可以创建LoadCompleted事件,然后在Program类中订阅它,并在其中显示并关闭闪屏。

这里是什么上述我的实现:

1-在您MainForm,添加LoadCompleted事件,然后覆盖OnLoad方法引发事件。 (大概Shown事件是不是适用我们的自定义事件。)

public event EventHandler LoadCompleted; 
protected override void OnLoad(EventArgs e) 
{ 
    base.OnLoad(e); 
    this.OnLoadCompleted(EventArgs.Empty); 
} 
protected virtual void OnLoadCompleted(EventArgs e) 
{ 
    var handler = LoadCompleted; 
    if (handler != null) 
     handler(this, e); 
} 
private void MainForm_Load(object sender, EventArgs e) 
{ 
    //Just for test, you can make a delay to simulate a time-consuming task 
    //In a real application here you load your data and other settings 
} 

2-在Program类,显示SplashForm则认购LoadCompleted事件您MainForm和显示MainForm,然后在LoadCompleted,靠近SplashForm

static class Program 
{ 
    static SplashForm mySplashForm; 
    static MainForm myMainForm; 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     //Show Splash Form 
     mySplashForm = new SplashForm(); 
     if (mySplashForm != null) 
     { 
      Thread splashThread = new Thread(new ThreadStart(
       () => { Application.Run(mySplashForm); })); 
      splashThread.SetApartmentState(ApartmentState.STA); 
      splashThread.Start(); 
     } 
     //Create and Show Main Form 
     myMainForm = new MainForm(); 
     myMainForm.LoadCompleted += MainForm_LoadCompleted; 
     Application.Run(minScreen); 
    } 
    private static void MainForm_LoadCompleted(object sender, EventArgs e) 
    { 
     if (mySplashForm == null || mySplashForm.Disposing || mySplashForm.IsDisposed) 
      return; 
     mySplashForm.Invoke(new Action(() => { mySplashForm.Close(); })); 
     mySplashForm.Dispose(); 
     mySplashForm = null; 
     myMainForm.Activate(); 
    } 
} 
+0

你可以把这个代码与我的形式和变量的名称? –

+0

我改变了名字,对你更友善。现在使用这个代码和你的表单名称,它足以查找和替换所有SplashForm的名称,你想要的飞溅形式(FormSplash),并找到并替换所有主窗体的名称,你想要为您的主窗体( Form1中)。 –

+0

我可以使用这个相同的代码改变ProgressBar中使用的一些部分?因为Thread.Sleep我现在不会使用。 –