Click here to Skip to main content
15,888,113 members
Articles / Multimedia / GDI+

A complete C# Screensaver that does double-buffering on multiple monitor systems!

Rate me:
Please Sign up or sign in to vote.
4.75/5 (37 votes)
24 Jun 200534 min read 272.9K   1.8K   190  
Example screensaver in source code. Does a mini-preview too!
  • swarmscreensaver_src.zip
    • SwarmScreenSaver
      • CodeCommentReport
        • banner.htm
        • banner.jpg
        • CodeCommentReportError.htm
        • commentreport.css
        • CommonCode
        • darkcorner.jpg
        • gradleft.jpg
        • gradtop.jpg
        • graycorner.jpg
        • Insects
        • minus.jpg
        • plus.jpg
        • Solution_SwarmScreenSaver.HTM
        • SwarmScreenSaver
          • CWP0.HTM
          • CWP10.HTM
          • CWP100.HTM
          • CWP101.HTM
          • CWP102.HTM
          • CWP103.HTM
          • CWP104.HTM
          • CWP105.HTM
          • CWP106.HTM
          • CWP107.HTM
          • CWP108.HTM
          • CWP109.HTM
          • CWP11.HTM
          • CWP110.HTM
          • CWP111.HTM
          • CWP112.HTM
          • CWP113.HTM
          • CWP114.HTM
          • CWP115.HTM
          • CWP116.HTM
          • CWP117.HTM
          • CWP118.HTM
          • CWP119.HTM
          • CWP12.HTM
          • CWP120.HTM
          • CWP121.HTM
          • CWP122.HTM
          • CWP123.HTM
          • CWP124.HTM
          • CWP125.HTM
          • CWP126.HTM
          • CWP127.HTM
          • CWP128.HTM
          • CWP129.HTM
          • CWP13.HTM
          • CWP130.HTM
          • CWP131.HTM
          • CWP132.HTM
          • CWP134.HTM
          • CWP135.HTM
          • CWP136.HTM
          • CWP137.HTM
          • CWP138.HTM
          • CWP139.HTM
          • CWP14.HTM
          • CWP140.HTM
          • CWP141.HTM
          • CWP142.HTM
          • CWP143.HTM
          • CWP144.HTM
          • CWP145.HTM
          • CWP146.HTM
          • CWP147.HTM
          • CWP148.HTM
          • CWP149.HTM
          • CWP15.HTM
          • CWP150.HTM
          • CWP151.HTM
          • CWP152.HTM
          • CWP153.HTM
          • CWP154.HTM
          • CWP155.HTM
          • CWP156.HTM
          • CWP157.HTM
          • CWP158.HTM
          • CWP159.HTM
          • CWP16.HTM
          • CWP160.HTM
          • CWP161.HTM
          • CWP162.HTM
          • CWP163.HTM
          • CWP164.HTM
          • CWP165.HTM
          • CWP166.HTM
          • CWP167.HTM
          • CWP168.HTM
          • CWP169.HTM
          • CWP17.HTM
          • CWP170.HTM
          • CWP171.HTM
          • CWP172.HTM
          • CWP173.HTM
          • CWP174.HTM
          • CWP175.HTM
          • CWP176.HTM
          • CWP177.HTM
          • CWP178.HTM
          • CWP179.HTM
          • CWP18.HTM
          • CWP180.HTM
          • CWP181.HTM
          • CWP182.HTM
          • CWP183.HTM
          • CWP184.HTM
          • CWP185.HTM
          • CWP186.HTM
          • CWP187.HTM
          • CWP188.HTM
          • CWP189.HTM
          • CWP19.HTM
          • CWP190.HTM
          • CWP191.HTM
          • CWP192.HTM
          • CWP2.HTM
          • CWP20.HTM
          • CWP21.HTM
          • CWP22.HTM
          • CWP23.HTM
          • CWP24.HTM
          • CWP25.HTM
          • CWP26.HTM
          • CWP27.HTM
          • CWP28.HTM
          • CWP29.HTM
          • CWP3.HTM
          • CWP31.HTM
          • CWP32.HTM
          • CWP33.HTM
          • CWP34.HTM
          • CWP35.HTM
          • CWP36.HTM
          • CWP38.HTM
          • CWP39.HTM
          • CWP4.HTM
          • CWP40.HTM
          • CWP41.HTM
          • CWP42.HTM
          • CWP43.HTM
          • CWP44.HTM
          • CWP45.HTM
          • CWP46.HTM
          • CWP47.HTM
          • CWP48.HTM
          • CWP49.HTM
          • CWP5.HTM
          • CWP50.HTM
          • CWP51.HTM
          • CWP52.HTM
          • CWP53.HTM
          • CWP54.HTM
          • CWP55.HTM
          • CWP56.HTM
          • CWP57.HTM
          • CWP58.HTM
          • CWP59.HTM
          • CWP6.HTM
          • CWP60.HTM
          • CWP61.HTM
          • CWP62.HTM
          • CWP63.HTM
          • CWP64.HTM
          • CWP65.HTM
          • CWP66.HTM
          • CWP67.HTM
          • CWP68.HTM
          • CWP69.HTM
          • CWP7.HTM
          • CWP70.HTM
          • CWP71.HTM
          • CWP72.HTM
          • CWP73.HTM
          • CWP74.HTM
          • CWP75.HTM
          • CWP76.HTM
          • CWP77.HTM
          • CWP78.HTM
          • CWP79.HTM
          • CWP8.HTM
          • CWP80.HTM
          • CWP81.HTM
          • CWP82.HTM
          • CWP83.HTM
          • CWP84.HTM
          • CWP85.HTM
          • CWP86.HTM
          • CWP88.HTM
          • CWP9.HTM
          • CWP90.HTM
          • CWP91.HTM
          • CWP92.HTM
          • CWP93.HTM
          • CWP94.HTM
          • CWP95.HTM
          • CWP96.HTM
          • CWP97.HTM
          • CWP98.HTM
          • CWP99.HTM
          • SwarmScreenSaver.HTM
        • titletile.jpg
        • vt.js
      • CommonCode
      • Insects
      • SwarmScreenSaver.sln
      • SwarmScreenSaver
using System;
using System.Windows.Forms; // This is so we can put up a messagebox.
using CommonCode.XmlConfig;
// using System.Diagnostics;
//using System.Reflection;

namespace SwarmScreenSaver
{

	

/// <summary>
/// Entrypoint.cs is a class file that is the main entry point for the entire program.<br></br>
/// It handles Configuration requests, creates and runs instances of Mini-preview and ScreenSaver<br></br>
/// which does the actual Screensaving. To install this program, run setup to a folder <br></br>
/// of your making and then rename Swarm.exe to swarm.scr and move it to your System32<br></br>
/// subdirectory and then right-click on the desktop and choose properties and<br></br>
/// select swarm and hit OK. Enjoy your new Screensaver. If you have source code included<br></br>
/// you should see a zip file or folder called CodeCommentReport. Inside is a web page version <br></br>
/// of the documentation for the source code and info on use of the source as a template for<br></br>
/// your own screensaver projects. Written in Visual C# from Visual Studio 2003 Universal.
/// </summary>

	public class EntryPoint 
	{

//		public static void OnExcept(Object sender, UnhandledExceptionEventArgs args) 
//		{
//			Application.Exit();
//		}

		private static void ParseArgsToPrefixAndArgInt( string[] args, out string argPrefix, out int argHandle)
		{
			string curArg;
			char[] SpacesOrColons =  {' ', ':'};

			switch(args.Length)
			{
				case 0: // Nothing on command line, so just start the screensaver.
					argPrefix = "/s";
					argHandle = 0;
					break;
				case 1:
					curArg = args[0];
					argPrefix = curArg.Substring(0,2);
					curArg = curArg.Replace(argPrefix,""); // Drop the slash /? part.
					curArg = curArg.Trim(SpacesOrColons); // Remove colons and spaces.
					argHandle = curArg == "" ? 0 : int.Parse(curArg); // if empty return zero. else get handle.
					break;
				case 2:
					argPrefix = args[0].Substring(0,2);
					argHandle = int.Parse(args[1].ToString());
					break;
				default:
					argHandle = 0;
					argPrefix = "";
					break;

			}
		}

	/// <summary>
	/// Main is the entry point.
	/// </summary>
	/// <param name="args">
	/// <description>
	/// A string array that contains the arguments passed in on the command line. For more info see the source code.
	/// </description>
	/// </param>
		//[STAThread] 
		static void Main(string[] args) 
		{
			//AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnExcept);
			// These are just some variables to old config and argument data.
			int baseVelocity;
			int beeCount;
			int colorCycleSeconds;
		    bool glitterOn;
			int swarmsPerScreen;
			bool useStyleDoubleBuffer;
			string argPrefix;
			int argHandle; // Will be 0 if no number found.
			XmlConfigSaver xm = new CommonCode.XmlConfig.XmlConfigSaver();
			// read xml config ini file, if missing, create and return default values.
			xm.readConfigXml(out baseVelocity, out beeCount, out colorCycleSeconds, out glitterOn, out swarmsPerScreen, out useStyleDoubleBuffer);
			xm = null; // We don't need to get the xml config class for now.
			if (args.Length > 2) 
			{
				MessageBox.Show("Too many arguments on the command line.");
				return;
			}
			ParseArgsToPrefixAndArgInt(args, out argPrefix, out argHandle);

			try
			{
				switch (argPrefix)
				{
					case "/a": // password dialog desired. I'll exit program instead of risking locking someone out due to a bug
						break; // in my code, or a change in the OS code.

					case "/c": // Show configuration screen.
						System.Windows.Forms.Application.Run(new ConfigForm(argHandle));						
						break; // Break goes to a point where we exit the program.
						// Start the screensaver in mini-preview or full screen mode depending on the number of args passsed in.

					case "/p": 
						if (argHandle == 0) goto case "/s"; // No handle found, do a fullscreen screensaver.
						else
						{
							MiniPreview mpTemp = new MiniPreview();
							mpTemp.DoMiniPreview(argHandle, baseVelocity, beeCount, colorCycleSeconds, glitterOn, useStyleDoubleBuffer);
							mpTemp = null; // This does not execute until preview is closed.
						}
						break; // Exit out of the main loop and the program closes.

					case "/s": // Start screensaver.
						ScreenSaver screenSaver = new ScreenSaver();
						screenSaver.RunMeTillShutdown( baseVelocity, beeCount, colorCycleSeconds, glitterOn, swarmsPerScreen, useStyleDoubleBuffer);
						screenSaver = null; // This doesn't execute until shutdown is done.	
						break;
					default:   // If something strange is passed in, just ignore and exit.
						break;
				} // Switch end.
			} // End of try statement.
			
			catch
			{	
				// Just exit because this is a screensaver for unattended operation. We don't want a message
				// and we want to exit if anything goes wrong.
			}
		} // Main End.

	} // Class End.
} // Namespace end.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Hello, I have been a professional hairpuller aka programmer for a large company during the last nine years. The acquiring company is closing down the california office so I'll be out of work at the end of June at the latest. I program in a variety of laguages, C#, C++, C, Visual Basic, SQl Server among others. I live north of Sacramento in Paradise CA. I've been programming since the early 1980 and people accuse me of going bald. I'm not going bald, my hair is just migrating south and into my ears.

I am a firm believer that the most widely used programming language is #$@$@$%#$%^ but folks tend to object to the unusual synatax where each word has a colorful meaning and sentences are limited to three words max, while the punching bag flys back and forth so fast it heats up from friction like a meteorite through the air.

In short, I'm nuts, but I'm good at what I do.

Programming is like beating my head against a brick wall, it hurts so bad I can't stand it, but when I stop beating my head against the wall (Solve the problem), it feels so good I gotta do it again!

Comments and Discussions