What's new

[C#] Create a functional splash screen

  • Thread starter Calamity
  • Start date
  • Views 6,825
Calamity

Calamity

Retired
Retired
Messages
6,704
Reaction score
1,159
Points
650
Sin$
0
Let me start by saying, I'm not the first person to post a splash screen tutorial, if you want to give him credit and thank him for it, be my guest, I'm not stopping you and I thank him as well.
Zer0's Splash screen tutorial

However, this tutorial is a more advance splash screen tutorial, allowing you to open the main form afterwards and close the splash screen form, and also allowing you to load anything you need for your program.

1. Create a new windows form project in C# (or VB)

se1.jpg

2. Open up Program.cs

3. Add the following class to the Program.cs file:
Code:
public class MyContext : ApplicationContext
{
public MyContext()
{
Application.Idle += new EventHandler(Application_Idle);

Form1 splash = new Form1();
splash.Show();
}

void Application_Idle(object sender, EventArgs e)
{
if (Application.OpenForms.Count == 0)
Application.Exit();
}
}
se3.jpg

4. Change the line 'Application.Run(new Form1());' to Application.Run(new MyContext());' to 'Application.Run(new

Code:
MyContext());

5. Go to the Form1.cs (or Splash loading form).

6. Set the FormBorderStyle properties to "None" and StartPosition to "CenterScreen".

se2.jpg

7. Add a background image on the form. (Properties > BackgroundImage > Import > Find your image)
- Along with Zer0, I also recommend setting it to Stretch, then size the form to your wants.

8. Set the forms opacity to 0%.

se8.jpg

9. Optional: Add a loading label, I'm creating the splash screen I used in Dark Bomber.

se9.jpg

10. Optional: Add a timer for the loading label. We'll set the interval to 500 and name it tmrLoading.

se10.jpg

11. Optional: Double click the tmrLoading and add the following code to the timer's tick event (Change to your likings):
Code:
if (label1.Text.ToLower() == "loading dark bomber")
label1.Text = "Loading Dark Bomber.";
else if (label1.Text.ToLower() == "loading dark bomber.")
label1.Text = "Loading Dark Bomber..";
else if (label1.Text.ToLower() == "loading dark bomber..")
label1.Text = "Loading Dark Bomber...";
else if (label1.Text.ToLower() == "loading dark bomber...")
label1.Text = "Loading Dark Bomber";

12. Optional: Double click the form and add the following line to the form's load event:

Code:
tmrLoading.Start();

se12.jpg

13. Add another timer to the form, we'll name this one tmrLoad and set the interval to 20.

se13.jpg

14. Double click the timer.

15. Add a private bool to the class variables, call it '_loading', then add the following code to your tmrLoad's tick event:

Code:
 if (_loading)
this.Opacity += .02;
else
{
if (this.Opacity == 0)
this.Close();
else
this.Opacity -= .02;
}

16. After that, add the tmrLoad.Start() call to the form's load event as well.

se16.jpg

17. Now this part is to allow you to load things, which is one of the original purposes of a splash screen. Add a
backgroundWorker to the form, and name it bgwLoad

se17.jpg

18. Double click the background worker, and in the bgwLoad's DoWork event, load any resources or anything online that
you need.

19. Switch back to the designer and go into the background workers' properties. Click the icon that looks like a Lightning
bolt (or somewhat looks like one) which is events, and double click "RunWorkerCompleted".

20. Add the following code to allow the splash to fade away to the RunWorkerCompleted event.

Code:
_loading = false;

21. Add the RunWorkerAsync method call to the form's load event:
Code:
bgwLoad.RunWorkerAsync();

se21.jpg

22. Add a new form to the project (your second form).

se22.jpg

23. Navigate back to the tmrLoad_Tick event, and edit the code to where the splash will close to the following:

Code:
        	if (_loading)
this.Opacity += .02;
else
{
if (this.Opacity == 0)
{
Form2 mainForm = new Form2();
mainForm.Show();
this.Close();
}
else
this.Opacity -= .02;
}

24. You're done! Feel free to edit the speed of the splash screen to your likings, and edit anything you want.

Let me know if I missed something, didn't explain something properly, or need additional help.

Source code download:

Code:
https://rapidshare.com/files/2306212782/SplashEx.rar
 
Top Bottom
Login
Register