Useful screensavers
Written by Harry Fairhead   
Thursday, 17 September 2009
Article Index
Useful screensavers
Display
Bouncing text
Look and behavior

The screensaver is no longer needed as a way of protecting displays from damage due to “burn in” but it is a very useful way of automatically running a program. If the user hasn’t pressed a key or moved the mouse for a set time then the configured screensaver kicks in. This provides us with a way of running a program when the machine is idle and a way of presenting the user with some information on their return.

Notice that it is usual for a screensaver to unload itself when the user presses a key or moves the mouse – but this isn’t mandatory. What the screensaver does after it has been loaded is entirely up to you and when you realise that a screensaver will run even before you have logged on to the machine and you can see that the scope for clever stuff is even greater.

So perhaps screensavers aren’t a complete waste of time – in fact they might well be just the opposite, i.e. a way of using otherwise waste computing time…

Saving screens

So how do you create a screensaver application?

It turns out to be surprisingly easy but if you look it up in the documentation you might not think so. There have been various attempts over the years to create a screensaver “framework”, which have generally only made something very simple seem much more difficult.

Getting started

It’s always helpful examine the simplest example of a project type so that you can really see how things work. Although screensavers seem complicated they are just standard .EXE files with their file names changed to end in .SCR. They also live in either the Windows/System or Windows/System32 directory.


If you select a screensaver then when the machine has been idle for the specified amount of time it is loaded and run as a standard EXE file would be.

Well there is a little more to it than this but this is a good starting point for a program. The only additional thing we need to take into account when writing a first screensaver is that the screensaver loader passes the screensaver a command line parameter to tell it what to do.

 

Parameter

Meaning

/s

Start in screensaver mode

/c

Show configuration dialog box with whatever window is currently active as the parent

/p

Show a preview of your screensaver

(None)

Show the configuration dialog box with no parent window.


The good news is that you really only need to support the /s option to create the simplest possible screensaver.

Start a new Console project called ScreenSaver1. This provides you with the initial code you need, i.e. a main function that is run when the application starts. The main function receives the command line parameters as a single string, which is called “args” for "arguments" by default:

static void Main(string[] args)
{

If there are any command line parameters the args string will have a length greater than zero:

if (args.Length > 0)
{

Given the simple nature of the possible parameters we can convert them into a standard form of two characters in lower case:

 string arg = args[0].ToLower().
Trim().Substring(0, 2);

The result can now be used to decide what to do. You could use nested if statements but a switch is so much neater and easier to follow:

  switch (arg)
{
  case "/c":
//configure
  break;
case "/s":
ShowScreenSaver();
break;
case "/p":
//preview
break;
}
}
else
{
ShowScreenSaver();
}
}

The final else clause belongs to the initial if statement and is executed if there are no command line parameters when we simply show the screensaver.

At some point in the future we are going to have to supply functions to deal with all three possibilities but for the moment all we need is the ShowScreenSaver function. In principle this could do anything you want it to but the normal action of a screensaver is to load graphics to fill the screen.

The simplest way of doing this in many languages is to load a form and customise it to fill the screen. For this simple example we just load a form and leave the customisation to later. Add a form called screen to the project, there is no need to do anything to it and add, just after the main function just defined:

static void ShowScreenSaver()
{
Application.Run(new screen());
}

This uses the Application object to run, on a new thread, a new instance of the form. This not only loads the form but start its “message pump” running which makes it responsive. After this the thread running the main function can terminate and the screen form will remain active until the user closes it. Finally to make this work we need to add:

using System.Windows.Forms;

to the start of the file continuing the main function.

<ASIN:1590598849>

<ASIN:0321485890>

<ASIN:1933988363>



Last Updated ( Friday, 25 September 2009 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.