Is it possible to change desktop background using VB.NET?
I'd like to change the icons too?
I plan on making a VB.NET program that can automatically make 开发者_如何学运维Windows XP look like Mac in just one click.
Steps you will do. Start visual studio 2005 and create a new window project. Set the following properties of the form
'Text = "Set Wallpaper"
'Size = “1024,750”
Now drip a picture box control on the form and set its following properties.
'Size = “1024,725”
'Sizemode = ”centerimage”
Drop a two button controls on the form and set its following properties as below.
'First button control.
'Name = " btgetimage"
'Text = " Brows For Image"
'Second button control.
'Name = " btsetwallpaper"
'Text = " Set Wallpaper"
Now drop an openfiledialog control on the form. Open you code window and import the following namespace.
Imports System.IO.Directory
Now declare the function and variables as below which will use win API's to set the wallpaper.
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
Const WallpaperFile As String = "c:\wallpaper.bmp"
Make a function as below.
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
Now in the click event of the first button write the following code to open and get the image.
OpenFileDialog1.InitialDirectory = "c:\"
OpenFileDialog1.Filter = "JPG|*.jpg|Bitmap|*.bmp"
Dim dialogresult As DialogResult = OpenFileDialog1.ShowDialog
If dialogresult = Windows.Forms.DialogResult.OK Then
PictureBox1.ImageLocation = OpenFileDialog1.FileName
btsetwallpaper.Enabled = True
End If
In the click event of the second button write following code to set the wallpaper.
SetWallpaper(Me.PictureBox1.Image)
MessageBox.Show("Wallpaper has been changed", "Set Wallpaper", MessageBoxButtons.OK, MessageBoxIcon.Information)
you can use Win32
'user32
' for change the desktop background.
you need to declare user32 pi function SystemParametersInfo
like as:
Private Declare Function SystemParametersInfo Lib “user32″
Alias “SystemParametersInfoA” (ByVal uAction As Integer, ByVal uParam As Integer,
ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer
and call this function with valid parameters
you can also check this link, this is the good article for learning how to change desktop background in vb.net
http://www.authorcode.com/how-to-set-desktop-background-in-vb-net/
Try this program :
Imports System
Imports System.Runtime.InteropServices
Public Class Desktop
Public Shared SPI_SETDESKTOPWALLPAPER As Integer = 20
Public Shared SPIF_UPDATEINIFILE As Integer = 1
Public Shared SPIF_SENDWININICHANGE As Integer = 2
Public Shared Sub Main(ByVal args() As String)
If (args.Length = 1) Then
SystemParametersInfo(SPI_SETDESKTOPWALLPAPER, 0, args(0), _
(SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE))
End If
End Sub
Private Declare Sub SystemParametersInfo Lib "User32.dll" (ByVal action As Integer, _
ByVal iparam As Integer, ByVal vparam As String, ByVal option As Integer)
End Class
This is a simple and functional code that sets your desktop using only a timer
Public Class Form1
Dim Location As String
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer
Private Const SETDESKWALLPAPER = 20
Private Const UPDATEINIFILE = &H1
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Location = ("image directory.jpg")
PictureBox1.BackgroundImage = Image.FromFile("C:\Users\Danny\Desktop\Hacker.jpg")
PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
SystemParametersInfo(SETDESKWALLPAPER, 0, Location, UPDATEINIFILE)
End Sub End Class
My personal version timered with 1 button, 1 checkbox, 1 timer (10 seconds) and 1 openfiledialog:
Imports System
Imports System.Runtime.InteropServices
Imports System.Threading
Public Class Form1
Private Declare Function SystemParametersInfo Lib “user32” Alias “SystemParametersInfoA” (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer
'Declare two constant
Private Const SETDESKWALLPAPER = 20
Private Const UPDATEINIFILE = &H1
Private Const SENDWININICHANGE As Integer = 2
Dim numfiles As Integer = 0
Dim numtimer As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
OpenFileDialog1.Multiselect = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OpenFileDialog1.Filter = "JPEG Files|*.jpg|BMP Files|*.bmp|PNG Files|*.png|GIF Files|*.gif"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
If CheckBox1.Checked = False Then
SystemParametersInfo(SETDESKWALLPAPER, 0, OpenFileDialog1.FileName.ToString, UPDATEINIFILE Or SENDWININICHANGE)
Else
Dim file As String
For Each file In OpenFileDialog1.FileNames
numfiles += 1
Next file
Timer1.Start()
End If
End If
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = False Then
Timer1.Stop()
numfiles = 0
numtimer = 0
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If numtimer < numfiles Then
SystemParametersInfo(SETDESKWALLPAPER, 0, OpenFileDialog1.FileNames(numtimer).ToString, UPDATEINIFILE Or SENDWININICHANGE)
numtimer += 1
Else
numtimer = 1
SystemParametersInfo(SETDESKWALLPAPER, 0, OpenFileDialog1.FileNames(numtimer).ToString, UPDATEINIFILE Or SENDWININICHANGE)
End If
End Sub
End Class
精彩评论