Click to See Complete Forum and Search --> : Launching Screen Saver


brjames32
June 24th, 2009, 03:46 AM
Hi people

I am trying to allow the user to launch the Windows Screen Saver by clicking on a button. I have the following code:
Option Explicit On


Public Class Form1
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&

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

SendMessage(Me.hwnd, WM_SYSCOMMAND, SC_SCREENSAVE, 0&)
End Sub
End Class

But get the error:

Error 1 'hwnd' is not a member of 'LaunchScreensaverTest.Form1'.

any ideas why?

Thanks ;)

HanneSThEGreaT
June 24th, 2009, 05:54 AM
We meet again :D

Your SendMessage API implementation as you have it currently, is applicable only to VB 6. hwnd, is also still VB6.

In .NET the SendMessage API looks a bit different, and there is no hwnd property in .NET. .NET makes use of Handle, and to make it compatible with the new SendMessage API it has to be converted to Int32.

This is new SendMessage for .NET :
<DllImport("user32.dll")> _
Private Shared Function SendMessage(ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
End Function 'API used to start the default screensaver

The System.Runtime.InterOpServices namespace should also be imported.

With this implementation, you can start the screensaver like :
Dim ScreenResult As Int32 'Returned Result For SendMessage API

ScreenResult = SendMessage(Me.Handle.ToInt32, WM_SYSCOMMAND, SC_SCREENSAVE, 0) 'Start Current System Screen Saver

For example.

Your code should now look like this :
Imports System.Runtime.InteropServices 'For APIs

Public Class Form1

<DllImport("user32.dll")> _
Private Shared Function SendMessage(ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
End Function 'API used to start the default screensaver

Private Const WM_SYSCOMMAND As Integer = &H112 'To send a "Windows" message
Private Const SC_SCREENSAVE As Integer = &HF140 'Constant for the Screensaver


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ScreenResult As Int32 'Returned Result For SendMessage API

ScreenResult = SendMessage(Me.Handle.ToInt32, WM_SYSCOMMAND, SC_SCREENSAVE, 0) 'Start Current System Screen Saver

End Sub
End Class

Voila!

I'm also attaching one of my projects here, that lists all screensavers, checks for system themes, wallpapers etc.

This is part of this article I wrote :
Customising Your Desktop with Visual Basic.NET 2005 (http://www.codeguru.com/article.php/c14319)

Have fun!

PS, I'd also recommend you to get the latest api viewer specific to .NET :
APi Viewer 2004 (http://www.activevb.de/rubriken/apiviewer/index-apiviewereng.html)

And read this article about the API changes in .NET from VB 6 :
Discovering the API Using Visual Basic 6 and Visual Basic .NET
(http://www.codeguru.com/vb/gen/vb_general/ideincludingvisualstudionet/article.php/c11981/)

brjames32
June 24th, 2009, 05:55 AM
Hi Hannes

Thanks for the great info as usual ;) It works a treat!

It was a mistake on my part. I took the code form an old VB6 prog I wrote years ago and forgot I couldn't just copy and paste :blush: Doh!!

Have agood day!

HanneSThEGreaT
June 25th, 2009, 01:55 AM
That's good news!

Good job! :thumb: