|
|
Title | Make a bouncing ball screen saver |
Keywords | screen saver, commandline, command line, parameters, bounce |
Categories | Windows, Multimedia, Graphics |
|
|
The program starts from a Main subroutine. That routine examines the command line parameters for the flags /p, /c, and /s.
- /c
- The screen saver should display a configuration screen. This program displays the frmConfig form as a modal dialog and then exits.
- /s
- 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.
- /p
- 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 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
Select Case RunMode
Case rmConfigure ' Display configuration dialog.
frmConfig.Show
Case rmScreenSaver ' Run as a screen saver.
frmCover.WindowState = vbMaximized
' Make sure there isn't another one running.
CheckShouldRun
' Display the cover form.
Load frmCover
frmCover.Show
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"
' Get the current window style.
window_style = GetWindowLong(frmCover.hwnd, _
GWL_STYLE)
' Add WS_CHILD to make this a child window.
window_style = (window_style Or WS_CHILD)
' Set the window's new style.
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, _
SWP_NOZORDER Or SWP_NOACTIVATE Or _
SWP_SHOWWINDOW
End Select
End Sub
|
|
The frmConfig form lets the user configure the program. It lets the user determine the number of bouncing balls to animate. 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 it loads, the form randomly generates the balls it will display. When its Timer event fires, the form examines each ball and adds the X and Y components of the ball's velocity to the ball's position, bouncing back any ball that hits an edge of the screen. The event handler then draws the ball.
|
|
' Move the balls.
Private Sub tmrMoveBalls_Timer()
Dim i As Integer
Dim wid As Single
Dim hgt As Single
' Erase the balls.
For i = 1 To NumBalls
With Balls(i)
FillColor = BackColor
Circle (.BallX, .BallY), .BallR, BackColor
End With
Next i
' Move and redraw the balls.
wid = ScaleWidth
hgt = ScaleHeight
For i = 1 To NumBalls
With Balls(i)
.BallX = .BallX + .BallVx
If .BallX < .BallR Then
.BallX = 2 * .BallR - .BallX
.BallVx = -.BallVx
ElseIf .BallX > wid - .BallR Then
.BallX = 2 * (wid - .BallR) - .BallX
.BallVx = -.BallVx
End If
.BallY = .BallY + .BallVy
If .BallY < .BallR Then
.BallY = 2 * .BallR - .BallY
.BallVy = -.BallVy
ElseIf .BallY > hgt - .BallR Then
.BallY = 2 * (hgt - .BallR) - .BallY
.BallVy = -.BallVy
End If
FillColor = .BallClr
Circle (.BallX, .BallY), .BallR, .BallClr
End With
Next i
End Sub
|
|
If the user moves the mouse, clicks a mouse button, or presses a key, frmCover unloads, ending the program.
Tip
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.
Note
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.
|
|
|
|
|
|