Saturday, July 10, 2010

Create A Screensaver

Simply put, a screensaver is just an executable .exe file with ending .scr and some special properties.
This fact we use when creating a screensaver in C#.
The screensaver program expects a parameter on startup, whether the screesaver is to be started ("/s") or configured ("/c").
As it is harder to start Windows forms - applications with parameters, we therefore create a console application.
To this we then add a new element, a form (Windows Form).
In the main function of the console application we then prompt the given parameter, if this is "/s" we show the added form (in my project this is called ScreenSaverForm):
System.Windows.Forms.Application.Run(new ScreensaverForm());

So that the form runs in fullscreen mode as we are used of screensaves, we set the following 3 properties in the function Load() of the form (see the previous post for more details):
        private void ScreensaverForm_Load(object sender, EventArgs e)
        {
            FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Bounds = Screen.PrimaryScreen.Bounds;
            Cursor.Hide(); // hide cursor
            TopMost = true;

        }
When testing the program in this development stage, the console application as well as the taskbar are still visible.
This problem can be fixed, when setting in the project properties (right click on the project in the project explorer) under "Application" the output type as "Windows - Application".
Now we just have to define, when the screensaver should end, of course this is the case when the user starts any input.
Therefore in the functions ScreensaverForm_KeyDown()ScreensaverForm_MouseDown() and private void ScreensaverForm_MouseMove() we execute corresponding actions.
Now the complete source code, first of the console application StartupProgramm.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ScreenSaver
{
    class StartupProgramm
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                // configuration
                if ((args[0].ToLower().Trim().Substring(0, 2) == "/c")) // lconfig - parameter is given in the form "/c:xxxxx"
                {
                    // if wished, create here a form for configuration purposes
                    System.Windows.Forms.MessageBox.Show("No settings available.");
                }
                // Start
                else if (args[0].ToLower() == "/s")
                {
                    // load screensaver form
                    System.Windows.Forms.Application.Run(new ScreensaverForm());
                }
            }
        }
    }
}

Now the sourcecode of the form ScreenSaverForm.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ScreenSaver
{
    public partial class ScreensaverForm : Form
    {

        Point PrevPos; // previous position of the cursor

        public ScreensaverForm()
        {
            InitializeComponent();
        }

        private void ScreensaverForm_Load(object sender, EventArgs e)
        {
            FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;  
            this.Bounds = Screen.PrimaryScreen.Bounds;
            Cursor.Hide();
            TopMost = true;
        }

        private void ScreensaverForm_MouseMove(object sender, MouseEventArgs e)
        {
            // if there was a previous point saved, compare to new point
            if (!PrevPos.IsEmpty)
            {
                // if mouse was moved, exit
                if (PrevPos.X != e.X || PrevPos.Y != e.Y)
                    Close();
            }
            PrevPos = new Point(e.X, e.Y);
        }

        private void ScreensaverForm_MouseDown(object sender, MouseEventArgs e)
        {
            Close();
        }

        private void ScreensaverForm_KeyDown(object sender, KeyEventArgs e)
        {
            Close();
        }
    }
}

The form is still empty, but can be filled with controls and functionality to your heart's desire.
To make windows recognize this screensaver, the .exe - file of the project has to be moved to C:\WINDOWS\system32 and be given the ending ".scr".
In the next post you find a complete screensaver with configuration form, it is a screensaver resembling the Matrix screens.

No comments:

Post a Comment