如何以编程方式将桌面背景设置为纯色

How to set the desktop background to a solid color programmatically

如何在 python 中以编程方式将桌面背景设置为纯色?

我想要这个的原因是让我自己成为一个实用程序,它根据我使用的几个虚拟桌面中的哪个更改背景颜色。

要更改windows 桌面墙纸我们通常做的是在桌面上单击鼠标右键并转到属性等。但是我们可以通过从命令行使用 reg 命令编辑注册表项来实现同样的目的。命令如下。

reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d  wallpaper_path /f

例如设置图片E:\photos\image1.bmp作为墙纸我们需要运行命令如下。

reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d E:\photos\image1.bmp /f

编辑注册表项后,我们需要运行以下命令使更改立即生效。

RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters

备注

this method works only for bmp images. If you have .jpg or .jpeg images you can’t set them as wallpaper from command line. You can use the Desktop settings UI to set a .jpg or .jpeg file as wallpaper.

所以现在你可以使用 ossubprocess 到 运行 cmd 然后你可以设置 desktop background

此页面也可能对您有所帮助:How do I set the desktop background on Windows from a script?

更新

后台存储在注册表中。有几种不同的方法来设置它。但是有几种方法可以通过命令行设置注册表值。

要将背景设置为特定颜色,您需要取消设置任何墙纸,然后设置颜色。

HKEY_CURRENT_USER\Control Panel\Colors\Background = 'r g b'
HKEY_CURRENT_USER\Control Panel\Desktop\Wallpaper = ''

正确的做法是用空字符串调用SystemParametersInfo SPI_SETDESKWALLPAPER删除壁纸,然后调用SetSysColors更改桌面颜色。您可能需要 ctypes 在 Python.

中执行此操作

如果要在 log-off 之后保留更改,您还需要将颜色写入注册表。