如何编写Windows应用程序来更改桌面壁纸?

时间:2009-05-17 02:15:07

标签: windows desktop desktop-wallpaper

我想编写一个小型Windows应用程序,根据从Web服务检索到的照片更改桌面墙纸?我该怎么办呢?哪种语言/技术是最快的代码?

2 个答案:

答案 0 :(得分:3)

C#和VB here中也有示例代码。除了调用SystemParametersInfo之外,它还为tile和style设置reg键。

答案 1 :(得分:1)

在网上找到这个(vb)代码:

Private Const SPI_SETDESKWALLPAPER As Integer = &H14
Private Const SPIF_UPDATEINIFILE As Integer = &H1
Private Const SPIF_SENDWININICHANGE As Integer = &H2
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

' change this to whatever filename you want to use'
Const WallpaperFile As String = "MovieCollectionImage.bmp"

''' <summary>
''' Sets the background of your Windows desktop. The image will be saved in MyPictures_
 and the background wallpaper updated.
''' </summary>
''' <param name="img">The image to be set as the background.</param>
''' <remarks></remarks>
Friend Sub SetWallpaper(ByVal img As Image)
     Dim imageLocation As String
     imageLocation = My.Computer.FileSystem.CombinePath_
(My.Computer.FileSystem.SpecialDirectories.MyPictures, WallpaperFile)
     Try
          img.Save(imageLocation, System.Drawing.Imaging.ImageFormat.Bmp)
          SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, imageLocation,_
 SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)
     Catch Ex As Exception
          MsgBox("There was an error setting the wallpaper: " & Ex.Message)
     End Try
End Sub

叫做:

SetWallpaper (Me.PictureBox1.Image)