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.
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
szAppNameto 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.
/* 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);