Introduction
Comme dit dans le titre ces quelques lignes de code permettent de changer l'image du bureau
et de l'inscrire dans le régistre.
Initialisation du projet
Ouvrir un nouveau projet
Dans la forme coller les composants suivants...
'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
'Vous pouvez aussi ajouter un CommondDialog pour rechercher un fichier image
'mais ce n'est pas le propos dans cette astuce.
'Pour le test, mettre le chemin et nom complet d'un fichier image dans text1
Dans le module de la forme
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