fortypoundhead.com

Start the screensaver from code in VB6

Posted On 2017-05-20 by VB6Boy
Keywords:
Tags: VB6 Miscellaneous Tutorial Scripting Windows
Views: 338


Nothing magical here. Just using the Windows API to start the screensaver on demand from code in Windows.

Let's say you have a, err, questionable, process running a computer. Hey, I don't judge. I like a good game of solitaire at work, too. Let us also pretend that your boss is not so keen on having you playing cards while you are supposed to be working.

You could potentially create a small, one-form app, with just a button on it. Make stay on top of all other windows, and park it in the corner of the screen. When you hear the boss coming, poke the button, and your screen saver instantly activates.

The function and API is shown below. Simply drop this in a module in your app, and you should be good to go.

Private Declare Function GetDesktopWindow Lib _
"user32" () As Long

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

Private Const WM_SYSCOMMAND As Long = &H112&
Private Const SC_SCREENSAVE As Long = &HF140&

Public Function StartScreenSaver() As Boolean

   Dim lDesktopWindow As Long
   Dim lRet As Long

   On Error GoTo ErrorHandler
   lDesktopWindow = GetDesktopWindow()
   lRet = SendMessage(lDesktopWindow, WM_SYSCOMMAND, _
   SC_SCREENSAVE, 0)

   StartScreenSaver = (lRet = 0)

ErrorHandler:

End Function

From your form, you can call it as shown below. I told you it was easy!

ret = StartScreenSaver

Note:

Yes, this works in all versions of Windows, up to and including Windows 10.


About the Author

VB6Boy has posted a total of 22 articles.

 


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.81.195.240

Before you can post, you need to prove you are human. If you log in, this test goes away.




Recent Forum Posts

Advanced search added
dwirch posted on September 23, 2017 at about 13:44 in Site News

Job Spammer: Gaurav Mehta - AgreeYa Solutions
dwirch posted on September 22, 2017 at about 10:35 in Spammers

Job Spammer: Prutha Siri - Javelin Systems
dwirch posted on September 10, 2017 at about 6:15 in Spammers

New security implemented
dwirch posted on September 7, 2017 at about 7:16 in Site News

Malicious IP Checker Companion Tool
dwirch posted on August 12, 2017 at about 20:24 in Site News

Job Spammer: Steve Adams
dwirch posted on August 8, 2017 at about 7:44 in Spammers