Microsoft KB Archive/315725

= HOW TO: Determine If Screen Saver Is Running by Using Visual Basic 6.0 =

Article ID: 315725

Article Last Modified on 10/26/2002

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q315725



IN THIS TASK
SUMMARY
 * Requirements
 * Create an Application That Determines Screen Saver State
 * Verify That It Works
 * Troubleshooting

REFERENCES



SUMMARY
This article demonstrates how to detect if a screen saver is running by using Visual Basic 6.0. In this sample, you use the SystemParametersInfo function and specify the SPI_GETSCREENSAVERRUNNING parameter.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows XP, Microsoft Window Millennium Edition (Me), Microsoft Windows 2000, or Microsoft Windows 98 (see the References section for articles that relate to earlier versions of Windows)

In addition, this article assumes that you have Visual Basic 6.0 programming experience.

back to the top

Create an Application That Determines Screen Saver State
 Start Visual Basic 6.0, and then create a new Standard EXE project. Form1 is created by default. Add a Timer control to Form1, and then set the Interval property of the Timer to 1000. This interval represents one second.  Right-click the form, and then click View Code. Add the following code to the General Declarations section of Form1's module: Option Explicit

Private Declare Function SystemParametersInfo _ Lib &quot;user32&quot; _ Alias &quot;SystemParametersInfoA&quot; _ (ByVal uiAction As Long, _      ByVal uiParam As Long, _       pvParam As Any, _       ByVal fWInIni As Long) As Boolean

Private Const SPI_GETSCREENSAVEACTIVE As Long = &H10 Private Const SPI_GETSCREENSAVERRUNNING As Long = &H72   Define a form-load handler procedure for your form. Add the following code, which displays a caption to indicate whether the screen saver is active: Private Sub Form_Load Dim bActive As Boolean ' Find out if screen saver is active, and display a suitable message. SystemParametersInfo SPI_GETSCREENSAVEACTIVE, 0, bActive, False If bActive Then Me.Caption = &quot;Screen saver is active&quot; Else Me.Caption = &quot;Screen saver is not active&quot; End If End Sub   Define a timer-event handler procedure for the timer. This event occurs once every second. Add the following code to this procedure, which produces a sound if the screen saver is running: Private Sub Timer1_Timer Dim bRunning As Boolean

' Beep if the screen saver is running. SystemParametersInfo SPI_GETSCREENSAVERRUNNING, 0, bRunning, False If bRunning Then Beep End If End Sub 

back to the top

Verify That It Works
 If a screen saver is not enabled on the computer, follow these steps to enable a screen saver:  Right-click in the desktop, and then click Properties.</li> In the Display Properties dialog box, on the Screen Saver tab, select a screen saver. Wait for approximately one minute, and then click OK.</li></ol> </li> Build and run your Visual Basic application. The form caption in your application displays the following message:

Screen saver is active

NOTE: If the caption displays the &quot;Screen saver is not active&quot; message, the screen saver is not enabled.</li> Wait for the screen saver to begin running. You hear short beeps at one-second intervals until the screen saver stops.</li> Close your Visual Basic application.</li> Reset the screen saver to your settings as appropriate.</li></ol>

back to the top

Troubleshooting

 * This application only calls SystemParametersInfo with the SPI_GETSCREENSAVEACTIVE parameter when the application starts to run. If the user changes the screen saver settings after this time, this information is not updated in the Visual Basic application.
 * Earlier versions of Windows, such as Microsoft Windows NT 4.0 and Microsoft Windows 95, do not support the SPI_GETSCREENSAVERRUNNING parameter to SystemParametersInfo. If you want to determine if a screen saver is running on these earlier versions of Windows, refer to the References section.

back to the top

<div class="references_section">