Start the Windows Screen Saver Using a Win32 API

Level:
Level2

In this tutorial we are going to use a Windows API call to start the screen saver. This proves to be a lot easier than you might expect. To start things lets just create a simple project. Add a Command button to Form1. Change the caption of the Command button to "Test".

In Visual Basic 6 if you want to call a Win32 API you must declare it. The easiest way to do this is at the beginning of the Form1 code. We will be using the SendMessage API to tell Windows to start the screen saver. The SendMessage API takes four parameters.

  • hWnd - Handle to the window whose window procedure will receive the message.
  • Msg - Specifies the message to be sent.
  • wParam - Specifies additional message-specific information.
  • lParam - Specifies additional message-specific information.

I know this seems really general, but it will make a lot more sense once you see it used. So lets get to the example.

At the top of the Form1 code lets declare our API function. Also declare a couple of constants that we will need when we call the API function. If you don't understand how all this works don't worry about it. As you read more tutorials and learn more about the Windows API it will make more sense. Here is the code.

Option Explicit
Private Declare Function SendMessage Lib "user32" Alias _
   "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
   ByVal wParam As Long, ByVal lParam As Long) As Long
Const WM_SYSCOMMAND = &H112&
Const SC_SCREENSAVE = &HF140&

This function does all the work. We simply have to call it in the right way and Windows will start the screen saver for us. So lets wire up the Command1 button to do just that.

Private Sub Command1_Click()
   SendMessage Form1.hWnd, WM_SYSCOMMAND, _
      SC_SCREENSAVE, 0&
End Sub

Lets break this apart in case you are wondering what we are doing. We are calling the SendMessage API. The first parameter tells the API who to send the message to. We are sending the message to our current window (Form1). The next parameter says what kind of message we are sending. We are sending a WM_SYSCOMMAND. This stands for Window Message SYStem Command. Which meens instead of our program trying to handle the message it will pass it on to the main Windows operating system. The last two parameters are always information contingent upon what message we are sending. In this case sense we are sending a WM_SYSCOMMAND the first parameter tells Windows what command we want executed. Thus we pass in the constant SC_SCREENSAVE which incase you havn't guessed it stands for System Command Screen Saver. This tells windows to start the screen saver for us. The last command in this case is not needed so we simply pass it a 0.

If you now run this program and click on the button Windows will start the screen saver for you. Note: you must have a default screen saver specified in your system settings. You can check this by going to the windows control panel and selecting the display settings.

Visual Basic Tutorial Screen 1

 

Congratulations by following this Visual Basic tutorial you now have a working VB6 program that starts the default screen saver. If you have any problems or suggestions feel free to leave a comment below.

If you enjoyed this post, subscribe for updates (it's free)

tutorial

very trueeeeeeeee

My suggestion :D

hi to the owner of this site and to all people who also are in this site to learn about visual basic 6. uhmm, well i can say that this is a nice tutorial but i think there's something lacking. you should explain what each line of code means so that it we would be more understandable.what i mean is that if we don't understand the code its just like copying from someone else's work or program right? i hope i don't offend anyone here. i just feel like suggesting to make this tutorial more easy to understand. i hope you add more tutorials to this section. thanks for making this site whoever you are. it helps a lot.more power to you and god bless! :D