Making a splash screen

前端 未结 6 1205
无人及你
无人及你 2021-01-06 06:29

The concept of a splash screen doesn\'t strike me as something that should be so complicated, but I\'m having trouble getting the whole splash screen painted.

Let\'s

6条回答
  •  旧巷少年郎
    2021-01-06 07:15

    Like me you probably created this as an afterthought and do not want to go through all the heck of redesigning your code to fit multi-threaded architecture...

    First create a new Form called SpashScreen, in the properties click on BackgroundImage and import whatever image you want. Also set FormBorderStyle to None so that you can't click on the x to close the screen.

        public Form1()
        {
            InitializeComponent();
    
            BackgroundWorker bw = new BackgroundWorker();
            bw.WorkerSupportsCancellation = true;
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerAsync();  // start up your spashscreen thread
            startMainForm();      // do all your time consuming stuff here  
            bw.CancelAsync();     // close your splashscreen thread  
        }
    
    
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
    
            SplashScreen ss = new SplashScreen();
            ss.Show();
    
            while (!worker.CancellationPending) //just hangout and wait
            {
                Thread.Sleep(1000);
            }
    
            if (worker.CancellationPending)
            {
                ss.Close();
                e.Cancel = true;
            }
    
        }
    

    This does not support progress bar or any fancy stuff but I am sure it can be tweaked.

提交回复
热议问题