简体   繁体   English

如何设置平滑过渡的 Windows 10 桌面背景?

[英]How can I set windows 10 desktop background with smooth transition?

I am changing my windows desktop background with the following code我正在使用以下代码更改我的 Windows 桌面背景

ctypes.windll.user32.SystemParametersInfoW(20, 0, "C:/image/jkk7LGN03aY.jpg" , 0)

my image directory has so many images and I am setting those one by one as follows我的image目录有很多图像,我将这些图像一一设置如下

for path in image_list:
    ctypes.windll.user32.SystemParametersInfoW(20, 0, path , 0)
    time.sleep(5)

Desktop background image is changing abruptly but I want a smooth transition.桌面背景图像突然变化,但我想要平滑过渡。 How can I do this?我怎样才能做到这一点?

Here's a pure Python snippet that I use regularly:这是我经常使用的纯 Python 片段:

It uses pywin32 to enable active desktop and set the wallpaper using a smooth transition (make sure you've not disabled window effects or you won't see any fade effect)它使用pywin32启用活动桌面并使用平滑过渡设置墙纸(确保您没有禁用窗口效果,否则您将看不到任何淡入淡出效果)

import ctypes
from typing import List

import pythoncom
import pywintypes
import win32gui
from win32com.shell import shell, shellcon

user32 = ctypes.windll.user32


def _make_filter(class_name: str, title: str):
    """https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumwindows"""

    def enum_windows(handle: int, h_list: list):
        if not (class_name or title):
            h_list.append(handle)
        if class_name and class_name not in win32gui.GetClassName(handle):
            return True  # continue enumeration
        if title and title not in win32gui.GetWindowText(handle):
            return True  # continue enumeration
        h_list.append(handle)

    return enum_windows


def find_window_handles(parent: int = None, window_class: str = None, title: str = None) -> List[int]:
    cb = _make_filter(window_class, title)
    try:
        handle_list = []
        if parent:
            win32gui.EnumChildWindows(parent, cb, handle_list)
        else:
            win32gui.EnumWindows(cb, handle_list)
        return handle_list
    except pywintypes.error:
        return []


def force_refresh():
    user32.UpdatePerUserSystemParameters(1)


def enable_activedesktop():
    """https://stackoverflow.com/a/16351170"""
    try:
        progman = find_window_handles(window_class='Progman')[0]
        cryptic_params = (0x52c, 0, 0, 0, 500, None)
        user32.SendMessageTimeoutW(progman, *cryptic_params)
    except IndexError as e:
        raise WindowsError('Cannot enable Active Desktop') from e


def set_wallpaper(image_path: str, use_activedesktop: bool = True):
    if use_activedesktop:
        enable_activedesktop()
    pythoncom.CoInitialize()
    iad = pythoncom.CoCreateInstance(shell.CLSID_ActiveDesktop,
                                     None,
                                     pythoncom.CLSCTX_INPROC_SERVER,
                                     shell.IID_IActiveDesktop)
    iad.SetWallpaper(str(image_path), 0)
    iad.ApplyChanges(shellcon.AD_APPLY_ALL)
    force_refresh()


if __name__ == '__main__':
    set_wallpaper(r'D:\Wallpapers\Cool\enchanted_mountain_4k.jpg')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM