C# WinForm - loading screen

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

I would like to ask how to make a loading screen (just a picture or something) that appears while the program is being loaded, and disappears when the program has finished loading.

in fancier versions, I have seen the process bar (%) displayed. how can you have that, and how do you calculate the % to show on it?

I know there is a Form_Load() event, but I do not see a Form_Loaded() event, or the % as a property / attribute anywhere.

回答1:

all you need to create one form as splash screen and show it before you main start showing the landing page and close this splash once the landing page loaded.

using System.Threading; using System.Windows.Forms;  namespace MyTools {     public class SplashForm : Form     {         //Delegate for cross thread call to close         private delegate void CloseDelegate();          //The type of form to be displayed as the splash screen.         private static SplashForm splashForm;          static public void ShowSplashScreen()         {             // Make sure it is only launched once.              if (splashForm != null)                 return;             Thread thread = new Thread(new ThreadStart(SplashForm.ShowForm));             thread.IsBackground = true;             thread.SetApartmentState(ApartmentState.STA);             thread.Start();         }          static private void ShowForm()         {             splashForm = new SplashForm();             Application.Run(splashForm);         }          static public void CloseForm()         {             splashForm.Invoke(new CloseDelegate(SplashForm.CloseFormInternal));         }          static private void CloseFormInternal()         {             splashForm.Close();             splashForm = null;         }     } }

and the main program function looks like this:

[STAThread] static void Main(string[] args) {     SplashForm.ShowSplashScreen();     MainForm mainForm = new MainForm(); //this takes ages     SplashForm.CloseForm();     Application.Run(mainForm); }


回答2:

If you're going to show the SplashForm more than once in your application, be sure to set the splashForm variable to null otherwise you'll get an error.

static private void CloseFormInternal() {     splashForm.Close();     splashForm = null; }


转载请标明出处:C# WinForm - loading screen
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!