PDA

Click to See Complete Forum and Search --> : One More Time


barrie
Nov 14th, 1999, 10:19 PM
I realize this question has been bounced around a couple of times, but there has never been a difinitive answer. I am looking for a way to programmatically change the active screen saver without opening the desktop control panel. I am certain there is a way to do it, as I have installed other programs that did it. Also, the desktop control panel does it somehow. Does anyone have any ideas, thoughts, suggestions, etc.?

jim
Nov 15th, 1999, 01:26 AM
Check out the

systemparametersinfo

API call to see how to do this.

Aaron Young
Nov 15th, 1999, 03:19 AM
Unfortunately SystemParametersInfo doesn't return the File Name of the ScreenSaver being used, but, there's a way you can get/set it..

On NT, the Screen Saver location is stored in the registry: HKEY_CURRENT_USER\Control Panel\Desktop under the Value SCRNSAVE.EXE, in Win95 it's stored in the System.ini in the [Boot] Section, so you need to be able to access both:

Here's a Routine I've put together which will locate the ScreenSaver and allow you to change it..

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Private Const HKEY_CURRENT_USER = &H80000001
Private bInReg As Boolean

Private Sub cmdGet_Click()
Dim lRegKey As Long
Dim sData As String
Dim iLen As Long
bInReg = True
If RegOpenKey(HKEY_CURRENT_USER, "Control Panel\Desktop\", lRegKey) = 0 Then
sData = Space(255)
If RegQueryValueEx(lRegKey, "SCRNSAVE.EXE", 0&, 1, ByVal sData, 255) = 0 Then
'Key Found in Registry
If InStr(sData, Chr(0)) Then sData = Left(sData, InStr(sData, Chr(0)) - 1)
End If
Call RegCloseKey(lRegKey)
End If
If Len(Trim(sData)) = 0 Then
bInReg = False
'Try the System.ini
sData = Space(255)
If GetPrivateProfileString("boot", "SCRNSAVE.EXE", "", ByVal sData, 255, "system.ini") Then
sData = Left(sData, InStr(sData, Chr(0)) - 1)
End If
End If
Text1 = sData
End Sub

Private Sub cmdSet_Click()
Dim lRegKey As Long
Dim sData As String
Dim iLen As Long

If bInReg Then
'Save to Registry
If RegOpenKey(HKEY_CURRENT_USER, "Control Panel\Desktop\", lRegKey) = 0 Then
sData = Trim(Text1)
If RegSetValueEx(lRegKey, "SCRNSAVE.EXE", 0&, 1, ByVal sData, Len(sData)) = 0 Then
'Registry Updated
MsgBox "Screen Saver Changed"
End If
Call RegCloseKey(lRegKey)
End If
Else
'Save to the System.ini
sData = Space(255)
'First need to change File Path/Name to 8.3 Format for 16Bit Compatiability
If GetShortPathName(ByVal Text1.Text, ByVal sData, 255) = 0 Then
MsgBox "Not a Valid Path/File, or Path too Long"
Else
If WritePrivateProfileString("boot", "SCRNSAVE.EXE", ByVal sData, "system.ini") Then
MsgBox "Screen Saver Changed"
End If
End If
End If
End Sub

I've not tested it on Win98, but I'm sure it must store the location of the ScreenSaver in one of the 2 Locations this routine checks for.

N.B. To change the ScreenSaver, first Get it, this tells the Routine if it's stored in the Registry or the System.ini

------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net