使用 VB.NET 更改桌面背景

use*_*269 4 vb.net

是否可以使用 VB.NET 更改桌面背景?

我也想换图标?

我打算制作一个 VB.NET 程序,只需单击一下,就可以自动使 Windows XP 看起来像 Mac。

rat*_*tty 5

你会做的步骤。启动 Visual Studio 2005 并创建一个新的窗口项目。设置表单的以下属性

'Text = "Set Wallpaper"
'Size = “1024,750”
Run Code Online (Sandbox Code Playgroud)

现在在窗体上滴下一个图片框控件并设置其以下属性。

'Size = “1024,725”
'Sizemode = ”centerimage”
Run Code Online (Sandbox Code Playgroud)

在窗体上放置两个按钮控件并设置其以下属性如下​​。

'First button control.
 
'Name = " btgetimage"
'Text = " Brows For Image"
 
'Second button control.
 
'Name = " btsetwallpaper"
'Text = " Set Wallpaper"
 
Run Code Online (Sandbox Code Playgroud)

现在在表单上放置一个 openfiledialog 控件。打开代码窗口并导入以下命名空间。

Imports System.IO.Directory
Run Code Online (Sandbox Code Playgroud)

现在声明如下函数和变量,它们将使用win API 来设置壁纸。

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"

 
Run Code Online (Sandbox Code Playgroud)

做一个如下的函数。

   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
Run Code Online (Sandbox Code Playgroud)

现在在第一个按钮的单击事件中编写以下代码以打开并获取图像。

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

 
Run Code Online (Sandbox Code Playgroud)

在第二个按钮的点击事件中编写以下代码来设置壁纸。

SetWallpaper(Me.PictureBox1.Image)

MessageBox.Show("Wallpaper has been changed", "Set Wallpaper", MessageBoxButtons.OK, MessageBoxIcon.Information)

 
Run Code Online (Sandbox Code Playgroud)