SystemParametersInfo

Python的模块pywin32中的win32gui.SystemParametersInfo()函数

在使用win32con.SPI_SETDESKWALLPAPER设置Wallpaper时,其第二个参数为图片路径,图片必须是BMP格式。如下:

win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,  imagepath,  1+2)

否则将报错如下:

pywintypes.error: (0, 'SystemParametersInfo', 'No error message is available')

 

在Python中设置桌面壁纸的方法如下: 

首先需要  import win32api, win32gui, win32api, Image

然后通过以下两个函数实现:

 1 def setWallpaperFromBMP(imagepath):   
 2     k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
 3     win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2") #2拉伸适应桌面,0桌面居中
 4     win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0")
 5     win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2)
 6     
 7     # convert jpg to bmp
 8 def setWallPaper(imagePath):
 9     bmpImage = Image.open(imagePath) 
10     newPath = imagePath.replace('.jpg', '.bmp') 
11     bmpImage.save(newPath, "BMP")
12     setWallpaperFromBMP(newPath)

 

 

posted @ 2013-11-10 17:07  最后的牛仔  阅读(2218)  评论(1编辑  收藏  举报