|
-
Sep 4th, 2006, 11:19 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Wallpaper and screen saver
hi i am new in API and i dont know what is the wrong with this code ??
the wallpaper is working only on bmp file format and i tried jpg and it didn't work.
and to the screen saver it didn't work at all.
this is the code:
VB Code:
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long
Const SPI_SETSCREENSAVETIMEOUT = 15
Const SPI_SETSCREENSAVEACTIVE = 17
Const SPI_SETDESKWALLPAPER = 20
Const SPIF_UPDATEINIFILE = &H1
Sub screen()
SystemParametersInfo SPI_SETSCREENSAVEACTIVE, True, cdl.FileName, SPIF_UPDATEINIFILE
End Sub
Sub wallpaper()
SystemParametersInfo SPI_SETDESKWALLPAPER, 0, cdl.FileName, SPIF_UPDATEINIFILE
End Sub
Private Sub cmdscreen_Click()
cdl.Filter = "screen|*.scr"
cdl.ShowOpen
screen
End Sub
Private Sub cmdwallpaper_Click()
cdl.Filter = "bmp|*.bmp"
cdl.ShowOpen
wallpaper
End Sub
-
Sep 5th, 2006, 01:49 AM
#2
Frenzied Member
Re: Wallpaper and screen saver
I think you can only use bitmap images when setting the desktop image. All the examples on pscode convert the image to a bitmap then make that the desktop. Just load the image into a picture
VB Code:
Dim mypic As IPictureDisp
Set mypic = LoadPicture("d:\in.jpg")
SavePicture mypic, "d:\newfile.bmp"
Set mypic = Nothing
-
Sep 5th, 2006, 04:29 PM
#3
Re: Wallpaper and screen saver
Assuming you're using XP.
I've used all the following code in the past but haven't got a chance to test it before posting, but it should work.
To use a jpg as the wallpaper you have to use the active desktop. This routine should cope with both:
VB Code:
Public Sub Wallpaper_Set(WallPaper As String)
'Setting (WallPaper As String) to "" removes it.
Dim X As Long
'Note how the string passed to the function is preceeded by the ByVal keyword.
'BMPs.
X = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, ByVal WallPaper, SPIF_SENDWININICHANGE Or SPIF_UPDATEINIFILE)
'JPGs.
'If it's the active desktop the above fails, so...
If X = 0 Then
'Use the code in this thread.
[url]http://www.vbforums.com/showthread.php?t=367342[/url] '<--------
ActiveDesktopSetWallpaper
End If
End Sub
As for the screensaver, you have to write to the registry.
VB Code:
Option Explicit
'Form level code.
Private Sub Form_Click()
'Note. You MAY have to use GetShortPathName when retrieving the screensaver file name.
modWriteRegSZ HKEY_LOCAL_MACHINE, "Control Panel\Desktop", "SCRNSAVE.EXE", "C:\WINDOWS\system32\MySaver.scr"
ScreenSaver_Toggle True
End Sub
VB Code:
Option Explicit
'Module level code.
'Toggle the Screensaver on-off
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" _
(ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Long, _
ByVal fuWinIni As Long) As Long
Private Const SPI_SETSCREENSAVEACTIVE = 17
'Registry APIs.
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, _
ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
ByVal samDesired As Long, 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
'Registry manipulation
Public Const HKEY_CURRENT_USER = &H80000001
Private Const REG_OPTION_NON_VOLATILE = 0
Private Const KEY_CREATE_SUB_KEY = &H4
Private Const KEY_SET_VALUE = &H2
Private Const REG_SZ = 1
Private Const ERROR_SUCCESS = 0&
Public Sub modWriteRegSZ(lngHKey As Long, strSubKey As String, strValueName As String, _
strValue As String)
'Create the key and respective REG_SZ value.
Dim lngRetVal As Long
Dim lngKeyHandle As Long
'Create the key.
If RegCreateKeyEx(lngHKey, strSubKey, 0&, 0&, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, _
ByVal 0&, lngKeyHandle, lngRetVal) <> ERROR_SUCCESS Then GoTo WRITE_SZ_ERROR
'Open the new key.
If RegOpenKeyEx(lngHKey, strSubKey, 0&, KEY_SET_VALUE, lngKeyHandle) <> _
ERROR_SUCCESS Then GoTo WRITE_SZ_ERROR
'Create a key and set its value.
If RegSetValueEx(lngKeyHandle, strValueName, 0&, REG_SZ, ByVal strValue, Len(strValue) + 1) _
<> ERROR_SUCCESS Then GoTo WRITE_SZ_ERROR
'Close the key.
Call RegCloseKey(lngKeyHandle)
'Exit.
Exit Sub
WRITE_SZ_ERROR:
'Close the key.
Call RegCloseKey(lngKeyHandle)
'Time for any error handling.
MsgBox "Error in modWriteRegSZ"
End Sub
Public Sub ScreenSaver_Toggle(Active As Boolean)
'To Activate Screen Saver, set active to true.
'To deactivate, set active to false.
Dim lActiveFlag As Long
lActiveFlag = IIf(Active, 1, 0)
Call SystemParametersInfo SPI_SETSCREENSAVEACTIVE, lActiveFlag, 0, 0
End Sub
-
Sep 5th, 2006, 11:07 PM
#4
Thread Starter
Hyperactive Member
Re: Wallpaper and screen saver
hi
thx for both of you .
and for schoolbusdriver
can you explain how the screen saver code works and why you use Registry API Functions
i already said i am new with API so i didn't understand anything
Thx
-
Sep 6th, 2006, 12:39 PM
#5
Re: Wallpaper and screen saver
Before XP the pointer to the screensaver file was stored in WIN.INI (I think!). Under XP it's stored in the registry under
"HKEY_LOCAL_MACHINE\Control Panel\Desktop\SCRNSAVE.EXE".
"Public Sub modWriteRegSZ" is a "stock" procedure I use to write simple strings to anywhere in the registry.
BTW, "SCRNSAVE.EXE" is the name of a registry key, not its value (the screensaver file's name)
"Public Sub ScreenSaver_Toggle" is simply there to ensure that the screensaver is enabled.
Welcome to the wonderful and confusing world of APIs
-
Sep 6th, 2006, 07:36 PM
#6
Thread Starter
Hyperactive Member
Re: Wallpaper and screen saver
thx a lot man
i now understand something .
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
|