ProblemYou have some down time between projects at work, and you want to implement a simple screensaver in Visual Basic. SolutionSample code folder: Chapter 14\SimpleScreenSaver Use this recipe's sample code as an example of how to develop a screensaver using .NET. The code creates a simple screensaver that displays either the time or the date and time together in the center of the display. DiscussionCreate a new Windows Forms project, and name it SimpleScreenSaver. Change the name of the main form from Form1.vb to ScreenSaver.vb. Open that form, and set the following properties:
This form will serve as the screensaver view. Maximizing it and setting it as the top-most form forces it to consume the entire display. Add a Label control named CurrentTime to the form's surface, and set these properties:
Next, add a Timer control named ClockTimer to the form. Set its Interval property to 1000(which means 1000 milliseconds), and set its Enabled property to TRue. The form should be somewhat bland and have the general look of Figure 14-20. Figure 14-20. The design of the screensaver formAdd the following code to the form's code template: Private LastMousePosition As New Point(-1, -1) Private Sub ClockTimer_Tick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ClockTimer.Tick ' ----- Show the time. RefreshClock( ) End Sub Private Sub RefreshClock( ) ' ----- Update the display when it changes. If (IncludeDateFlag( ) = True) Then CurrentTime.Text = Now.ToLongDateString & vbCrLf & _ Now.ToLongTimeString Else CurrentTime.Text = Now.ToLongTimeString End If End Sub Private Sub ScreenSaver_FormClosing(ByVal sender As Object, _ ByVal e As System.Windows.Forms.FormClosingEventArgs) _ Handles Me.FormClosing ' ----- Restore the mouse pointer. Windows.Forms.Cursor.Show( ) End Sub Private Sub ScreenSaver_KeyDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyEventArgs) _ Handles Me.KeyDown ' ----- Pressing any key stops the program. Me.Close( ) End Sub Private Sub ScreenSaver_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load ' ----- Hide the mouse cursor. Windows.Forms.Cursor.Hide( ) RefreshClock( ) End Sub Private Sub ScreenSaver_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles Me.MouseDown ' ----- Clicking stops the program. Me.Close( ) End Sub Private Sub ScreenSaver_MouseMove(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles Me.MouseMove ' ----- Moving the mouse stops the program. If (LastMousePosition <> New Point(-1, -1)) Then ' ----- See if the mouse moved since last time. If (LastMousePosition <> New Point(e.X, e.Y)) Then Me.Close( ) End If End If ' ----- Record the current point. LastMousePosition = New Point(e.X, e.Y) End Sub Private Sub ScreenSaver_Resize(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Resize ' ----- Center the label on the form. CurrentTime.Location = New Point(0, (Me.Height - _ CurrentTime.Height) / 2) CurrentTime.Size = New Size(Me.Width, CurrentTime.Height) End Sub Add a new module to the project through the Project Add Module menu command, and name the module file Finally, add a form that lets the user indicate whether to include the date on the screensaver display. Add the form through the Project Add Windows Form menu command, and name the form file Set FormBorderStyle to FixedDialog. Set Text to Configure Screen Saver. Set ControlBox to False. Set StartPosition to CenterScreen. Add a CheckBox control to the form named IncludeDate, and set its Text property to Include Date in Screen Saver Display. Also add two Button controls named ActOK and ActCancel, and set their Text properties to OK and Cancel, respectively. Select the form again, and set its AcceptButton property to ActOK and its CancelButton property to ActCancel. The form should look like Figure 14-21. That's it for the main display and code design, but we still need to make a few changes to the project itself to prepare it for screensaver use. Open the Project Properties window. On the Application panel, set "Startup object" to Sub Main, and clear (uncheck) the "Enable application framework" field. Build the project through the Build Build SimpleScreenSaver menu command. In Windows Explorer, locate the executable file. It will appear in the SimpleScreenSaver.exe file to SimpleScreenSaver.scr. Then, copy that file into your system's Windows\System32 directory (the exact location will vary by system). The screensaver is ready to use. Open up the Display Properties within your system's Control panel. On the Screen Saver tab, select SimpleScreenSaver from the Screen Saver drop-down list (Figure 14-22). Figure 14-22. The installed screensaver, ready to useClicking on the Settings button lets you configure the screensaver through the custom Config.vb form. The Preview button runs the screensaver immediately. Screensavers are regular Windows applications, but they reside only in the Windows\System32 directory, and their file extension is .scr instead of .exe. What the user experiences as a screensaver is simply a maximized borderless form. You can add any controls you want to the form, and you can display any graphics or images you require to make the screen saver interesting. Screensaver programs perform three distinct functions: main display, preview display, and configuration. (The sample program does not implement the preview display functionality.) The functionality you present depends on the command-line options supplied to the application:
The recipe's Sub Main routine examines the command-line arguments and takes the appropriate action. In the absence of any command-line arguments, the screensaver should assume the /C argument. This recipe's code implements a very simple screensaver that displays either the time or the combined date and time, updating the display once per second through a Timer control. It determines whether to display the date portion through a setting in the registry, located at: \\HKEY_CURRENT_USER\Software\MyCompany\SimpleScreenSaver\IncludeDate The screensaver runs until it detects a key press (through the Form.KeyPress event), a mouse click (Form.MouseDown), or a mouse movement (Form.MouseMove). It turns out that each form receives a MouseMove message right when the form first opens, whether the mouse is moving or not. Therefore, the code includes some special code to ensure that the first MouseMove event call does not exit the screensaver. |