brisonela
brisonela

Reputation: 125

Welcome loading on c#

I´m making a program using C# and It´s almost over and I was thinking that It will be interesting to include a welcome form to load by 5 seconds showing the name of the program and other stuff... like this in microsoft word...

enter image description here

but I'm not sure how to do this. I will like some advice please....

Upvotes: 0

Views: 972

Answers (3)

MoonKnight
MoonKnight

Reputation: 23831

I do this using 'System.Theading' and this works very well for me. The following code launches a "splash screen" on a separate thread whilst your application (in my example below it is called MainForm()) loads or initialises. Firstly in your "main()" method (in your program.cs file unless you have renamed it) you should show your splash screen. This will be a WinForm or WPF form that you wish to show the user at start up. This is launch from main() as follows:

    [STAThread]
    static void Main()
    {
        // Splash screen, which is terminated in MainForm.       
        SplashForm.ShowSplash();

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        // Run UserCost.
     Application.Run(new MainForm());
    }

In your SplashScreen code you need something like the following:

public partial class SplashForm : Form
{

    // Thredding.
    private static Thread _splashThread;
    private static SplashForm _splashForm;    

    public SplashForm()
    {
        InitializeComponent();
    }

    // Show the Splash Screen (Loading...)      
    public static void ShowSplash()    
    {        
        if (_splashThread == null)        
        {            
            // show the form in a new thread            
            _splashThread = new Thread(new ThreadStart(DoShowSplash));            
            _splashThread.IsBackground = true;            
            _splashThread.Start();        
        }    
    }    

    // Called by the thread    
    private static void DoShowSplash()    
    {        
        if (_splashForm == null)            
            _splashForm = new SplashForm();        
        // create a new message pump on this thread (started from ShowSplash)        
        Application.Run(_splashForm);
    }    

    // Close the splash (Loading...) screen    
    public static void CloseSplash()    
    {        
        // Need to call on the thread that launched this splash        
        if (_splashForm.InvokeRequired)            
            _splashForm.Invoke(new MethodInvoker(CloseSplash));        
        else            
            Application.ExitThread();    
    }

}

This launches the splash form on a separate background thread allowing you to proceed with the rendering of your main application simultaneously. To finish off and close the splash screen down when your application has been initialised you place the following inside the default constructor (you could overload the constructor if you wanted to):

    public MainForm()
    {
        // ready to go, now initialise main and close the splashForm. 
        InitializeComponent();
        SplashForm.CloseSplash();
    }

This is all self-explanatory and you should be able to establish the exact working of the code on your own. I hope it helps you.

Upvotes: 1

Denis Biondic
Denis Biondic

Reputation: 8201

Well, you create a form, set its borders to none, put an image, an winforms timer set to fire after 5 sec in which you open main form, and of you go.

However, more complex splash screens (WinForms) require GDI+, clipping etc, but this will do I guess...

Upvotes: 3

LBushkin
LBushkin

Reputation: 131796

If you were using WPF you could use the System.Windows.SplashScreen object.

In WinForms, it's a bit harder, but you could use the example located here to get you started.

Keep in mind, splash screen are meant for cases when the application takes a while to load to help the user feel like something is happening. If you're just going to delay the application load by 5 seconds, you're actually degrading the experience for your users. Five seconds is a very long time ...

Upvotes: 4

Related Questions