Discussion:
setting the wallpaper style in vb.net
(too old to reply)
Patrick Dugan
2007-04-07 16:55:38 UTC
Permalink
The code below is what i have been *trying* to use to set the desktop
wallpaper picture and the Wallpaper style. The picture gets replaced
with the new image each time, but the style never changes.

Whatever it is set to in the "Display Properties > Desktop" is where is
stays and the code below does not seem to effect it. The registry
entries ARE getting changed but it does not actually apply the new
style. I have tried two different registry entries (The other is
currently remmed out) and I have even tried both at the same time. The
style never gets applied. The return code is always true and the
registry entries do get changed.

What am i missing?


Private Declare Auto Function SystemParametersInfo Lib "user32.dll"
(ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As
String, ByVal fuWinIni As Integer) As Integer

Private Declare Function GetDesktopWindow Lib "user32" () As Int32
Dim intWindowStyle as integer = 2 ' 0 = centered, 1= tiled, 2 =
stretched



Friend Sub SetWallpaper(ByVal Filename As String)

Dim imageLocation As String
Dim SPI_SETDESKWALLPAPER As Integer = &H14
Dim SPIF_UPDATEINIFILE As Integer = &H1
Dim SPIF_SENDWININICHANGE As Integer = &H2

Dim RegKey2 As RegistryKey

'RegKey2 =
Registry.CurrentUser.OpenSubKey("Software\Microsoft\Internet
Explorer\Desktop\General", True)

RegKey2 = Registry.CurrentUser.OpenSubKey("Control
Panel\Desktop", True)
RegKey2.SetValue("Wallpaper", WallpaperFile)
Select Case intWindowStyle
Case 0 ' Centered
RegKey2.SetValue("TileWallpaper", 0)
RegKey2.SetValue("WallpaperStyle", intWindowStyle)
Case 1 ' Tiled
RegKey2.SetValue("TileWallpaper", 1)
RegKey2.SetValue("WallpaperStyle", intWindowStyle)
Case 2 ' Stretched
RegKey2.SetValue("TileWallpaper", 0)
RegKey2.SetValue("WallpaperStyle", intWindowStyle)
End Select

SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, filename,
SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)


End Sub
Scott Thomson
2011-08-17 21:27:03 UTC
Permalink
What you are missing is the Reg Type. Use the overloaded method as follows:

RegKey2.SetValue("TileWallpaper", 1, Microsoft.Win32.RegistryValueKind.String)
RegKey2.SetValue("WallpaperStyle", intWindowStyle, Microsoft.Win32.RegistryValueKind.String)

Windows uses REG_SZ string types. The SetValue() method by default uses DWORD hex types, and while the numbers are updated, they are in the wrong format and are ignored.

Thank you for steering me in the right direction with the registry idea.

Sincerely,
Scott Thomson
VB6 / VB.NET 2010 programmer
Post by Patrick Dugan
The code below is what i have been *trying* to use to set the desktop
wallpaper picture and the Wallpaper style. The picture gets replaced
with the new image each time, but the style never changes.
Whatever it is set to in the "Display Properties > Desktop" is where is
stays and the code below does not seem to effect it. The registry
entries ARE getting changed but it does not actually apply the new
style. I have tried two different registry entries (The other is
currently remmed out) and I have even tried both at the same time. The
style never gets applied. The return code is always true and the
registry entries do get changed.
What am i missing?
Private Declare Auto Function SystemParametersInfo Lib "user32.dll"
(ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As
String, ByVal fuWinIni As Integer) As Integer
Private Declare Function GetDesktopWindow Lib "user32" () As Int32
Dim intWindowStyle as integer = 2 ' 0 = centered, 1= tiled, 2 =
stretched
Friend Sub SetWallpaper(ByVal Filename As String)
Dim imageLocation As String
Dim SPI_SETDESKWALLPAPER As Integer = &H14
Dim SPIF_UPDATEINIFILE As Integer = &H1
Dim SPIF_SENDWININICHANGE As Integer = &H2
Dim RegKey2 As RegistryKey
'RegKey2 =
Registry.CurrentUser.OpenSubKey("Software\Microsoft\Internet
Explorer\Desktop\General", True)
RegKey2 = Registry.CurrentUser.OpenSubKey("Control
Panel\Desktop", True)
RegKey2.SetValue("Wallpaper", WallpaperFile)
Select Case intWindowStyle
Case 0 ' Centered
RegKey2.SetValue("TileWallpaper", 0)
RegKey2.SetValue("WallpaperStyle", intWindowStyle)
Case 1 ' Tiled
RegKey2.SetValue("TileWallpaper", 1)
RegKey2.SetValue("WallpaperStyle", intWindowStyle)
Case 2 ' Stretched
RegKey2.SetValue("TileWallpaper", 0)
RegKey2.SetValue("WallpaperStyle", intWindowStyle)
End Select
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, filename,
SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)
End Sub
Loading...