VB6: Change the desktop picture and display mode
Introduction
As mentioned in the title these few lines of code can be used to change the desktop image and save the changes in the registry.
Project Initialization
- Open a new project.
- In the form paste the following components ... '
'1 textBox Name = Text1
'1 CommandButton Name = Applique
' caption = Appliquer
'3 x OptionButton Name = Option1
' index = 0 : caption = Centrer
' index = 1 : caption = Mosaique
' index = 2 : caption = Etirer
'You can also add a CommondDialog to find a particular image file.
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 pour la base de registre:
' ---------------------------------------------
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 NomFichier As String
Dim AffiType As Integer
Private Sub Applique_Click()
Dim Txt1 As String, Txt2 As String
Dim R As Long
Dim Hand As Long
' Gestion de l'erreur si pas d'image
On Error Resume Next
NomFichier = Text1.Text
' Mettre les options dans les régistres
Select Case AffiType
Case 0 ' Centrer
Txt1 = "0": Txt2 = "0"
Case 1 ' Mosaïque
Txt1 = "0": Txt2 = "1"
Case 2 ' Etirer
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&, NomFichier, SPIF_UPDATEINIFILE Or _
SPIF_SENDWININICHANGE
End Sub
Private Sub Option1_Click(Index As Integer)
AffiType = Index
End Sub
See also
Knowledge communities.
Original article published by
lermite222. Translated by
jak58.