Writing Windows Screensavers in C

It is quite simple to use C to write a screensaver because there is a screensaver library with the commonly required functions and detailed examples with most compilers.

N.B. This article is now quite dated although should still produce a working screensaver. The code segments are from the 16-bit SDK.

Summary

DEF file

You need to provide a label to mark the code as screensaver you do this by adding the following lines to the .DEF file. Some newer compilers no longer use DEF files so you will have to enter the details from this else where in your development environment. Luckily most compilers are backwards compatible and will allow you to use a DEF file if you wish to do so. The exports are also included in the DEF file.

DESCRIPTION 'SCRNSAVE :Interesting Title'
EXETYPE     WINDOWS
EXPORTS
            SCREENSAVERPROC                  @1
            SCREENSAVERCONFIGUREDIALOG       @2
            ARROWCONTROLPROC                 @3
            DLGCHANGEPASSWORD                @4
            DLGGETPASSWORD                   @5
            DLGINVALIDPASSWORD               @6
            HELPMESSAGEFILTERHOOKFUNCTION    @7

Headers and associated requirements

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

Also include scrnsave.lib in the linker options for your development environment

You also need the following variables to run the library.
char szAppName[40];

// Externals defined in SCRNSAVE.LIB. Required for all screen savers.

HINSTANCE _cdecl hMainInstance;
HWND _cdecl hMainWindow;
char _cdecl szName[TITLEBARNAMELEN];
char _cdecl szIsPassword[22];
char _cdecl szIniFile[MAXFILELEN];
char _cdecl szScreenSaver[22];
char _cdecl szPassword[16];
char _cdecl szDifferentPW[BUFFLEN];
char _cdecl szChangePW[30];
char _cdecl szBadOldPW[BUFFLEN];
char _cdecl szHelpFile[MAXFILELEN];
char _cdecl szNoHelpMemory[BUFFLEN];
UINT _cdecl MyHelpMessage;
HOOKPROC _cdecl fpMessageFilter;
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.
 */
LONG FAR PASCAL __export 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(hWnd, msg, wParam, lParam)
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 FAR PASCAL __export ScreenSaverConfigureDialog(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
This controls a dialog to setup all of the configuration, to use the default password dialog, just use the following code.

FARPROC fpDialog;

if((fpDialog = MakeProcInstance(DlgChangePassword,hMainInstance)) == NULL)
    return FALSE;
DialogBox(hMainInstance, MAKEINTRESOURCE(DLG_CHANGEPASSWORD),hDlg, fpDialog);
FreeProcInstance(fpDialog);

Example Source Code

DOWNLOAD 16bit C Source Code


Home Education Employment Hobbies Screen Savers
Email : Andy.Clark@Dial.Pipex.Com - Last Updated : 12th Dec 1998