简体   繁体   中英

Splash screen c#

I'm not so advanced in c# and I make small projects, but now I have a problem. I make a splash screen. Everything work. I make a project with a menu, and in menu you can choose different variants: Encryption, Decrpytion and Exit. In each, I have a "home" button. When I press button, in every menu, every time appear that splash screen and I need to wait. It's annoying. How can I set to work just 1 time (just when I start the program)?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Soft.Properties;
using System.Threading;

namespace Soft
{
    public partial class Meniu : Form
    {
    public Meniu()
    {
        Thread t = new Thread(new ThreadStart(SplashStart));
        t.Start();
        Thread.Sleep(5000);

        InitializeComponent();

        t.Abort();

    }

    public void SplashStart()
    {
        Application.Run(new LOGO());
    }

And one more question. After the Logo appear, the program minimize. Any advice?

If you want show splash screen only on program starting open your Program.cs and add a counter inside static class Program section for count form runs.

public static int counter=0;

And edit your code like this:

public Meniu()
        {
            InitializeComponent();
            Program.counter++;
            if (Program.counter == 1) // If first run minimize and show splash screen
            {
                this.WindowState = FormWindowState.Minimized;
                Thread t = new Thread(new ThreadStart(SplashStart));
                t.Start();
                Thread.Sleep(5000);
                t.Abort();
            }
            else // If not first run
            {
                this.WindowState = FormWindowState.Normal;
            }
        }

        public void SplashStart()
        {
           Application.Run(new LOGO());
        }

您可以在首次启动时设置运行时标志,并为下次启动设置时间戳,以表明您的应用程序已在运行,无需显示启动屏幕,只需在不同功能之间切换即可。

You can also use WindowsFormsApplicationBase from Microsoft.VisualBasic.ApplicationServices namespace. It's available in Winforms projects. This base class provide easy way to add splash screen into your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM