2009-12-18 132 views
3

如何更改桌面牆紙?如何更改桌面壁紙?

我想這

procedure TForm1.Button1Click(Sender: TObject); 
var 
    PicPath: String; 
begin 
    PicPath := 'C:\test.bmp'; 
    SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, pChar(PicPath), SPIF_SENDCHANGE) 
end; 

但沒有奏效。

回答

4

我只是在XP(以及在Vista D2009)D2007試了一下,這個代碼工作。
但趕上如果,爲什麼它不工作,應測試的結果代碼,並從Windows得到錯誤:

if not SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, pChar(PicPath), SPIF_SENDCHANGE)then 
    RaiseLastOSError; 

在大多數情況下,這將是因爲BMP文件未找到:

System Error. Code: 2. 
The system cannot find the file specified. 
0

你可以看看這個python腳本: http://gaze.svn.sourceforge.net/viewvc/gaze/trunk/implementation/src/gazelib/os_interface.py?view=markup

這是做所有的魔法蟒蛇方法。它會更改一些註冊表項,然後調用系統方法更新壁紙。

103 def set_wallpaper(self, file_path) : 
    104  self.__lock.acquire() 
    105  # this module is part of python 2.5 by default 
    106  import ctypes 
    107  import _winreg 
    108  reg = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, self.__REGISTRY_PATH, 0, _winreg.KEY_SET_VALUE) 
    109  # First center the image and turn off tiling 
    110  _winreg.SetValueEx(reg, "TileWallpaper", 0, _winreg.REG_SZ, "0") 
    111  _winreg.SetValueEx(reg, "WallpaperStyle", 0, _winreg.REG_SZ, "0") 
    112  # Set the image 
    113  _winreg.SetValueEx(reg, "ConvertedWallpaper", 0, _winreg.REG_SZ, os.path.realpath(file_path)) 
    114  _winreg.SetValueEx(reg, "Wallpaper", 0, _winreg.REG_SZ, self.convert_to_bmp(file_path)) 
    115  _winreg.CloseKey(reg) 
    116  # Notify the changes to the system 
    117  func_ret_val = ctypes.windll.user32.SystemParametersInfoA(\ 
    118   self.__SPI_SETDESKWALLPAPER,\ 
    119   0,\ 
    120   None,\ 
    121   self.__SPIF_UPDATEINIFILE | self.__SPIF_SENDWININICHANGE) 
    122  assert func_ret_val == 1 
    123  self.__lock.release() 
+0

跆拳道?這不是Delphi代碼! – Ampere 2017-09-13 10:58:09

0

查看VB代碼here,它可以給你一個線索。

SystemParametersInfo(SPI_SETDESKWALLPAPER,0,imageLocation,SPIF_UPDATEINIFILE或者SPIF_SENDWININICHANGE)

0

這應該工作

Procedure TForm1.Button1Click(Sender: TObject); 
var 
    PicPath : string; 
begin 
    PicPath := 'C:\test.bmp'; 
    SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, Pointer(PicPath), SPIF_SENDWININICHANGE); 
end;