【问题标题】:how to change desktop wallpaper?如何更换桌面壁纸?
【发布时间】:2009-12-18 17:47:09
【问题描述】:

如何更换桌面壁纸?

我试过了

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

但它没有用。

【问题讨论】:

    标签: delphi api


    【解决方案1】:

    我刚刚在 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.
    

    【讨论】:

      【解决方案2】:

      您可以查看这个 python 脚本: http://gaze.svn.sourceforge.net/viewvc/gaze/trunk/implementation/src/gazelib/os_interface.py?view=markup

      这是发挥所有魔力的 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()
      

      【讨论】:

        【解决方案3】:

        检查一个VB代码here,它可以给你一个线索。

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

        【讨论】:

          【解决方案4】:

          这应该可以工作

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

          【讨论】:

            猜你喜欢
            • 2017-06-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-05-04
            • 1970-01-01
            • 2020-10-16
            相关资源
            最近更新 更多