Delphi 如何更换桌面壁纸?

Delphi 如何更换桌面壁纸?,delphi,api,Delphi,Api,如何更改桌面壁纸 我试过这个 procedure TForm1.Button1Click(Sender: TObject); var PicPath: String; begin PicPath := 'C:\test.bmp'; SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, pChar(PicPath), SPIF_SENDCHANGE) end; 但它不起作用 您可以查看此python脚本: 这就是python

如何更改桌面壁纸

我试过这个

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

但它不起作用

您可以查看此python脚本:

这就是python方法,它发挥了所有的魔力。它会更改一些注册表项,然后调用一个系统方法来更新墙纸

  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()
检查一个VB代码,它可以给你一个线索


SystemParametersInfo(SPI_SetdeskWallper,0,imageLocation,SPIF_UpdateInFile或SPIF_SendWinInChange)

我刚刚在XP上用D2007(在Vista上也用D2009)试过,这段代码很有效。
但是要了解它是否工作以及为什么不工作,您应该测试结果代码并从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.
这应该行得通

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