Author: Dave Murray
How to Write a Screensaver in Delphi
Answer:
In order to write a screensaver we need to include several procedures:
FormShow - hide cursor, setup message processing, start screensaver display
FormHide - stop screensaver display, show cursor
DeactivateScrSaver - process messages, deactivate if keys / mouse pressed
Typical code for these procedures is shown below.
You should ensure that your form is designed with the style fsStayOnTop. You also
need to make sure only one instance of your program is running in the usual way.
Finally you need to include the compiler directive {$D SCRNSAVE programname Screen
Saver} in your project unit (*.dpr).
Once your program is compiled then change it's filename extension to SCR and copy
it to your \WINDOWS\SYSTEM folder.
If running on Windows 98 you must disable the screensaver calls when it is running.
I have not needed to do this for versions of windows prior to 98 but it should work
on all versions. To do this insert the following call into your FormCreate method:
1 2 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, nil, 0);
3 4 //You must reverse this in your FormClose method: 5 6 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 1, nil, 0);
7 8 var9 crs: TPoint; {original location of mouse cursor}10 11 procedure TScrForm.FormShow(Sender: TObject);
12 {starts the screensaver}13 begin14 WindowState := wsMaximized; {ensure program fills screen}15 GetCursorPos(crs); {get current cursor position}16 Application.OnMessage := DeactivateScrSaver; {check for Mouse/Keys}17 ShowCursor(false); {hide the cursor}18 {start screensaver display...}19 //20 end; {procedure TScrForm.FormShow}21 22 procedure TScrForm.FormHide(Sender: TObject);
23 {returns control to the user}24 begin25 Application.OnMessage := nil; {discard the message}26 {stop the screensaver...}27 //28 ShowCursor(true); {bring the cursor back}29 end; {procedure TScrForm.FormHide}30 31 procedure TScrForm.DeactivateScrSaver(var Msg: TMsg; var Handled: boolean);
32 {detects Mouse movement or Keyboard use}33 var34 done: boolean;
35 begin36 if Msg.message = WM_MOUSEMOVE then{mouse movement}37 done := (Abs(LOWORD(Msg.lParam) - crs.x) > 5) or38 (Abs(HIWORD(Msg.lParam) - crs.y) > 5)
39 else{key / mouse button pressed?}40 done := (Msg.message = WM_KEYDOWN) or (Msg.message = WM_KEYUP) or41 (Msg.message = WM_SYSKEYDOWN) or (Msg.message = WM_SYSKEYUP) or42 (Msg.message = WM_ACTIVATE) or (Msg.message = WM_NCACTIVATE) or43 (Msg.message = WM_ACTIVATEAPP) or (Msg.message = WM_LBUTTONDOWN) or44 (Msg.message = WM_RBUTTONDOWN) or (Msg.message = WM_MBUTTONDOWN);
45 if done then46 Close;
47 end; {procedure TScrForm.DeactivateScrSaver}