Brought to you by Mainstram System Development
 

MSD Home Page
Company Profile
MSD Whats News
MSD Products
Tutors & Downloads
MSD Delphi Forum
Hot Delphi Links
MSD Site Map Contact Us
  Tutors & Downloads

January 2000: Windows Screen Savers
This tutor focuses on creating Windows Screen Savers. This is to demonstrate that the power of Borland Delphi is not only limited to Database Applications. Check the download section for more Window Screen Saver that was created by Borland Delphi.

Writing a Windows Screen Saver in Borland Delphi.
  Writing a Windows Screen Saver in Borland Delphi can be both Fun and Challenging. It is a simple exercise, but uses a variety of Programming Techniques and Skills. In this Tutor, the basics of writing a Windows Screen Saver will be explained to you, and for those of you that do not like typing, the source code can be downloaded here. Let us look at a list of things we need to understand to create the Windows Screen Saver.
  • The Working of a Basic Windows Screen Saver.
  • What Windows Expect from a Windows Screen Saver.
  • Trapping Windows Messages.
  • How to use Command Line Parameters.
  • How to check for Multiple Instances of the Windows Screen Saver.
Let us look at the working of a Basic Windows Screen Saver.
A Windows Screen Saver is nothing more than just a simple executable application with a "SCR" extension. We can proof that by renaming any Screen Saver Application (SCR) file to have a "EXE" extension. Now, try to execute it by double clicking on the file. So, Delphi can easily create a Windows Screen Saver, just by creating an application with an "EXE" extension and renaming it to a "SCR" extension.

Let us have a look at what Windows expect from a Windows Screen Saver.
There are two basic requirements for any application to be a Windows Screen Saver. One thing that is very important to note, is the way in which Windows activate a screen saver. When you select a screen saver that must activate, you also specify a timeout, indicating how long the screen must display its contents before starting the Windows Screen Saver application. Windows wait the specified time, and if there was no system activity (no keyboard or mouse input), is starts the Windows Screen Saver application. Important to note that it does it every time the timer times out, even if the Windows Screen Saver is already running.

The other important thing to keep in mind, is the way Windows activates the Windows Screen Saver Application. To activate the Windows Screen Saver, a /s is passed to the application, while on the other hand, a /c is passed to the application, to configure it.

What are Window Messages?
The Windows Operating System uses a very simple, but effective way of sending information from one application to another. It uses Windows Messages. It is possible for an application to generate and trap these messages.

OK. Are you ready? Let?s start then. First we create a new application. Set the following properties of the form to the following values:

  • Property BorderStyle - Value bsNone
  • Property FormStyle - Value fsStayOnTop
  • Property WindowState - Value wsMaximized
Create the following Events on the form:

Event OnShow:

procedure TfrmScreen.FormShow(Sender: TObject);
begin
  GetCursorPos(crs);
  Application.OnMessage := DeactivateScrnSaver;
  ShowCursor(false);
end;
The onShow event triggers the first time the application sets focus to the form. The GetCursorPos saves the cursor position in a variable crs (this variable must be declared in the form by the user), for later use. A message handler is defined and the cursor is set to invisible.

OnHide:

procedure TfrmScreen.FormHide(Sender: TObject);
begin
  Application.OnMessage := nil;
  ShowCursor(true);
end;
When the application closes, we need to resore all the changes we have made to the system. Restoring the message handler, and showing the cursor again.

The next step may sound very difficult, but it is really very straight forward. We need to create a message handler to process system messages. In order to create message handler, we need to create a Public method called DecativateScrnSaver. Your form?s declaration should look similar to this:

TfrmScreen = class(TForm)
  procedure FormActivate(Sender: TObject);
  procedure FormHide(Sender: TObject);
public
  procedure DeactivateScrnSaver(var Msg : TMsg; 
                                       var Handled : boolean);
end;
Here is the implementation of our new message handler:

procedure TfrmScreen.DeactivateScrnSaver(var Msg: TMsg; 
                                                varHandled: boolean);
var
  done : boolean;

begin
  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
    frmScreen.Close;
end;
The message handler works by trapping all the Windows Messages, and search for the specific windows message. The first part of the method searches for the WM_MOUSEMOVE message. If that message is found, we know the user moved the mouse. All we need to do is to test if it moved more than 5 pixels. This can be cone by using the variable crs that we initialized in the onActicate event of the application. If it moved more then 5 pixels, we terminate the screen saver. If it was not a mouse move message, we test for a keyboard message. If it was not one of them, we ignore the message, and continue with the Windows Screen Saver routine.

And that?s all there is to creating a simple Windows Screen Saver. Now you can add a second form to your application that handles the configuration of the Windows Screen Saver. Talking about Windows Screen Saver Configuration. We still need to modify our Windows Screen Saver application to recognize different Windows Commands from Command Line Parameters. Let?s have a look at our Project Source. It can be found from Project | View Source.

Var
  sParam : String;
  iPos : integer;

begin
  {/s = Avrivate Screen Saver}
  {/c:xxxxxx = Cofiguration}
  sParam := UpperCase(ParamStr(1));

{This is to strip only the /c from the command line string} iPos := Pos(':',sParam); If iPos > 0 then sParam := Copy(sParam,1,iPos-1); if (ParamCount > 0) and (sParam = '/S') then begin Application.CreateForm(TfrmScreen, frmScreen); Application.Run; end else If (ParamCount > 0) and (sParam = '/C') then begin Application.CreateForm(TfrmConfig, frmConfig); Application.Run; end;

Now you may compile and execute your Windows Screen Saver. If you are lucky enough, the application will compile without any major errors. As soon as you move the mouse, or press a key on the keyboard, the application wil terminte. Now, let's add the final piece of code to make this application a true Windows Screen Saver.

The only thing that is needed in your application is the check for multiple instances of the Windows Screen Saver. This can be done with the hPrevInst variable. Simply encapsulate the program source above in this if statement:

if hPrevInst = 0 then
begin
  .
  .
  .
end;
This concludes the tutor on creating a Windows Screen Saver in Borland Delphi.



 
�MSD (Pty) Ltd., All Rights Reserved.
MSD logo are Registered Trademarks of
Mainstream System Development.
Comments & Questions to The Web Master

01/12/2000 10:30