如何在Windows窗体应用程序中构建启动画面?

76

我需要在应用程序启动时显示闪屏界面几秒钟。有人知道如何实现吗?

非常感谢您的帮助。


14
+1是因为迄今为止没有一个真正好的“溅泼效果”的例子。我从不同的博客和网站上找到了一些混乱的实现方式,但在经历了很多麻烦之后,终于出现了一个好的实现方法。这个问题确实很好。 - Anderson Matos
有另一种选项,不需要使用计时器或在主窗体和启动窗体之间保持引用关系:https://dev59.com/vFzUa4cB1Zd3GeqP6caj - Veldmuis
可能是启动画面示例的重复问题。 - Jim G.
以下示例来自Telerik,使用了ShapedForm控件,但是将其更改为普通的Windows表单。这是我见过的最简单和最好的方法。http://www.telerik.com/support/kb/winforms/forms-and-dialogs/details/add-splashscreen-to-your-application - driverobject
尝试这个解决方案 https://dev59.com/bWUo5IYBdhLWcg3wrhPj#15836105 - 4lex
显示剩余2条评论
13个回答

0

这是创建启动画面的最简单方法:

首先,在Form1.cs代码中的命名空间之前添加以下代码行:

using System.Threading;

现在,请按照以下步骤操作:

  1. 在你的应用程序中添加一个新表单

  2. 将这个新表单命名为FormSplashScreen

  3. 在BackgroundImage属性中,从你的文件夹中选择一张图片

  4. 添加一个progressBar

  5. 在Dock属性中,将其设置为Bottom

  6. 在MarksAnimationSpeed属性中,将其设置为50

  7. 在你的主表单中,默认情况下命名为Form1.cs,创建以下方法:

     private void StartSplashScreen()
     {
         Application.Run(new Forms.FormSplashScreen());
     }
    
  8. 在Form1.cs的构造方法中,添加以下代码:

     public Form1()
     {
         Thread t = new Thread(new ThreadStart(StartSplashScreen));
         t.Start();
         Thread.Sleep(5000);
    
         InitializeComponent();//此代码由Visual Studio自动生成
    
         t.Abort();
     }
    
  9. 现在,只需运行应用程序,它就会完美地工作。


-2

试试这段代码

public partial class ssplashscreen : Form
    {
        public ssplashscreen()
        {                
            InitializeComponent();    
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.Increment(1);
            if (progressBar1.Value == 100)
            {
                timer1.Stop();
                this.Hide();
                Form frm = new login();
                frm.Show();
            }
        }
    }

4
这个回答需要一些解释。 - Raidri

-4

试试这个:

namespace SplashScreen
{
    public partial class frmSplashScreen : Form
    {
        public frmSplashScreen()
        {
            InitializeComponent();
        }

        public int LeftTime { get; set; }

        private void frmSplashScreen_Load(object sender, EventArgs e)
        {
            LeftTime = 20;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (LeftTime > 0)
            {
                LeftTime--;
            }
            else
            {
                timer1.Stop();
                new frmHomeScreen().Show();
                this.Hide();
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接