-7

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

4

1 回答 1

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();
   }

希望这有帮助

于 2013-09-07T20:37:10.147 回答