tags:

views:

142

answers:

4

How do I change desktop wallpaper?

I tried this

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

But it didn't work.

A: 

You can check out this python script: http://gaze.svn.sourceforge.net/viewvc/gaze/trunk/implementation/src/gazelib/os_interface.py?view=markup

This is the python method that does all the magic. It changes a few registry keys and then calls a system method to update the wallpaper.

  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()
ruibm
A: 

Check a VB code here, it can give you a clue.

SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, imageLocation, SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)

Captain Comic
+3  A: 

I just tried it with D2007 on XP (and also D2009 on Vista), and this code works.
But to catch If and why it is not working, you should test the result code and get the error from Windows:

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

In most cases, it will be because the bmp file is not found:

System Error.  Code: 2.
The system cannot find the file specified.
François
A: 

Hi.

This should work

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