使用 VB.NET 更改桌面背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2278234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Desktop background using VB.NET
提问by user225269
Is it possible to change desktop background using VB.NET?
是否可以使用 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.
我打算制作一个 VB.NET 程序,只需单击一下,就可以自动使 Windows XP 看起来像 Mac。
回答by ratty
'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)
回答by ratty
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
回答by Herry
you can use Win32
'user32
' for change the desktop background.
you need to declare user32 pi function SystemParametersInfo
like as:
您可以使用Win32
“ user32
”更改桌面背景。您需要SystemParametersInfo
像这样声明 user32 pi 函数:
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
您也可以查看此链接,这是学习如何在 vb.net 中更改桌面背景的好文章
http://www.authorcode.com/how-to-set-desktop-background-in-vb-net/
http://www.authorcode.com/how-to-set-desktop-background-in-vb-net/
回答by user3688161
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