【问题标题】:How can I set windows 10 desktop background with smooth transition?如何设置平滑过渡的 Windows 10 桌面背景?
【发布时间】:2019-07-11 06:35:51
【问题描述】:

我正在使用以下代码更改我的 windows 桌面背景

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

我的image 目录有很多图片,我将它们一一设置如下

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

桌面背景图片突然变化,但我想要平滑过渡。我该怎么做?

【问题讨论】:

    标签: python python-3.x windows windows-10


    【解决方案1】:

    这是一个我经常使用的纯 Python sn-p:

    它使用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')
    
    

    【讨论】:

    • 对其进行了测试,它运行起来就像一个魅力。非常感谢。
    • 从设置>个性化>背景>选择适合>填充更改壁纸填充样式
    • @Safron 安装 pywin32 库
    • 对我来说,Windows Explorer 在使用这种方法时会逐渐变慢(拖动窗口变得迟钝,alt+tab 变慢等)。重新启动 Windows 资源管理器可以暂时修复它。这段代码是否会加载以前的图像?它可能包含某种内存泄漏吗?
    • 我为提高性能做了两件事。 (1) 只启用一次活动桌面并重复使用“iad”实例。 (2) 在 Windows 设置中禁用“自动从我的背景中选择强调色”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 2022-07-13
    相关资源
    最近更新 更多