2009-12-18 6 views
3

Comment changer de fond d'écran?comment changer le papier peint de bureau?

J'ai essayé

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

Mais cela n'a pas fonctionné.

Répondre

4

Je viens d'essayer avec D2007 sur XP (et D2009 sur Vista), et ce code fonctionne.
Mais pour attraper si et pourquoi il ne fonctionne pas, vous devez tester le code de résultat et obtenir l'erreur à partir de Windows:

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

Dans la plupart des cas, ce sera parce que le fichier bmp est introuvable:

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

Vous pouvez consulter ce script python: http://gaze.svn.sourceforge.net/viewvc/gaze/trunk/implementation/src/gazelib/os_interface.py?view=markup

Ceci est la méthode de python qui fait toute la magie. Il modifie quelques clés de registre et appelle ensuite une méthode système pour mettre à jour le fond d'écran.

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

WTF? ce n'est pas du code Delphi! – Ampere

0

Vérifiez un code VB here, il peut vous donner un indice.

SystemParametersInfo (SPI_SETDESKWALLPAPER, 0, ImageLocation, SPIF_UPDATEINIFILE Ou SPIF_SENDWININICHANGE)

0

Cela devrait fonctionner

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