杜运庆
在客户运行您的应用程序的时候自动更换墙纸,显然是广告宣传的一个好机会。但无论是MSDN还是网络上的免费资源代码,更换墙纸几乎都是同一个API函数SystemParametersInfo和同一段代码,国外站点提供的免费代码也是如此。从VB4.0开始就这样。这段代码是:
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 Form_Load()
ChangeWP = SystemParametersInfo(SPI_SETDESKWALLPAPER,
0, "Path+BmpFile", 0)
End Sub
使用这个例程确实能够切换墙纸,但存在以下问题:
1、用鼠标右键点击桌面,会看到【背景】→【墙纸】里面的文件名是空的。
2、重新启动后桌面墙纸消失。
3、没告诉您怎么切换"居中"或"平铺"。
怎么解决这些问题呢?
1、Windows的墙纸必须以BMP格式的文件存放在Windows目录下面,如果您的图片不在该目录或不是BMP文件,那么就会出现第一个问题。因此必须把您的图片转换成BMP文件并复制到Windows目录下面。
2、要使重新启动后需要在注册表里面修改相应的键值,具体位置是 HKEY_CURRENT_USER\Control Panel\desktop中的Wallpaper键,让它的键值等于您的全路径文件名。
3、在更换墙纸以前首先设置是否"居中",需要修改注册表中HKEY_CURRENT_USER\Control Panel\desktop中的TileWallpaper键,键值"0"表示"居中","1"表示"平铺"。
本例把一个JPG格式的图片成功地设置为墙纸,全部代码如下:
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
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
Const REG_SZ As Long = 1
Const HKEY_CURRENT_USER = &H80000001
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
Private Sub SetKeyValue(sKeyName As String, sValueName As String, vValueSetting As Variant, lValueType As Long, lPredefinedKey As Long)
lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0,
KEY_ALL_ACCESS, hKey)
lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
RegCloseKey (hKey)
End Sub
Private Function SetValueEx(ByVal hKey As Long, sValueName As String, lType As Long, vValue As Variant) As Long
Dim lValue As Long
Dim sValue As String
Select Case lType
Case REG_SZ
sValue = vValue & Chr$(0)
SetValueEx = RegSetValueExString(hKey, sValueName, 0&, lType,
sValue, Len(sValue))
Case REG_DWORD, REG_BINARY
lValue = vValue
SetValueEx = RegSetValueExLong(hKey, sValueName, 0&,
lType, lValue, 4)
End Select
End Function
Private Sub Form_Load()
'取得windows目录
Dim Path As String, strSave As String
strSave = String(50, Chr$(0))
Path = Left$(strSave, GetWindowsDirectory(strSave, Len(strSave)))
'转换图片并保存到Windows目录下面
Image1.Picture = LoadPicture(App.Path & "\MyFlower.Jpg")
SavePicture Image1, Path & "\MyFlower.bmp"
Dim aa As String
'写入注册表
'设定居中
SetKeyValue "Control Panel\desktop", "TileWallpaper",
"0", REG_SZ, HKEY_CURRENT_USER
'设定平铺
' SetKeyValue "Control Panel\desktop",
"TileWallpaper", "1", REG_SZ, HKEY_CURRENT_USER
'更换墙纸
aa = Path & "\MyFlower.bmp"
ChangeWP = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, aa, 0)
'在注册表中记录图片位置
SetKeyValue "Control Panel\desktop", "Wallpaper", aa,
REG_SZ, HKEY_CURRENT_USER
End Sub
使用本例要注意:必须先设置是否"居中",然后再更换墙纸。本例在Windows95+VB6下调试通过。
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比