Delphi Tutorial


 

 

      

    

Download this example

          A Windows screen saver is basically just a standard Windows executable that has been renamed to have a .SCR filename extension. In order to interface properly, certain requirements must be met. In general, the program must:

  • Distinguish between active mode and configuration mode
    Disallow multiple copies of itself to run
    Exit when the user presses a key or moves the mouse

          First, we need to be able to load and save the current configuration. To do this, we should place the Speed Interval value into an initialization file (*.INI) in the user's Windows directory. Delphi's TIniFile object is just the thing for this.

          Add the following uses clause to the implementation section of the configuration form's unit:

uses
     IniFiles;

          Then, add the following procedure declarations to the configuration form unit:

private
     { Private declarations }
     procedure SaveConfig(Sender: TObject);
public
     { Public declarations }
     Speed : Integer;
     procedure LoadConfig(Sender: TObject);
end;

 

const
     CfgFile = 'FLOWERS.INI';

procedure TFlowers_CFG_Form.LoadConfig(Sender: TObject);
var
     IniFile : TIniFile;
begin
     IniFile := TIniFile.Create(CfgFile);
     Try
     With IniFile Do
          Speed := ReadInteger('Config', 'Speed',10);
     Finally
          inifile.Free;
     End;
end;

procedure TFlowers_CFG_Form.SaveConfig(Sender: TObject);
var
     IniFile : TIniFile;
begin
     IniFile := TIniFile.Create(CfgFile);
     Try
     With IniFile Do
          WriteInteger('Config', 'Speed', Speed);
     Finally
          IniFile.Free;
     End;
end;

 

Load the configuration automatically

procedure TFlowers_CFG_Form.FormCreate(Sender: TObject);
begin
     { First, load the configuration automatically
     whenever the program starts up. }

     LoadConfig(Self);
     Edit1.Text := IntToStr(Speed);
end;

 

For screensaver form, make it transparent at startup

procedure TFlowers_SCR_Form.FormCreate(Sender: TObject);
begin
     { Set transparent background }
     Brush.Style:=bsClear;

     Timer1.Enabled := False;
end;

     In order to periodically display flowers on the desktop, we must respond to the OnTimer event of the Timer component

procedure TFlowers_SCR_Form.Timer1Timer(Sender: TObject);
Var
     X, Y : Integer;
begin
     { Select random position }
     X := Random(Screen.Width);
     Y := Random(Screen.Height);

     { Clear the previous image }
     Image1.Picture:=Image2.Picture;

     { Randomly assigned a image from image list
     GetBitmap - Retrieves a specified image as a bitmap.}

     ImageList1.GetBitmap(Random(10), Image1.Picture.Bitmap);

     { Set new image position }
     Image1.Top := Y - Round(Image1.Height / 2);
     Image1.Left := X - Round(Image1.Width / 2);
end;

 

          Next, we will need a way to deactivate the screen saver when a key is pressed, the mouse is moved, or the screen saver form looses focus. One way to do this is to create an handler for the Application.OnMessage event that looks for the necessary conditions to terminate the screen saver.

procedure TFlowers_SCR_Form.DeactivateScrnSaver(var Msg : TMsg; var Handled : boolean);
Var
     Done : Boolean;
begin
     { When a WM_MOUSEMOVE window message is received,
     compare the new coordinates of the mouse to the
     original location. If it has moved more than our
     threshold (5 pixels in any direction), then we close
     the screen saver. Otherwise, if a key is pressed or
     another window or dialog box takes the focus, the screen
     saver closes. }

     If Msg.message = WM_MOUSEMOVE Then
          Done := (Abs(LOWORD(Msg.lParam) - CRS.X) > 5) Or
                    (Abs(HIWORD(Msg.lParam) - CRS.Y) > 5)
     Else
          Done := (Msg.message = WM_KEYDOWN) Or (Msg.message = WM_ACTIVATE) Or
                    (Msg.message = WM_ACTIVATEAPP) Or (Msg.message = WM_NCACTIVATE);
     If Done Then Close;
end;

 

          In order for this procedure to go into effect, however, we need to set the Application.OnMessage property and get the original position of the mouse cursor. We can do this is in the form's OnShow event handler:

procedure TFlowers_SCR_Form.FormShow(Sender: TObject);
begin
     { Get the original position of the mouse cursor }
     GetCursorPos(CRS);
     Timer1.Interval := Flowers_CFG_Form.Speed * 100;

     Application.OnMessage := DeactivateScrnSaver;

     { Hiding the mouse cursor. }
     ShowCursor(False);

     Timer1.Enabled := True;
end;

 

          Here we also specify the timer's interval and activate it, as well as hiding the mouse cursor. Most of these things should be undone, however, in the form's OnHide event handler

procedure TFlowers_SCR_Form.FormHide(Sender: TObject);
begin
     Application.OnMessage := nil;
     Timer1.Enabled := False;
     ShowCursor(True);
end;

 

          You can define the text that will appear in the Control Panel Desktop list of screen savers by adding a {$D text} directive to the project source file. The $D directive inserts the given text into the module description entry of the executable file. For the Control Panel to recognize the text you must start with the term "SCRNSAVE", followed by your description.

Beneath the directive "{$R *.RES}", add the following line:

{$D SCRNSAVE Flowers Screen Saver}

          Windows launches the screen saver program under two possible conditions (Actually there are more than two parameters, for this simple example I am going to discuss only the main two parameters). It distinguishes between the two modes by adding a command line parameter -- "/s" for active mode and "/c" for configuration mode. For our screen saver to function properly with the Control Panel, it must check the command line for these switches. Modify the project source as follows:

begin
     { Only one instance is allowed at a time.
     The hPrevInst variable is a global variable defined
     by Delphi to point to previous instances of the current
     program. It will be zero if there are no previous
     instances still running. }

     If hPrevInst = 0 Then
     Begin
          Application.Initialize;
          { If screen saver form call . . . }
          If (ParamCount > 0) And (UpperCase(ParamStr(1)) = '/S') Then
          Begin
               { Flower_SCR_Form needs to be the Main Form.}
               Application.CreateForm(TFlowers_SCR_Form, Flowers_SCR_Form);
               Application.CreateForm(TFlowers_CFG_Form, Flowers_CFG_Form);
          End Else
          { If screen saver configuration form call . . . }
          If (ParamCount > 0) And (UpperCase(Copy(ParamStr(1),1,2)) = '/C') Then
          Begin
               { Flower_CFG_Form needs to be the Main Form.}
               Application.CreateForm(TFlowers_CFG_Form, Flowers_CFG_Form);
               Application.CreateForm(TFlowers_SCR_Form, Flowers_SCR_Form);
          End;
          Application.Run;
     End;
end.

          Once you've tested and debugged your screen saver, you are ready to install it. To do so, simply copy the executable file (Flowers.exe) to the Windows directory, changing its filename extension to .SCR in the process (Flowers.scr). Then, launch the Control Panel, double-click on Desktop, and select Screen Saver | Name. You should see "Spheres Screen Saver" in the list of possible screen savers. Select it and set it up.

DOWNLOAD this example

Click here to download this complete example for Delphi 6. (File size 301KB)

Click here to download compiled fully functional above screen saver. (File size 476KB)


Click here To download compiled image warping screen saver. This one uses Open GL technology. (File size 472KB)

Go top

 


Previous Page First Page Next Page
 
Copyright © 2003 digitalcoding.com. All rights reserved.