Creating a Visual Basic Screen Saver


Visual Basic is a great developers language it allows you to create applications that are very functional and to do so in a quick way. This is why people love VB6. However, at times it can be hard to figure out how to interact directly with the Windows Operating System. To do this we need to make API calls. Anyone who has delved into using VB and API's knows that it can be a little cumbersome at times. However, this tutorial demonstrates how you can make the API calls from your Visual Basic application and create a full fledged screen saver!

Note: This tutorial was adopted from one written by Lorenzo Dalla Vecchia.

A screen saver at its core is very simple. It is simply a window that covers the entire screen. In Visual Basic 6 this is accomplished by createing a form that will be displayed full size overtop of everything else. Lets start with the simple. Create a new VB6 project with just the main form. Set the properties to the following:

Property Setting
Caption ""
ControlBox False
MinButton False
MaxButton False
BorderStyle 0 - None
WindowState 2 - Maximized
BackColor &H0

With these properties, the form will be black, will have no border, buttons or title bar and will cover all the screen.
So, create a form and give it the above properties.

Now we must do two other things. First, each screen saver is a form that stays on the top of all others. Second, we notice that, during the display of a Screen Saver, the mouse cursor desappear.

To make our program do these two things, we first put these two lines in the declarations section of the form:

Private 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

Private Declare Function ShowCursor Lib "user32" _
	(ByVal bShow As Long) As Long

We are going to use these functions a little later. First lets create a helper subroutine that will always keep our form on top.

Sub AlwaysOnTop (FrmID As Form, OnTop As Boolean)
   Const SWP_NOMOVE = 2
   Const SWP_NOSIZE = 1
   Const HWND_TOPMOST = -1
   Const HWND_NOTOPMOST = -2

   If OnTop Then
      OnTop = SetWindowPos(FrmID.hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
      OnTop = SetWindowPos(FrmID.hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
   End If
End Sub

The next part is the load-up of our Screen Saver: put these lines in the Load event of the form. This will handle things when the Windows OS launches our screen saver.

Select Case LCase(Left(Command, 2))
    Case "/p": End
    Case "/s"
    Case Else: Me.Hide: frmSettings.Show: Exit Sub
End Select

Dim x As Integer
Call AlwaysOnTop(Me, True)
x = ShowCursor(False)

The form will always stay on top and the cursor will desappear.
When the Screen Saver unloads, we must do the inverse. Put these lines in the Unload event of the form.

Dim x As Integer
x = ShowCursor(True)

The first 5 lines of the Form_Load event are needed to make the Screen Saver work, work as an example or show its settings. Windows tells the Screen Saver what to do through command line parameters: /S tells the Screen Saver to start working, /P tells the Screen Saver to start working in a preview area, and no parameters makes the Screen Saver show its settings dialog. So, you may create a form called frmSettings that gives the opportunity to set Screen Saver parameters and to save them in a INI file or in the Registry; that dialog must unload the program when it is finished.

Another important thing to do, is to handle the events that unload the Screen Saver. A Screen Saver is usually unloaded when we move the mouse or hit a key, so we'll put the instruction Unload Me in the KeyPress event of the form. For the MouseMove or MouseClick events, use these lines:

Static Count As Integer
Count = Count + 1
If Count > 5 Then
   Unload Me
End If

This makes sure that the mouse is moved atleast a little bit before it exits. Otherwise your screen saver will exit too easily (with just a bump of the desk or something).

Well, it is your turn now! Can you creat an original and good Screen Saver. You can use controls (even OCXs) to create the graphic, and add code anywhere you want. If you want, you can download this example, located in the samples section of the site, demostrating a full working Screen Saver.

Installing The Screen Saver

When you have finished creating the look of the Screen Saver, it's the moment to compile it and bring it to life. Once you have compiled your Screen Saver, change its EXE extension to SCR. Then, copy it to the Windows\System folder.

To use the Screen Saver, go in the Control Panel, double-click the icon Screen, select the second tab (Screen Saver) and select your program from the Screen Saver dropdown list. Then, click OK.

Download Sample Source Code For VB Screen Saver

If you enjoyed this post, subscribe for updates (it's free)


well all is right bt after applying screen saver it appear as minimize mode. Awesome explanation, am truly impressed.

how to put swf as screen saver

it's very nice but how to put flash files as screen saver

where is the load event of

where is the load event of the form


.. Nice codes it heLps me a Lot on my thesis project thanks!.. :)


well all is right bt after applying screen saver it appear as minimize can i solve this problem.


add following line in Form Load
Me.WindowState = vbMaximized
For more help call me on 9221055664,9821546130.

Simple, complete, Easy: 5

Simple, complete, Easy: 5 Stars


That was an awesome explanation! Very lucid and informative... Thank you!
I have a question though... How does the screensaver detect the cpu idle time as in how is the screensaver loaded in the first place? is there any code for that? Could you please help?


wonderful explanation, am truly impressed.

Good presentation

Very informative and simple presentation...... Congrats!!!