changing background

how do i change the desktop background of the current user on the press of a button?
[84 byte] By [dakota367] at [2007-12-24]
# 1

I believe you may need to do some P/Invoking as there is no direct way of doing it in .NET

take a look at this, but its in C#

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=728979&SiteID=1

Conversion to VB.NET, by sure to import the System.Runtime.Reflection namespace:



<DllImport("user32.dll", SetLastError=true)> private shared function SystemParametersInfo (Byval uAction as Integer, ByVal uParam as Integer, ByVal szImagePath as String, ByVal fuWinIni as Integer) as Integer
End Function

..
..
public function ChangeWallpaper (ByVal path as String) as Boolean
const SPI_SETDESKWALLPAPER as Integer = &h14
const SPIF_UPDATEINIFILE As Integer = &H1 'will make your change permanent in the user profile
const SPIF_SENDCHANGE as Integer = &H2 ' will inform all the windows that something has changed

Dim result as Integer = SystemParametersInfo (SPI_DESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE or SPIF_SENDCHANGE)
return Convert.ToBoolean(result) 'Not sure about this
end function

does this help?

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
Im using VB2005 How do I get this to work, please help.
dakota367 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
I've just given you the code for VB.NET :-)
ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
Put this statement at the top of your source code file:
Imports System.Runtime.Interop
nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
i had that, that was all good it is the actual code its self
dakota367 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6
Yeah, it wasn't kosher. Try this instead:

Imports System.Runtime.InteropServices
Module Module1
Private Declare Auto Function SystemParametersInfo Lib "user32.dll" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal szImagePath As String, ByVal fuWinIni As Integer) As Boolean
Private Const SPI_SETDESKWALLPAPER As Integer = &H14
Private Const SPIF_UPDATEINIFILE As Integer = &H1
Private Const SPIF_SENDCHANGE As Integer = &H2

Public Function ChangeWallpaper(ByVal path As String) As Boolean
Return SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE Or SPIF_SENDCHANGE)
End Function
End Module

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
okay thing you i got that to work but is there any wha to make it so that it can use jpeg and other common formats?
dakota367 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8

jpeg and bitmaps are the supported one's I believe - what happens when you try to give it a jpeg/other file/image formats? the code I posted is the code used for this which NoBugz has kindly corrected/made more accurate.

ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9
It works with bmp files but not jpg when you put a jpg file it just uses the default color as your background.
dakota367 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 10
Yes,

Windows doesn't like to deal with .jpeg files, just .bmp files.

The shell can, it offers the IActiveDesktop::SetWallpaper()

method. Unfortunately, the COM component that implements this

method (shell32.dll) doesn't export the IActiveDesktop interface so you

don't have a declaration available for it to use in your program.

That is what makes dealing with the shell so difficult in .NET, we need

a Primary Interop Assembly for the shell but none is available

(AFAIK). I hope MSFT is paying attention here.

A workaround for you is to convert the .jpeg file to a .bmp file yourself. Here's code to do this:

Imports System.Runtime.InteropServices

Imports System.Drawing

Module Module1

Private Declare Auto Function SystemParametersInfo Lib

"user32.dll" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal

szImagePath As String, ByVal fuWinIni As Integer) As Boolean

Private Const SPI_SETDESKWALLPAPER As Integer = &H14

Private Const SPIF_UPDATEINIFILE As Integer = &H1

Private Const SPIF_SENDCHANGE As Integer = &H2

Public Function ChangeWallpaper(ByVal path As String) As Boolean

path = ConvertBitmap(path)

Return SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path, SPIF_UPDATEINIFILE Or SPIF_SENDCHANGE)

End Function

Private Function ConvertBitmap(ByVal srce As String) As String

' Convert image file <srce> to a bitmap file, stored in My Pictures

Dim sbmp As New Bitmap(srce)

Dim dbmp As New Bitmap(sbmp.Width, sbmp.Height, Imaging.PixelFormat.Format24bppRgb)

Dim gr As Graphics = Graphics.FromImage(dbmp)

gr.DrawImage(sbmp, 0, 0)

Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)

path += "\wallpaper.bmp"

dbmp.Save(path, Imaging.ImageFormat.Bmp)

sbmp.Dispose()

dbmp.Dispose()

Return path

End Function

End Module

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 11
So all of that goes into the same module? And if so do I need to only use it the same way as before?
dakota367 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 12
it goes anywhere you wish to execute the code :-)
ahmedilyas at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...