|
-
June 24th, 2009, 02:46 AM
#1
Launching Screen Saver
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:
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
Visual Basic 2005 ver. 8.0.50727.867
-
June 24th, 2009, 04:54 AM
#2
Re: Launching Screen Saver
We meet again 
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 :
Code:
<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 :
Code:
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 :
Code:
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
Have fun!
PS, I'd also recommend you to get the latest api viewer specific to .NET :
APi Viewer 2004
And read this article about the API changes in .NET from VB 6 :
Discovering the API Using Visual Basic 6 and Visual Basic .NET
Last edited by HanneSThEGreaT; June 14th, 2010 at 05:42 AM.
-
June 24th, 2009, 04:55 AM
#3
Re: Launching Screen Saver
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 Doh!!
Have agood day!
Last edited by brjames32; June 24th, 2009 at 04:58 AM.
Visual Basic 2005 ver. 8.0.50727.867
-
June 25th, 2009, 12:55 AM
#4
Re: Launching Screen Saver
That's good news!
Good job!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|