|
|
Title | Make a slide show screen saver in VB .NET |
Description | This example shows how to make a slide show screen saver in VB .NET. |
Keywords | VB.NET, screen saver, commandline, command line, parameters |
Categories | Graphics, Windows, Multimedia |
|
|
Thanks to John Daly.
The program starts from a Main subroutine. That routine examines the command line parameters for the flags /p, /c, and /s.
- /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.
- /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.
|
|
Public Enum ActionType
actConfigure
actPreview
actRun
End Enum
Sub Main(ByVal args As String())
Dim frmMain As New frmMain
Dim frmOption As New frmOptions
If VerifySettingsFile() Then
LoadSettings()
Else
Application.Run(frmOption)
Exit Sub
End If
'get our arguments
If args.Length = 0 Then
action = ActionType.actRun
Else
Select Case args(0).ToLower
Case "/p"
action = ActionType.actPreview
Case "/c"
action = ActionType.actConfigure
Case "/s"
action = ActionType.actRun
End Select
End If
'process our request
Select Case action
Case ActionType.actConfigure
Application.Run(frmOption)
Case ActionType.actPreview
SetForm(frmMain, args(1))
Application.Run(frmMain)
Case ActionType.actRun
Application.Run(frmMain)
End Select
End Sub
|
|
When invoked for configuration, the program runs the frmOption form. This form loads and saves the image directory and the time between images in a file using XML serialization. See the code for details.
When invoked in screen saver mode, the program displays frmMain.
When invoked for preview, the program calls subroutine SetForm and then displays frmMain. Subroutine SetForm reparents the form into the preview window. (This process is impressively awkward. Perhaps they'll streamline this a bit in the next major release of Windows. Then we can learn how to do it all over again!)
|
|
Private Sub SetForm(ByRef f As Form, ByRef arg As String)
Dim style As Integer
Dim previewHandle As Integer = Int32.Parse(CType(arg, _
String))
Dim corners As New Win32Corners
Dim r As New Win32Corners.RECT
'get dimensions of preview
corners.GetClientRect(previewHandle, r)
With f
.WindowState = FormWindowState.Normal
.FormBorderStyle = FormBorderStyle.None
.Width = r.right
.Height = r.bottom
End With
'get and set new window style
style = corners.GetWindowLong(f.Handle.ToInt32, _
corners.GWL_STYLE)
style = style Or corners.WS_CHILD
corners.SetWindowLong(f.Handle.ToInt32, _
corners.GWL_STYLE, style)
'set parent window (preview window)
corners.SetParent(f.Handle.ToInt32, previewHandle)
'save preview in forms window structure
corners.SetWindowLong(f.Handle.ToInt32, _
corners.GWL_HWNDPARENT, previewHandle)
corners.SetWindowPos(f.Handle.ToInt32, 0, r.left, 0, _
r.right, r.bottom, _
corners.SWP_NOACTIVATE Or corners.SWP_NOZORDER Or _
corners.SWP_SHOWWINDOW)
End Sub
|
|
When the Timer on the program's main form receives a Tick event, the program calls subroutine DrawTheImage to draw the next image.
|
|
' Draw the image as large as possible without distortion.
Private Sub DrawTheImage()
Dim PanelGraphic As Graphics = pnlImage.CreateGraphics
PanelGraphic.Clear(Color.Black)
Try
If intLast = sFiles.GetLength(0) Then intLast = 0
Dim newImage As Bitmap = _
Bitmap.FromFile(sFiles(intLast))
Dim x As Integer
Dim y As Integer
Dim wid As Integer
Dim hgt As Integer
If (newImage.Height / newImage.Width) > _
(pnlImage.Height / pnlImage.Width) Then
' The new image is relatively tall and thin.
' Make the image as tall as possible.
hgt = pnlImage.ClientSize.Height
wid = CInt(hgt * newImage.Width / _
newImage.Height)
Else
' The new image is relatively short and wide.
' Make the image as wide as possible.
wid = pnlImage.ClientSize.Width
hgt = CInt(wid * newImage.Height / _
newImage.Width)
End If
x = (pnlImage.Width - wid) \ 2
y = (pnlImage.Height - hgt) \ 2
PanelGraphic.DrawImage(newImage, x, y, wid, hgt)
newImage.Dispose()
PanelGraphic.Dispose()
intLast += 1
Catch ex As Exception
DrawErrorImage()
End Try
End Sub
|
|
If the user presses a key or moves the mouse, the program ends.
|
|
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e _
As System.Windows.Forms.KeyEventArgs) Handles _
MyBase.KeyDown
Application.Exit()
End Sub
Private Sub pnlImage_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
pnlImage.MouseMove
Static done_before As Boolean
Static mouse_x As Long
Static mouse_y As Long
If Not done_before Then
done_before = True
mouse_x = e.X
mouse_y = e.Y
Else
If Math.Abs(e.X - mouse_x) > 10 Or Math.Abs(e.Y - _
mouse_y) Then
Application.Exit()
End If
End If
End Sub
|
|
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 Explorer, select the project. Then select the View menu's Property Pages command.
Click the Configuration Properties category and select Debugging. Enter the parameters you want to use in the "Command line arguments" text 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.
|
|
|
|
|
|