Writing Windows(32bit) Screensavers in C

It is quite simple to use C to write a screensaver for Windows 9x or Windows NT because there is a screensaver library with the commonly required functions and detailed examples with most compilers. The details here assume that you will be using a single windows timer to control your animation.

Summary

Screensaver Name

There are differences between Windows 9x and Windows NT with where the name of the screensaver is taken from.

In Windows 9x the control pannel simply uses the filename (minus the .SCR) in the list of screensavers.

In Windows NT the control pannel uses the resource string table to extract the name, this has to meet several requirements.

e.g.

STRINGTABLE DISCARDABLE 
BEGIN
    Description             "Mr Bump Screensaver"
END

Headers and associated requirements

Don't forget to #include <scrnsave.h> and #include <windows.h>

The <scrnsave.h> is a useful reference of requirements for using the screensaver and library and is worth checking to see if has more recent recommendations to this document.

This file requires your to add scrnsave.lib to the linker options of your development environment

You also need the following variables to run the library.

char szAppName[40];
Set
szAppName
to the name of your screensaver.

Procedures

The main part of the screensaver is the ScreenSaverProc.

/* ScreenSaverProc - Main entry point for screen saver messages.
 *  This function is required for all screen savers.
 *
 * Params:  Standard window message handler parameters.
 *
 * Return:  The return value depends on the message.
 *
 *  Note that all messages go to the DefScreenSaverProc(), except
 *  for ones we process.
 */
LRESULT WINAPI ScreenSaverProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
This needs to respond to the following messages.
WM_CREATE
Initialise and start a timer with the SetTimer function
WM_TIMER
Do the animation
WM_DESTROY
Finish the screensaver
WM_ERASEBKGND
You can set the background if you need to.
Everything else
Do everything else with DefScreenSaverProc
LRESULT WINAPI DefScreenSaverProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)

See #include <scrnsave.h> for details of what this function does.

You will also need a function to handle the configuration.

/* ScreenSaverConfigureDialog -- Dialog box function for configuration
 * dialog.
 *
 * Params:  hWnd -- Handle to window
 *
 * Return:  None
 */
BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)

Unlike 16bit screensavers you no longer need to provide a change password button on the dialog. See security section for more details of passwords.

FARPROC fpDialog;

if((fpDialog = MakeProcInstance(DlgChangePassword,hMainInstance)) == NULL)
    return FALSE;
DialogBox(hMainInstance, MAKEINTRESOURCE(DLG_CHANGEPASSWORD),hDlg, fpDialog);
FreeProcInstance(fpDialog);
Finally add the following segment to your source code.
/* To allow the programmer the ability to register child control windows, this
 * function is called prior to the creation of the dialog box.  Any
 * registering that is required should be done here, or return TRUE if none
 * is needed...
 */
BOOL WINAPI RegisterDialogClasses (HANDLE hInst) 
{
    return TRUE;
}

Home Education Employment Hobbies Screen Savers
Email : Andy.Clark@Dial.Pipex.Com - Last Updated : 25th July 1999