"The Matrix" Screen Saver

Several years ago I'd written an article about screen savers in .NET. The very detailed article has since gone to PDF form (found here), but I thought it would be good to mention some of the finer points in case anyone else out there found this and was interested in writing a C#-based screen saver. The samples you'll generally find available aren't nearly good enough as they don't provide enough code, or the right code, to do the job.

In this case, I have a screen saver that mimics the screens seen in one  of my favorite movies, "The Matrix." Here's the general idea:

Matrix Screen Image

Aside from the actual simulation, which I'll leave to you to decipher from the code, two things are critical here. First, you have to properly accept (and handle) the command line parameters. And second, you have to deal with multiple screens. Here's some detail.

As for the command line, this is the code you'll need:

string cmd = args.Length > 0 ?
    (args[0].Length > 1 ?
     args[0].ToLower(CultureInfo.InvariantCulture).Trim().Substring(0, 2) :
    args[0].ToLower(CultureInfo.InvariantCulture).Trim().Substring(0, 1)) : "";

if (cmd == "/c" || cmd == "-c"  || cmd == "c" || cmd == "" )
    // Options dialog.
} // if
else if (cmd == "/p" || cmd == "-p" || cmd == "p" )
    // "Preview" mode.
} // else if
else if (cmd == "/s" || cmd == "-s" || cmd == "s" )
    // Run screen saver.
} // else if
    // Sorry...don't recognize the command line parameter.
} // else

The trickiest part is the "/c" option, which requires you to accept a window handle (as a string) and render the screen output there. (That's how the options dialog shows you the preview.) The details are in the code, which you can download here, but essentially you convert the handle from a string to an IntPtr, and then create a Graphics object from that. The option will come in this form:


Multiple screens are another tricky aspect. The basic approach is you iterate the screens attached to the system, which you find in Screen.Allscreens, like so:

Int32 totWidth = 0;
Int32 totHeight = 0;
for (Int32 i = 0; i < Screen.AllScreens.Length; i++)
   // Check the dimensions of this screen
   if ( Screen.AllScreens[i].Bounds.X == 0 )
       // We simply compare the width of this screen to
       // the total width we've accumulated, and if greater,
       // use this screen's width. Otherwise, we're covered.
       if (Screen.AllScreens[i].Bounds.Width > totWidth )
           totWidth = Screen.AllScreens[i].Bounds.Width;
   } // if
       // The widths are additive...
       totWidth += Screen.AllScreens[i].Bounds.Width;
   } // else

   // Height
   if (Screen.AllScreens[i].Bounds.Y == 0)
       // We simply compare the height of this screen, similar
       // to what we did for width.
       if (Screen.AllScreens[i].Bounds.Height > totHeight)
           totHeight = Screen.AllScreens[i].Bounds.Height;
   } // if
       // The heights are additive...
       totHeight += Screen.AllScreens[i].Bounds.Height;
   } // else
} // for

To install the screen saver, simply copy the Matrix.scr file to your hard drive and right-click. Then select Install from the list of available options (you can alternatively test it or configure it as well). The compiled image in the download is for all CPUs, but feel free to recompile it to match your own system. Be sure to compile a release build, however, so the streams aren't too slow!

Tags: , , , ,
Categories: .NET General

Actions: E-mail | Permalink | Comment RSSRSS comment feed


April 19. 2010 05:35

Rick Argenal

Couldnt be written any better about c# . Reading this column strikes a chord in my memory of my old room mate! He always kept talking about this. I will forward this article to him. Pretty certain he will have a fantastic read. Gratitude 4 sharing!

Rick Argenal

April 20. 2010 18:46

Reinaldo Pavek

Couldnt be written any better about c# . Reading this column jogs my memory of my old room mate! He always kept talking about this. I will forward this informative article to him. Pretty sure he will have a great read. Gratitude 4 sharing!

Reinaldo Pavek

April 20. 2010 20:30

Williams Loscalzo


Williams Loscalzo

June 27. 2012 02:40


Pingback from fittravelsynonym.radioonthenet.mobi

Helping Victims of Torture (Voice Of America) | Fit Travel Synonym


August 22. 2012 05:50


Pingback from howtodownloadmovie.com

House, M.D.: Season Eight | How to Download Movie


December 20. 2016 10:18


Pingback from steroidsforsale.biz

anabolic steroids for sale online


Add comment

  Country flag

Click to change captcha
  • Comment
  • Preview