如何在Winforms中完全加载程序之前显示启动画面?

时间:2013-09-07 19:50:07

标签: c# winforms

如何在程序完全加载之前显示启动画面,然后自动关闭并显示主窗体!

1 个答案:

答案 0 :(得分:1)

您可以在第二个线程上创建启动,当您的应用程序启动时,您可以卸载第二个线程

public partial class SplashForm : Form
{
    public SplashForm()
   {
    InitializeComponent();
   }
   //The type of form to be displayed as the splash screen.
   private static SplashForm splashForm;

   static public void Show(string txt)
   {
       // Make sure it is only launched once.

       if (splashForm != null)
       {
           splashForm.BeginInvoke(new MethodInvoker(delegate { splashForm.label1.Text =       txt; }));

           return;
       }

       Thread thread = new Thread((ThreadStart)delegate { ShowForm(txt); });
       thread.IsBackground = true;
       thread.SetApartmentState(ApartmentState.STA);
       thread.Start();
   }
   static private void ShowForm(string txt)
   {
       splashForm = new SplashForm();

       splashForm.label1.Text = txt;

       Application.Run(splashForm);
   }

   //Delegate for cross thread call to close
   private delegate void CloseDelegate();
   static public void CloseForm()
   {
       splashForm.Invoke(new CloseDelegate(SplashForm.CloseFormInternal));
   }
   static private void CloseFormInternal()
   {
       splashForm.Close();
   }

} 希望这个帮助