WRITE YOUR OWN WINDOWS SCREEN SAVER

How to get started on writing one

Original date: 12 Jul, 2006

In this tutorial, I will show you how to write a screen saver. First, create a new Win32 project (configure for Win32 platform). Create a new code file and pull up a char and grab some Mountain Dew or what have you.

You will need to know generally how to program WindowsAPI but i'm teaching you new API features to use. A screen saver is used to prevent phosphor burn for all you old school monitor fans. Since the new LCDs are here with their sharp images, the screen saver became more of just a cool thing to put on your computer anyway. It's supposed to run when the computer is idle for more than a granted amount of time. Some people password the saver to protect sensitive information that was visible on the screen before the screen saver ran.

Know that i'm done talking about them, here's how to code them. The normal main function for a windows program is already written! It's located in a file called ScrnSave.lib. You must link it to your project before you begin (MSVS users, right click on resources, add existing, and then navigate to the library file located under PlatformSDK and lib).

Then you must also link ComCtl32.lib because the screen saver library uses it. Don't worry! I won't make you write COM so relax :)

Then include any other librarys and whatnot if you intend to use DirectX, OpenGL, GDI, or other graphic API for the screen saver.

Let's get to the point, here is a list of includes:


#include <windows.h>
#include <scrnsave.h>


These are needed in your code file. Nothing complicated, just an extra header for screen savers.

All of the basic window application initialization stuff was already done. The window procedure (read WindowAPI documentation if you don't what what that is) is already defined! It's called LONG WINAPI ScreenSaverProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam). That's the function prototype for the procedure. Here's what an empty basic one looks like:


LONG WINAPI ScreenSaverProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
// Handles screen saver messages
switch(message)
{
case WM_CREATE:
// Creation of the screen saver window
return 0;
case WM_ERASEBKGND:
// Erases the screen saver background
return 0;
case WM_TIMER:
// Handles the timer
return 0;
case WM_DESTROY:
// Cleans up the screen saver window
PostQuitMessage(0);
return 0;
}
return DefScreenSaverProc(hwnd,message,wparam,lparam);
}


These are the basic four message types to be handled. You must start up graphic engines and application under WM_CREATE. Fill the screen with black or whatever you want for a background under WM_ERASEBKGND. Place the screen saver logic for one frame under WM_TIMER (to move something, you would increment it's position by one here). WM_TIMER is called however many times a second you feel like it calling. Finally, clean up your app and graphics engines under WM_DESTROY.

You're missing an actual timer. You must set the timer so that your application will execute. Here's what you do:


uTimer = SetTimer(hwnd, 1, 1000, NULL);


Instead of 1000, enter the amount of milliseconds needed to pass for the code under WM_TIMER to be executed. uTimer is needed because:


KillTimer(hwnd, uTimer);


KillTimer must be placed under WM_DESTROY and that variable is needed to tell Windows what timer you're killing.

Now for the last bit:


BOOL WINAPI ScreenSaverConfigureDialog(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
return true;
}
BOOL WINAPI RegisterDialogClasses(HANDLE hmodule)
{
return true;
}


The two functions here are needed, but they can do nothing if you want them to. The configure dialog function is really supposed to pop up a dialog box so the user can configure your screen saver, but if you don't know how to write a dialog box, just return true.

The Register dialog box classes thing is for special controls in your configure dialog box, if you don't what to use classes for anything dialog related, just return true here too.

So, all you need to do is fill in the blanks with code simillar to an animation and you're set to run. But before you run, you must rename the .exe file to .scr and then run otherwise it does not work. It's annoying, but you're screen saver will close immediatly unless you give it a .scr extension. That way, Windows knows it's a screen saver, and will only terminate it when you move the mouse or press keys on the keyboard.

Have fun!