Skip to: Site menu | Main content

Login

Name: 
Password:
Remember me?
Register

Setting the Desktop Wallpaper Through VB

written by Mark Rowlinson - Last updated Oct 2004

The following code provides a function to set desktop wallpaper in windows from VB. To use it simply call SetDesktopPicture() passing it the filename of the Bitmap file containing the image. The return value will be true for sucess and false for failure

e.g.

If SetDesktopPicture("C:pic.bmp") Then 
    MsgBox "Wallpaper set!" 
End If 

NB: The code will only work with bitmaps and not jpegs or other image files.

 
 'define constants required
Public Const SPIF_UPDATEINIFILE = &H1 
Public Const SPI_SETDESKWALLPAPER = 20 
Public Const SPIF_SENDWININICHANGE = &H2 
 
 'declare the API function
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" _ 
(ByVal lngAction As Long, ByVal lngParam As Long, ByVal strPath As String, ByVal lngFlags As Long) As Long 
 
Public Function SetDesktopPicture(ByVal strFilePath As String) As Boolean 
    Dim lngRet As Long 
    lngRet = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0&, pFileName, _ 
    SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE) 
    If lngRet>0 Then 
        SetDesktopPicture=True 
    Else 
        SetDesktopPicture=False 
    End If 
End Sub