Advertisment

Write your own Screensaver

author-image
PCQ Bureau
New Update

Although modern monitors do not require screensavers for preventing burn-ins, they are still used for the often interesting things they display on the screen.

Advertisment

Honestly, I had no idea of how screensavers worked or how to code one until a few days back. However, a little free time at work gave me the opportunity to research this area and I found that creating one’s own saver was no big deal. So let’s see how to code a custom screensaver in Visual Basic 6.

Creating a new project

First, open VB and create a new ‘Standard EXE’ project. Screensavers are nothing but normal Windows Executables (EXEs) renamed with a .SCR extension. (Of course, this also means that a .SCR file is a potential virus risk. So take care when accepting .SCR files as e-mail attachments.) Call the newly created project any name, like

MySaver.

Advertisment

Now go into the project Properties and change the name of the project to have the keyword SCRNSAVER: at the beginning. That is, the name of the project should now read ‘SCRNSAVER: MySaver’. This is to set a flag in the header identifying this program executable as a screensaver so that it will show up correctly in the Display Properties Screensaver tab.

Adding common API calls

To run a screensaver, there are a few API calls that VB must make to Windows, as there are no equivalent functions in it. These common API calls can be embedded in a VB module. Insert a new code Module to the VB project and add the following code to it. (The text in pink color below is not part of the code).

Advertisment

Declare Function ShowCursor Lib “user32” (ByVal bShow As Long) As Long



Declare Function FindWindow Lib “user32” Alias “FindWindowA” (ByVal lpClassName As String, ByVal lpWindowName As String) As Long


Declare Function GetSystemMetrics Lib “user32” (ByVal nIndex As Long) As Long


Declare Function SetWindowPos Lib “user32” (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long





Constants for screen dimensions




Public Const SM_CXSCREEN = 0


Public Const SM_CYSCREEN = 1


Public Const HWND_TOP = 0


Public Const SWP_SHOWWINDOW = &H40






Constants for application




Public Const SW_SHOWNORMAL = 1


Private Const APP_NAME = “Saver”

The above code lets you call the Windows API functions ShowCursor, FindWindow, GetSystemMetrics and

SetWindowPos. These functions enable the VB program to show and hide the mouse cursor, find whether another instance of the program is already running, get the width and height of the screen, and set the width and height of the program

respectively. 

Next you need some common routines that are to be called in all screensavers.

Advertisment

Sub Main()



Select Case Mid(UCase$(Trim$ (Command$)), 1, 2)


Case “/C” Config Menu


frmConfig.Show


Case “”, “/S” Screensaver Show


runScreensaver


Case “/P” Preview Anim


End


Case “/A” Password Protect


MsgBox “Password Protection not available yet” 


End Select


End Sub


Private Sub runScreensaver() ‘ start the screensaver


checkInstance Any other instance?


ShowCursor False Hide mouse


Load frmMain Load the main form


frmMain.Show Show the form


End Sub


Sub exitScreensaver() Exit the screensaver


ShowCursor True Display Cursor


End Quit


End Sub


Private Sub checkInstance()


If Not App.PrevInstance Then Exit Sub


If FindWindow(vbNullString, APP_NAME) Then End


frmMain.Caption = APP_NAME


End Sub
























The four procedures here are more or less obvious in what they achieve, except for probably the first one. The subroutine Main will be the first function called when the screensaver runs. Windows passes a parameter to the screensaver depending on what is called. When a screensaver is activated automatically or through the ‘Preview’ button it gets no parameter or a “/S”. When the ‘Settings’ button is pressed, the screensaver is called with a “/C” parameter. The Password settings sends a “/A” and the small monitor that displays a preview is “/P”. Depending upon the input given, we call the appropriate procedure to run the screensaver. Note that the above code does not perform any action for “/P” and “/A” as yet.

The screensaver main form

Advertisment

Next, add a form to your project and call it frmMain. Change its background color to black and turn off the caption, title bar, borders and the minimize/maximize buttons. Add a standard VB timer control by the name tmrTimer. Also add a picture box to the form and call it picLogo. Assign the Picture property to any image you wish.

One of the basic things to remember when coding a screensaver is that you need to capture all events that can occur on the main form as well as on any controls on the form (like the PictureBox we have) and perform an appropriate action, which is mostly to terminate the saver. Whenever there is a mouse move, click, key press or any other event, simply call the exitScreensaver procedure as in the following example.

Private Sub Form_Click()



exitScreensaver exit screensaver


End Sub

Advertisment

The next thing is to initialize the form that you wish to display. At the minimum, you will need to cover the entire screen including the Windows taskbar with the form. Take a look at the following code as an example. The code will simply show a full screen blank display.

Private Sub Form_Load()



Dim cx As Long Screen width


Dim cy As Long Screen height


Dim RetVal As Long Return from API call


cx = GetSystemMetrics(SM_CXSCREEN) Get full screen width


cy = GetSystemMetrics(SM_CYSCREEN) Get full screen height


Make the form fill the full screen


RetVal = SetWindowPos(Me.hwnd, HWND_TOP,0,0,cx,cy,SWP_SHOWWINDOW)


Any custom stuff if you want here. 


End Sub







If you wish to have animations or any other embellishments, you must use the timer control that we inserted in the form and use that to periodically display graphics or other things you wish. Due to space constraints, I’m not including the source code here. However, you can pick up a sample screensaver and its VB source project from the accompanying CD.

Advertisment

Conclusion

As you can see, writing a screensaver is simplicity itself. The complexity only depends upon the level of animation or graphics support you wish to give in the screensaver. The complete source code with extensive comments in the form of a template VB Project is included in the CD with this issue (in the directory /cdrom/sorc_code/ssaver), which you can use to create your own saver. Also as a bonus are two other screensavers–one that demonstrates simple animation in the form of a flying logo and the other one for Star Trek fans like me, a fully configurable animated screensaver of the Starship USS Enterprise flying through space. wishing you fun and happy coding.

Vinod Unny  is a technology consultant at iSquare Technologies

Advertisment