What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a slide show screen saver (PictureSaver)
Keywordsscreen saver, commandline, command line, parameters, slide show, PictureSaver
CategoriesWindows, Multimedia, Graphics
The program starts from a Main subroutine. That routine examines the command line parameters for the flags /p, /c, and /s.

The screen saver should display a configuration screen. This program displays the frmConfig form as a modal dialog and then exits.

The screen saver should run as a normal screen saver. The program also runs in this mode if it receives no command line flag or some other flag. To display as a normal screen saver, the program displays the frmCanvas form as a modal dialog.

The screen saver should display as a preview. The second command line argument gives the hWnd (window handle) of the preview window in which the program should draw the preview. The program turns that value into a Long and uses GetClientRect to get that window's size and position.

It loads the screen saver form frmCover and adds the style WS_CHILD to the window to indicate that it is a child window. It reparents frmCover so it is a child of the preview window and uses SetWindowLong to make the preview window the form's parent. Finally, the code displays frmCover at the preview form's location.

' Start the program.
Public Sub Main()
Dim args As String
Dim preview_hwnd As Long
Dim preview_rect As RECT
Dim window_style As Long


    ' Get the configuration parameters.

    ' Get the command line arguments.
    args = UCase$(Trim$(Command$))

    ' Examine the first 2 characters.
    Select Case Mid$(args, 1, 2)
        Case "/C"   ' Display configuration dialog.
            RunMode = rmConfigure
        Case "", "/S"   ' Run as a screen saver.
            RunMode = rmScreenSaver
        Case "/P"       ' Run in preview mode.
            RunMode = rmPreview
        Case Else       ' This shouldn't happen.
            RunMode = rmScreenSaver
    End Select

    ' Run in the appropriate mode.
    Select Case RunMode
        Case rmConfigure    ' Display configuration dialog.

        Case rmScreenSaver  ' Run as a screen saver.
            ' Make sure there isn't another one running.

            ' Display the cover form.
            ShowCursor False

        Case rmPreview      ' Run in preview mode.
            ' Get the preview area hWnd.
            preview_hwnd = GetHwndFromCommand(args)

            ' Get the dimensions of the preview area.
            GetClientRect preview_hwnd, preview_rect

            Load frmCover

            ' Set the caption for Windows 95.
            frmCover.Caption = "Preview"

            ' Add WS_CHILD to make this a child window.
            window_style = GetWindowLong(frmCover.hwnd, _
                GWL_STYLE) Or WS_CHILD
            SetWindowLong frmCover.hwnd, _
                GWL_STYLE, window_style

            ' Set the window's parent so it appears
            ' inside the preview area.
            SetParent frmCover.hwnd, preview_hwnd

            ' Save the preview area's hWnd in
            ' the form's window structure.
            SetWindowLong frmCover.hwnd, _
                GWL_HWNDPARENT, preview_hwnd

            ' Show the preview.
            SetWindowPos frmCover.hwnd, _
                HWND_TOP, 0&, 0&, _
                preview_rect.Right, _
                preview_rect.Bottom, _
    End Select
End Sub
The frmConfig form lets the user configure the program. It lets the user determine the number of seconds between pictures and whether the program should display file names. It also lets the user enter a list of directories from which the program should display pictures. If the user clicks Ok, the program saves the parameters in the registry. See the code for details.

The frmCover form displays the screen saver. When its Timer event fires, the form randomly selects a picture from among the graphical files in the configured directories and displays it. In preview mode, the program shrinks the picture to fit in the preview window.

' Display the next picture.
Private Sub tmrShowPicture_Timer()
Dim file_num As Integer
Dim file_name As String
Dim pic_scale As Single
Dim wid As Single
Dim hgt As Single
Dim X As Single
Dim Y As Single

    ' See if it's time yet.
    If Now < g_NextTime Then Exit Sub

    ' Record the next time we should display a picture.
    g_NextTime = DateAdd("s", g_Interval, Now)

    ' Make sure we have a picture.
    If g_Files.Count < 1 Then
        DrawWidth = 5
        Line (0, 0)-(ScaleWidth, ScaleHeight), vbRed
        Line (ScaleWidth, 0)-(0, ScaleHeight), vbRed
        Exit Sub
    End If

    ' Pick a random picture.
    file_num = Int(Rnd * g_Files.Count + 1)

    ' Load the picture.
    On Error GoTo FileErr
    picHidden.Picture = LoadPicture(g_Files(file_num))

    ' See if we are in preview mode.
    If RunMode = rmPreview Then
        ' Preview mode. Scale the picture to fit.
        If ScaleWidth / Screen.Width < ScaleHeight / _
            Screen.Height Then
            pic_scale = ScaleWidth / Screen.Width
            pic_scale = ScaleHeight / Screen.Height
        End If
        wid = pic_scale * picHidden.ScaleWidth
        hgt = pic_scale * picHidden.ScaleHeight
        X = (ScaleWidth - wid) / 2
        Y = (ScaleHeight - hgt) / 2
        PaintPicture picHidden.Picture, X, Y, wid, hgt
        ' Display the picture at its full size.
        X = (ScaleWidth - picHidden.ScaleWidth) / 2
        Y = (ScaleHeight - picHidden.ScaleHeight) / 2
        PaintPicture picHidden.Picture, X, Y

        ' Display the file name if necessary.
        If g_ShowFileNames Then
            CurrentX = 0
            CurrentY = 0
            Print g_Files(file_num)
        End If
    End If
    Exit Sub

    DrawWidth = 5
    Line (0, 0)-(ScaleWidth, ScaleHeight), vbRed
    Line (ScaleWidth, 0)-(0, ScaleHeight), vbRed
    Exit Sub
End Sub
If the user moves the mouse, clicks a mouse button, or presses a key, frmCover unloads, ending the program.


In a program such as this one that reads command line parameters, it is handy to test the program with different parameters. In this example, you can test the program with the /c parameter.

It's a real hassle, however, to have to compile the program and then launch it from a shortcut or DOS window so you can pass it different parameters. Fortunately you can make the development environment start the program with command line parameters.

In Project menu, select PictureSaver Properties. Click the Make tab and enter your parameters in the Command Line Arguments box.


To install the screen saver, compile the program into an executable file. Change the file's extension from .exe to .scr and copy it into the system directory. That's all you need to do.

In VB5/6, you can tell the development environment to build the executable directly in the Windows directory with the .scr extension.

Bug Fix

Warren Buske brought this to my attention. In Windows 2000, if you open the screen saver in preview mode, it later appears shrunk in a tiny window instead of maximized as specified by the form's WindowState property. To fix this, the program now explicitly maximizes the screen saver window after displaying it:


' Force maximized (for Windows 2000). frmCover.WindowState = vbMaximized

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.