64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
import ctypes
|
|
import os
|
|
import logging
|
|
from ctypes import wintypes
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
SPI_SETDESKWALLPAPER = 0x0014
|
|
SPIF_UPDATEINIFILE = 0x01
|
|
SPIF_SENDWININICHANGE = 0x02
|
|
|
|
FILL_STYLES = {
|
|
"fill": {"WallpaperStyle": "10", "TileWallpaper": "0"},
|
|
"fit": {"WallpaperStyle": "6", "TileWallpaper": "0"},
|
|
"stretch": {"WallpaperStyle": "2", "TileWallpaper": "0"},
|
|
"tile": {"WallpaperStyle": "0", "TileWallpaper": "1"},
|
|
"center": {"WallpaperStyle": "0", "TileWallpaper": "0"},
|
|
}
|
|
|
|
|
|
def set_fill_style(style: str) -> None:
|
|
import winreg
|
|
|
|
values = FILL_STYLES.get(style)
|
|
if not values:
|
|
values = FILL_STYLES["fill"]
|
|
|
|
try:
|
|
key = winreg.OpenKey(
|
|
winreg.HKEY_CURRENT_USER,
|
|
r"Control Panel\Desktop",
|
|
0,
|
|
winreg.KEY_SET_VALUE,
|
|
)
|
|
winreg.SetValueEx(key, "WallpaperStyle", 0, winreg.REG_SZ, values["WallpaperStyle"])
|
|
winreg.SetValueEx(key, "TileWallpaper", 0, winreg.REG_SZ, values["TileWallpaper"])
|
|
winreg.CloseKey(key)
|
|
logger.info("Fill style set to: %s", style)
|
|
except Exception as e:
|
|
logger.error("Failed to set fill style: %s", e)
|
|
|
|
|
|
def set_wallpaper(image_path: str, fill_style: str = "fill") -> bool:
|
|
if not os.path.exists(image_path):
|
|
logger.error("Image not found: %s", image_path)
|
|
return False
|
|
|
|
set_fill_style(fill_style)
|
|
|
|
abs_path = os.path.abspath(image_path)
|
|
result = ctypes.windll.user32.SystemParametersInfoW(
|
|
SPI_SETDESKWALLPAPER,
|
|
0,
|
|
abs_path,
|
|
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE,
|
|
)
|
|
|
|
if result:
|
|
logger.info("Wallpaper set: %s", abs_path)
|
|
else:
|
|
logger.error("Failed to set wallpaper: %s", abs_path)
|
|
|
|
return bool(result)
|