在VB6和VB.net中设置桌面墙纸

80酷酷网    80kuku.com

  在VB6中设置墙纸

Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long
Const SPI_SETDESKWALLPAPER = 20
Const SPIF_SENDWININICHANGE = &H2
Const SPIF_UPDATEINIFILE = &H1
Private Sub Command1_Click()
re = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, "c:\Test.BMP", 0)
End Sub


在VB.NET 2003中设置墙纸

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
'设置墙纸
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, "c:\Test.BMP", 0)
End Sub
'取消设置
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, "", 0)
End Sub

在VB.NET 2005中设置墙纸:

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
''' <summary>
''' 设置墙纸
''' </summary>
Private Sub SetWallpaper()
'得到我的图片目录中的文件,在VB2005中,利用My.Computer简单得到我的图片目录
Dim imgLocation As String = My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.MyPictures, "MyImage.bmp")
Try
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, imgLocation Or SPIF_SENDWININICHANGE)
Catch Ex As Exception
MsgBox("设置过程中出现错误: " & Ex.Message)
End Try
End Sub


参考:

SystemParametersInfo函数的定义和参数:

声明如下:
   Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long

  其中各参数的意义如下表:

参数: 意义
uAction Long,指定要设置的参数。参考uAction常数表
uParam Long,参考uAction常数表
lpvParam Any,按引用调用的Integer、Long和数据结构。
fuWinIni 这个参数规定了在设置系统参数的时候,是否应更新用户设置参数
 





 下面是部分uAction参数,和使用它们的方法:

参数 意义和使用方法
6 设置视窗的大小,SystemParametersInfo(6, 放大缩小值, P, 0),lpvParam为long型
17 开关屏保程序,SystemParametersInfo(17, False, P, 1),uParam为布尔型
13,24 改变桌面图标水平和垂直间距,uParam为间距值(像素),lpvParam为long型
15 设置屏保等待时间,SystemParametersInfo(15, 秒数, P, 1),lpvParam为long型
20 设置桌面背景墙纸,SystemParametersInfo(20, True, 图片路径, 1)
93 开关鼠标轨迹,SystemParametersInfo(93, 数值, P, 1),uParam为False则关闭
97 开关Ctrl+Alt+Del窗口,SystemParametersInfo(97, False, A, 0),uParam为布尔型
  









本例中我们选择图片并取得图片的完整路径,然后通过调用API函数,将这幅图设为墙纸,使用的语法为:SystemParametersInfo SPI_SETDESKWALLPAPER, 0, bmpfile, 1

  其中SystemParametersInfo表示要设置桌面墙纸,bmpfile是要设置的图片的路径。

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: