[VB6] How Do I Change The Windows WallPaper?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: [VB6] How Do I Change The Windows WallPaper?

Threaded View

  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,091

    Lightbulb [VB6] How Do I Change The Windows WallPaper?

    Q: What APIs Are Needed To Change The WallPaper From VB6?

    A: You will need to add the following APIs and their respective Constants :

    Code:
    'Retrieves or sets the value of one of the system-wide parameters
    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
    
    'Opens the specified registry key
    Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    
    'Sets the data and type of a specified value under a registry key
    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, ByVal lpData As String, ByVal cbData As Long) As Long
    
    'Writes all the attributes of the specified open registry key into the registry
    Private Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    
    'The RegCloseKey function releases the handle of the specified key
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    
    'Creates the specified registry key
    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    
    Private Const HKEY_CURRENT_USER = &H80000001 'The HKEY_CURRENT_USER subtree contains the user profile for the user who is currently logged on to the computer
    
    Private Const SPIF_SENDWININICHANGE = &H2 'Send Change Message
    Private Const SPIF_UPDATEINIFILE = &H1 'Update INI File
    
    
    Private Const SPI_SETDESKWALLPAPER = 20 'Change Wallpaper
    Q: How Do Put These APIs To Work?

    A: Add the following 2 procedures. One Procedure makes the Registry editing easier. We need to change the registry as the wallpaper settings are stored in the registry, under the following key : HKEY_CURRENT_USER\Control Panel\Desktop. Apart from the registry modification, we need to send the physical "Change WallPaper Message". This is done through the SystemParametersInfo API. This API handles all system settings, such as underlining of hot keys, and the Accessibility Options for example.

    Code:
    'Write To The Registry
    'Group = Registry Tree
    'Section = Sub Key In Group Tree
    'Key = Key To Edit
    'NewValue = What To Write
    Private Sub WriteRegistry(ByVal Group As Long, ByVal Section As String, ByVal Key As String, NewValue As String)
    
        Dim lResult As Long 'Result Of Write Operation
        
        Dim lKeyValue As Long 'Current Vlaue
        Dim lnLen As Long
        
        On Error Resume Next
        
        lResult = RegOpenKey(Group, Section, lKeyValue) 'Open The Key
        
        lnLen = Len(NewVal)
        
        'Write The Info
        lResult = RegSetValueEx(lKeyValue, Key, 0&, 1&, NewValue, lnLen)
        lResult = RegFlushKey(lKeyValue)
        
        'Close Key
        lResult = RegCloseKey(lKeyValue)
    
    End Sub
    
    ' Routine to change the windows wallpaper
    ' NewWall = Name and path of required file
    ' WallStyle = Display type flag (  "Center", "Stretch" or "Tile" )
    Private Sub ChangeWall(NewWall As String, WallStyle As String)
    
        Dim lReturn As Long 'Return of SysParInfo API
        
        'Determine WallPaper Style
        If WallStyle <> "Center" And WallStyle <> "Tile" And WallStyle <> "Stretch" Then
            WallStyle = "Stretch"
        End If
        
        'Determine Center
        If WallStyle = "Center" Then
            WriteRegistry HKEY_CURRENT_USER, "Control Panel\Desktop", "TileWallpaper", "0"
            WriteRegistry HKEY_CURRENT_USER, "Control Panel\Desktop", "WallpaperStyle", "0"
            
        'Determine Tile
        ElseIf WallStyle = "Tile" Then
            WriteRegistry HKEY_CURRENT_USER, "Control Panel\Desktop", "TileWallpaper", "1"
            WriteRegistry HKEY_CURRENT_USER, "Control Panel\Desktop", "WallpaperStyle", "0"
            
        'Determine Stretch
        ElseIf WallStyle = "Stretch" Then
            WriteRegistry HKEY_CURRENT_USER, "Control Panel\Desktop", "TileWallpaper", "0"
            WriteRegistry HKEY_CURRENT_USER, "Control Panel\Desktop", "WallpaperStyle", "2"
        End If
        
        'Set the WallPaper
        lReturn = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0&, NewWall, SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)
    
    End Sub
    Q: Is There Anything Else Needed?

    A: Yes, obviously? All that is actually left for us to do is to call the ChangeWall sub with the file we want to set as the wallpaper and the style of the wallpaper. That is it :

    Code:
    Private Sub Command1_Click()
    
        Dim FileName As String 'File Loc Of Picture
        Dim WallStyle As String 'Style Of WallPaper
    
        FileName = "C:\WINDOWS\Gone Fishing.bmp"
        WallStyle = "Tile"
        
        Call ChangeWall(FileName, WallStyle)
        
    End Sub
    A full working sample is attached to this post.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center