Results 1 to 6 of 6

Thread: [RESOLVED] Wallpaper and screen saver

  1. #1

    Thread Starter
    Hyperactive Member snakeman's Avatar
    Join Date
    Aug 2006
    Posts
    351

    Resolved [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:
    1. 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
    2.  
    3. Const SPI_SETSCREENSAVETIMEOUT = 15
    4. Const SPI_SETSCREENSAVEACTIVE = 17
    5. Const SPI_SETDESKWALLPAPER = 20
    6. Const SPIF_UPDATEINIFILE = &H1
    7.  
    8. Sub screen()
    9. SystemParametersInfo SPI_SETSCREENSAVEACTIVE, True, cdl.FileName, SPIF_UPDATEINIFILE
    10. End Sub
    11.  
    12. Sub wallpaper()
    13. SystemParametersInfo SPI_SETDESKWALLPAPER, 0, cdl.FileName, SPIF_UPDATEINIFILE
    14. End Sub
    15.  
    16. Private Sub cmdscreen_Click()
    17. cdl.Filter = "screen|*.scr"
    18. cdl.ShowOpen
    19. screen
    20. End Sub
    21.  
    22. Private Sub cmdwallpaper_Click()
    23. cdl.Filter = "bmp|*.bmp"
    24. cdl.ShowOpen
    25. wallpaper
    26. End Sub

  2. #2
    Frenzied Member numtel's Avatar
    Join Date
    Apr 2000
    Location
    CA
    Posts
    1,163

    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:
    1. Dim mypic As IPictureDisp
    2. Set mypic = LoadPicture("d:\in.jpg")
    3. SavePicture mypic, "d:\newfile.bmp"
    4. Set mypic = Nothing

  3. #3
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    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:
    1. Public Sub Wallpaper_Set(WallPaper As String)
    2. 'Setting (WallPaper As String) to "" removes it.
    3.    Dim X As Long
    4.  
    5. 'Note how the string passed to the function is preceeded by the ByVal keyword.
    6. 'BMPs.
    7.    X = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, ByVal WallPaper, SPIF_SENDWININICHANGE Or SPIF_UPDATEINIFILE)
    8. 'JPGs.
    9. 'If it's the active desktop the above fails, so...
    10.    If X = 0 Then
    11. 'Use the code in this thread.
    12. [url]http://www.vbforums.com/showthread.php?t=367342[/url] '<--------      
    13.       ActiveDesktopSetWallpaper
    14.    End If
    15. End Sub

    As for the screensaver, you have to write to the registry.
    VB Code:
    1. Option Explicit
    2.  
    3. 'Form level code.
    4.  
    5. Private Sub Form_Click()
    6. 'Note. You MAY have to use GetShortPathName when retrieving the screensaver file name.
    7.    modWriteRegSZ HKEY_LOCAL_MACHINE, "Control Panel\Desktop", "SCRNSAVE.EXE", "C:\WINDOWS\system32\MySaver.scr"
    8.    ScreenSaver_Toggle True
    9. End Sub
    VB Code:
    1. Option Explicit
    2.  
    3. 'Module level code.
    4.  
    5. 'Toggle the Screensaver on-off
    6. Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" _
    7.    (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Long, _
    8.    ByVal fuWinIni As Long) As Long
    9. Private Const SPI_SETSCREENSAVEACTIVE = 17
    10.  
    11. 'Registry APIs.
    12. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    13.  
    14. Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" _
    15.    (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, _
    16.    ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
    17.    lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
    18.  
    19. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    20.    (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
    21.    ByVal samDesired As Long, phkResult As Long) As Long
    22.  
    23. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
    24.    (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
    25.    ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    26.  
    27. 'Registry manipulation
    28. Public Const HKEY_CURRENT_USER = &H80000001
    29. Private Const REG_OPTION_NON_VOLATILE = 0
    30. Private Const KEY_CREATE_SUB_KEY = &H4
    31. Private Const KEY_SET_VALUE = &H2
    32.  
    33. Private Const REG_SZ = 1
    34. Private Const ERROR_SUCCESS = 0&
    35.  
    36. Public Sub modWriteRegSZ(lngHKey As Long, strSubKey As String, strValueName As String, _
    37.    strValue As String)
    38. 'Create the key and respective REG_SZ value.
    39.    Dim lngRetVal     As Long
    40.    Dim lngKeyHandle  As Long
    41.  
    42. 'Create the key.
    43.    If RegCreateKeyEx(lngHKey, strSubKey, 0&, 0&, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, _
    44.       ByVal 0&, lngKeyHandle, lngRetVal) <> ERROR_SUCCESS Then GoTo WRITE_SZ_ERROR
    45. 'Open the new key.
    46.    If RegOpenKeyEx(lngHKey, strSubKey, 0&, KEY_SET_VALUE, lngKeyHandle) <> _
    47.       ERROR_SUCCESS Then GoTo WRITE_SZ_ERROR
    48. 'Create a key and set its value.
    49.    If RegSetValueEx(lngKeyHandle, strValueName, 0&, REG_SZ, ByVal strValue, Len(strValue) + 1) _
    50.       <> ERROR_SUCCESS Then GoTo WRITE_SZ_ERROR
    51. 'Close the key.
    52.    Call RegCloseKey(lngKeyHandle)
    53. 'Exit.
    54.    Exit Sub
    55. WRITE_SZ_ERROR:
    56. 'Close the key.
    57.    Call RegCloseKey(lngKeyHandle)
    58. 'Time for any error handling.
    59.    MsgBox "Error in modWriteRegSZ"
    60. End Sub
    61.  
    62. Public Sub ScreenSaver_Toggle(Active As Boolean)
    63. 'To Activate Screen Saver, set active to true.
    64. 'To deactivate, set active to false.
    65.    Dim lActiveFlag As Long
    66.  
    67.    lActiveFlag = IIf(Active, 1, 0)
    68.    Call SystemParametersInfo SPI_SETSCREENSAVEACTIVE, lActiveFlag, 0, 0
    69. End Sub

  4. #4

    Thread Starter
    Hyperactive Member snakeman's Avatar
    Join Date
    Aug 2006
    Posts
    351

    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

  5. #5
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    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

  6. #6

    Thread Starter
    Hyperactive Member snakeman's Avatar
    Join Date
    Aug 2006
    Posts
    351

    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
  •  



Click Here to Expand Forum to Full Width