【问题标题】:How to make a SplashScreen in C# WindowsFormsApplication [duplicate]如何在 C# WindowsFormsApplication 中制作 SplashScreen [重复]
【发布时间】:2013-12-10 21:38:37
【问题描述】:

我想知道如何在 C# 中制作 SplashScreen。我环顾了 StackOverflow,但没有发现任何有用的东西,你能帮帮我吗?我已经有 1 个表格,里面填满了东西,我只需要一些简单的说明:

  • 让一个新表单出现在我的主表单之前
  • 让 SplashScreen 消失,几秒钟后出现主窗体(可能是三秒钟)

我不需要动画启动画面。
提前致谢!

请问您是否需要我提供的任何代码示例 :)

【问题讨论】:

标签: c# winforms splash-screen


【解决方案1】:

这是一个闪屏的例子——我在我的一个项目中使用过——它使用了多线程:

namespace  WindowsForm1
{
    public enum SplashTypeOfMessage
    {   Success,    
        Warning,
        Error       
    }

    public partial class SplashForm : Form
    {
        static SplashForm _splashForm = null;
        static Thread _splashThread = null;
        public static object locker = new object(); 
        public  static bool WaitPlease = true;  

        private  SplashForm()
        {
            InitializeComponent();
            lblLoading.Text = "Please wait Loading";
        }

        public static  void ShowSplashScreen()
        {            
            if (_splashForm != null)
                return;
            _splashThread = new Thread(new ThreadStart(SplashForm.ShowSplash));
            _splashThread.IsBackground = true; 
            _splashThread.SetApartmentState(ApartmentState.STA) ; 
            _splashThread.Start();
        }

        public static void ShowSplash()
        {
            if (_splashForm==null)
            {                
                _splashForm = new SplashForm();
                _splashForm.blueLoaderBar1.StartAnimation();

            }
              _splashForm.TopMost = true;
              _splashForm.Show();
              lock (SplashForm.locker)
              {
                  WaitPlease = false;
              }

            Application.Run(_splashForm);

        }

        delegate void CloseSplashHandler(SplashTypeOfMessage typeOfMessage, string message,bool itWasRinvoked);

        public static void CloseSplash(SplashTypeOfMessage typeOfMessage,string message,bool itWasrinvoked)
        {                             
            CloseSplashHandler closeSpalshHandler = new CloseSplashHandler(CloseSplash);
            bool launched=false;
            while (!launched && !itWasrinvoked)
            {
                lock (SplashForm.locker)
                {
                    if (!SplashForm.WaitPlease)
                    {
                        launched = true;
                    }
                }
             }

            if (_splashForm!=null && _splashThread!=null )
            {
                if (_splashForm.InvokeRequired)
                {
                    _splashForm.Invoke(closeSpalshHandler,new object[] {typeOfMessage,message,true});
                }
                else
                {                    
                    switch (typeOfMessage)
                    {
                        case SplashTypeOfMessage.Warning:
                            break;
                        case SplashTypeOfMessage.Error:
                            MessageBox.Show("Error");                                                  
                            break;
                        default:
                            break;
                    }
                    _splashForm.Close();
                    _splashThread = null;

                }                                
            }
        }             
    }
}

你可以这样称呼它:

SplashForm.ShowSplashScreen();

这是关闭启动画面的方法:

SplashForm.CloseSplash(typeOfMessage ,string.Empty,false);

【讨论】:

  • 我已经实现了你的代码,我想我需要在开始时添加一些 using System.Something 语句,目前我有默认的: using System;使用 System.Collections.Generic;使用 System.ComponentModel;使用 System.Data;使用 System.Drawing;使用 System.Linq;使用 System.Text;使用 System.Windows.Forms;编辑:我删除了一些不需要的 sn-ps,但我的 IDE 仍然无法识别“线程”。
  • 咳嗽得到这个:jetbrains.com/resharper
  • @Pyroglyph 你需要使用 System.Threading;
  • 好吧,我使用常识并尝试使用 System.Threading,它似乎工作,虽然我不知道把 SplashForm.ShowSplashScreen();如果我把它放在 SplashForm.cs 中,它不会显示,如果我把它放在我的主窗体中,它坚持认为 SplashForm 在当前上下文中不存在,而 SplashForm 中的几乎所有内容都是公开的!
  • 在你的主要方法 Application.EnableVisualStyles();布尔错误 = 假; Application.SetCompatibleTextRenderingDefault(false); SplashForm.ShowSplashScreen();
猜你喜欢
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
相关资源
最近更新 更多