Just nu i M3-nätverket
Gå till innehåll

Ändra wallpaper


Miky

Rekommendera Poster

Hej

 

Hade tänkt att göra ett program som ändrar bakgrunden på skrivbordet. Men jag hittar inte någonstans om hur man ändrar. Någon som kan hjälpa mig?

 

Använder Visual Studio 2008

OS: Vista (även om jag skulle vilja kunna ändra på andra OS också, om det nu skulle vara vissa skillnader i kod)

 

/Miky

Länk till kommentar
Dela på andra webbplatser

Jag löste det själv...

 

Jag klistrar in hela classen som jag har skrivit... :)

 

Imports System.IO
Public Class wallpaperManager
   Public Enum style
       Stretched
       Centered
       Tiled
   End Enum

   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
   Private Const SPI_SETDESKWALLPAPER = 20
   Private _style As style
   Private _filename As String

   Public Sub New()
       MyBase.New()
       _style = style.Stretched
   End Sub
   Property selectedStyle() As style
       Get
           Return _style
       End Get
       Set(ByVal value As style)
           _style = value
       End Set
   End Property
   Property filename() As String
       Get
           Return _filename
       End Get
       Set(ByVal value As String)
           If Not File.Exists(value) Then Throw New Exception("File does not exist!")
           _filename = value
       End Set
   End Property

   Public Sub changeWallpaper()
       Dim key = My.Computer.Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)
       Dim lngSuccess As Long
       Dim strBitmapImage As String

       If Not File.Exists(_filename) Then Throw New Exception("File does not exist!")

       Try
           Select Case _style
               Case style.Stretched
                   key.SetValue("WallpaperStyle", "2")
                   key.SetValue("TileWallpaper", "0")
               Case style.Centered
                   key.SetValue("WallpaperStyle", "1")
                   key.SetValue("TileWallpaper", "0")
               Case style.Tiled
                   key.SetValue("WallpaperStyle", "1")
                   key.SetValue("TileWallpaper", "1")
           End Select

           strBitmapImage = _filename
           lngSuccess = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, strBitmapImage, 0)
       Catch ex As Exception
           Throw New Exception("Could not change wallpaper!")
       End Try

   End Sub
End Class

Länk till kommentar
Dela på andra webbplatser

Arkiverat

Det här ämnet är nu arkiverat och är stängt för ytterligare svar.

×
×
  • Skapa nytt...