Mega Code Archive

 
Categories / Delphi / System
 

How do I write a screen saver

Title: How do I write a screen saver? How do I write a screen saver? This FAQ is intended to discuss the mechanics involved in making a screen saver. It is not intended to be a guide regarding screen saver effects or coming up with them (to be honest the ones in the example are pretty bad - I'm not the one to come to if you want help on those). There are no guarantees placed upon the code you see here. It's been tested in Turbo Delphi 2006, and to the best of my knowledge works. Much of the code is based on Inprise TID #4534, though much has been changed from what is presented there for various reasons. So what is a screen saver? From a technical standpoint, a screen saver is nothing more than a program which satisfies certain requirements. Those are: 1) Be compiled with an *.SCR extension. 2) Have defined a resource string which describes the screen saver. 3) Provide a means to determine whether the program is running, and exit if it is. 4) Accept a set of set command-line parameters. 5) Act accordingly on those parameters. SCR & Resource String To handle the SCR extension and resource string is reasonably straight forward. In the main file: CODE {$R sid.RES} {$E scr} The first includes a resource file named sid.RES, the second defines the compiled extension to be scr. To make the resource file, call BRCC32. The file will have one string defined - for a screen saver, the description string is the resource string found in the first position. Is a copy running? A semaphore is used in that event. One is checked for on run time and then created by a name upon run if it doesn't exist. If it already exists the program exits. Command-line parms Command-line parms are accepted by the screen saver to determine what it should do. Of primary interest is the first parm. When it is received it should be checked for any combination ("C", "-C", or "/C" should all suffice, as well as nothing). Valid command-line parms: /C - Bring up configuration screen. /P - Run screen saver in window-box mode. This is the little box that comes up and shows you the screen saver when you select it. /S - Shows the screen saver in full-screen mode. /A - Password Change call (Win9X only) Configuration This is the screen you see when you click Settings. This can be straight-forward VCL. Create a form with the values you want to change/save. Have a cancel and save button (even an about button if you want it!). When you load the form, load the read-in values to the form. When you save the form, write them to registry. Nothing explicitly special. Preview This is the window-box mode. It will involve receiving a window handle to draw your screen-saver action on. You will need to associate a canvas to the window handle and then do your drawing there. It will need to respond reasonably quickly to input to exit (~10ms). Show This is the screen saver show mode. It will involve a VCL form that you make full screen, and then draw on. A number of events will be defined so the screen saver may respond to inputs. Password Believe it or not, the responsibility of handling the limited password facility in Windows 9X fell upon each screen saver. This call is pretty standard, however. Sample Project Source Files Due to length I won't be copying any specific source here and will provide it in the zip file below. Descriptions of source files you will find are below: ssdemo.dpr This is the main source file. Bring it up, for you will notice many changes in it, primarily to handle the command parms. configunit.pas The pas file associated with the configuration menu. Nothing that is particularly remarkable - load values into form on startup, give option to save them on exit. drawunit.pas The form for Show mode. FormKeydown, FormMouseMove, and FormMouseDown are defined to handle events and detect standard user input. FormCreate is used to handle initializations, show the form in full screen, and perform any initial actions for the screen saver action. FormClose performs any deinitializations necessary. To draw the effects: OnIdle is set to a trigger procedure which draws one step of the effects - PostMessage is used to place it within queue. Upon termination, CheckMyTerminate is called. The primary reason for this call is to handle the Windows 9X password security requirement. scrnsave.pas Some generic utility functions related to screen savers. You shouldn't have to touch these (mostly) to do what you need. Initialization code exists here to handle a few preliminaries for these functions. read_values : Read values out of the registry into a global record - substitutes defaults if they are not found. write_values : Writes values to the registry. checkSSpassword : Called to check for a password (Win9X). Get_SSaverName : Not used in this project. Will read a file and return the 1st resource string in it. Useful if you want to find out screen saver names. SSPwdChangeCall : Called to change the screen saver password (Win9X). SS_Initialize : Most of this code is the same so I placed it here. Returns data from the command-line parms, as well as the semaphore address (look at the address and change it if you reuse this code). WinMSSinceStart : Timing code which is useful for screen drawing effects if a delay is required, since sleep() is generally not a good idea in long periods. sid.rc The resource file containing the resource id string for the project. Change and compile this to change the string that shows up in the screen saver config. ssaction.pas Screen saver action code - this takes the string in config and shows it in a random spot on the screen in a random color with the configured delay between showing it. I set up SS_Init, SS_Start, and SS_End procedures which all take window handles. This is so the code can be used both for Window box mode (Preview) and screen saver mode (Show). The graphics could probably be drawn to a TBitmap and then StretchDrawed to the windows canvas handle to give a better effect, though I didn't do that. WinRect holds the screen dimensions of whatever window was passed. Download the Sample Project As was indicated, the screen saver puts a text line to the screen in various positions with a delay. This file can be downloaded here. Relevant questions may be asked in the thread announcing this.