Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#

.NET Splash Screen Component

Rate me:
Please Sign up or sign in to vote.
3.31/5 (21 votes)
23 Oct 2008CC (ASA 2.5)3 min read 111.6K   6K   141  
A splash screen which allows for dynamic creation and updates.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using DGDev;

namespace Demo
{
	static class Program
	{
		private static SplashScreen splash;

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			/*****************************************
			 * Setup and display splash screen
			 *************************************** */
			const String resourceName = "Splash.png"; // Name of EmbeddedResource Image
			var InfoLabel = new Point(25, 92); // Creates the point to position the ProgramInfoLabel
			var StatusLabel = new Point(28, 261); // Creates the point to position the StatusLabel

			splash = SplashScreen.Current; // 
			splash.SetTransparentKey = Color.Fuchsia;
			var rs = GetResourceStream(resourceName);
			splash.SetBackgroundImage = new Bitmap(rs);

			//splash.SetFade = true;  // This fades the splash screen in and out. However, when using TransparencyKey, doesn't not display correctly.
			
			splash.SetInfoLabel(InfoLabel, 499, 117);
			splash.SetStatusLabel(StatusLabel, 490, 17);
			splash.ShowSplashScreen();
			
			splash.SetInfo = "This program is just an example.";

			// Create and AssemblyHandler
			AppDomain currentDomain = AppDomain.CurrentDomain;
			currentDomain.AssemblyLoad += asmLoadHandler;

			splash.SetStatus = "Reading Configuration ...";
			LoadConfigData();

			Application.Run(new Form1());
		}

		private static System.IO.Stream GetResourceStream(String resource)
		{
			var ea = System.Reflection.Assembly.GetExecutingAssembly();
			foreach (String curResource in ea.GetManifestResourceNames())
			{
				if (curResource.EndsWith(resource))
				{
					return ea.GetManifestResourceStream(curResource);
				}
			}
			return null;
		}

		static void asmLoadHandler(object sender, AssemblyLoadEventArgs args)
		{
			// This probably won't display anything for the demo because there is hardly anything to load.
			splash.SetStatus = "Loading Assembly: " + args.LoadedAssembly.GetName().Name + " ...";
		}

		private static void LoadConfigData()
		{
			// Simulate loading...
			// This would not normally be done here, but rather in the Form_Load, etc etc
			splash.SetStatus = "Loading module 1";
			Thread.Sleep(500);
			splash.SetStatus = "Loading module 2";
			Thread.Sleep(300);
			splash.SetStatus = "Loading module 3";
			Thread.Sleep(900);
			splash.SetStatus = "Loading module 4";
			Thread.Sleep(100);
			splash.SetStatus = "Loading module 5";
			Thread.Sleep(500);
			splash.SetStatus = "Loading module 6";
			Thread.Sleep(600);
			splash.SetStatus = "Loading module 7";
			Thread.Sleep(200);
			splash.SetStatus = "Loading module 8";
			Thread.Sleep(800);
			splash.SetStatus = "Loading module 9";
			Thread.Sleep(400);
			splash.CloseSplashScreen();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions