Ask a question »

VB6: Modifying the image of the desktop

May 2015


VB6: Modifying the image of the desktop + write changes to the registry






As stated here is a short tip, allowing you to change the image of the desktop, including the modifications in the registry.

Project initialization


Open a new project
In the Form paste the following piece of information:

'1 textBox Name = Text1
'1 CommandButton  Name = Apply					
                ' caption = Apply
'3 x OptionButton Name = Option1
'   index = 0 : caption = Center
'   index = 1 : caption = Mosaique
'   index = 2 : caption = Stretch


A CommondDialog can be added, to search for image file. (Note that in this case it is not necessary)

Testing


For testing purposes, put the full path and name of an image file in text1

In the module of the Form

Option Explicit

Private Declare Function SystemParametersInfo Lib "User32" Alias "SystemParametersInfoA" _
(ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As String, ByVal fuWinIni As Long) As Long

 Const SPI_SETDESKWALLPAPER = 20
 Const SPIF_UPDATEINIFILE = &H1
 Const SPIF_SENDWININICHANGE = &H2


' API for Registry:
' ---------------------------------------------
 Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long
 Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (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

 Const HKEY_CURRENT_USER = &H80000001
 Const ERROR_SUCCESS = 0&
 Const REG_SZ = 1
 
 Dim FileName As String
 Dim AffiType As Integer

Private Sub Apply_Click()
Dim Txt1 As String, Txt2 As String
Dim R As Long
Dim Hand As Long
    'Manage error if no image found
    On Error Resume Next

    FileName = Text1.Text

' Put options in the registry
    Select Case AffiType
    Case 0 ' Center
        Txt1 = "0": Txt2 = "0"
    Case 1 ' Mosaïc
        Txt1 = "0": Txt2 = "1"
    Case 2 ' Stretch
        Txt1 = "2": Txt2 = "0"
    End Select
   R = RegCreateKey(HKEY_CURRENT_USER, "Control Panel\Desktop", Hand)
   R = RegSetValueEx(Hand, "WallpaperStyle", 0, REG_SZ, ByVal Txt1, Len(Txt1))
   R = RegCloseKey(Hand)
   
   R = RegCreateKey(HKEY_CURRENT_USER, "Control Panel\Desktop", Hand)
   R = RegSetValueEx(Hand, "TileWallpaper", 0, REG_SZ, ByVal Txt2, Len(Txt2))
   R = RegCloseKey(Hand)
   
    SystemParametersInfo SPI_SETDESKWALLPAPER, 0&, FileName, SPIF_UPDATEINIFILE Or _
    SPIF_SENDWININICHANGE

End Sub

Private Sub Option1_Click(Index As Integer)
    AffiType = Index

End Sub
For unlimited offline reading, you can download this article for free in PDF format:
Vb6-modifying-the-image-of-the-desktop.pdf

See also

In the same category

Published by netty5. - Latest update by jak58
This document entitled « VB6: Modifying the image of the desktop » from Kioskea (en.kioskea.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.