Skip to content

Instantly share code, notes, and snippets.

@abdusco
Last active May 29, 2021 01:03
Show Gist options
  • Save abdusco/b4cf0780e88cbe4f3aac5ebf210170b0 to your computer and use it in GitHub Desktop.
Save abdusco/b4cf0780e88cbe4f3aac5ebf210170b0 to your computer and use it in GitHub Desktop.
Enable ActiveDesktop and set wallpaper with fade effect on Windows (tested on W10)
import ctypes
from win32com.shell import shell, shellcon
import pythoncom
def enable_active_desktop():
"""
Taken from:
https://stackoverflow.com/a/16351170
In case of a link rot:
> You have to tell windows that you want to enable ActiveDesktop. I tell it every time right before setting the wallpaper through ActiveDesktop.
> public static void EnableActiveDesktop()
> {
> IntPtr result = IntPtr.Zero;
> WinAPI.SendMessageTimeout(WinAPI.FindWindow("Progman", null), 0x52c, IntPtr.Zero, IntPtr.Zero, 0, 500, out result);
> }
"""
progman = ctypes.windll.User32.FindWindowW('Progman', 0)
cryptic_params = (0x52c, 0, 0, 0, 500, None)
ctypes.windll.User32.SendMessageTimeoutW(progman, *cryptic_params)
def change_wallpaper(abs_path_to_image: str):
iad = pythoncom.CoCreateInstance(shell.CLSID_ActiveDesktop,
None,
pythoncom.CLSCTX_INPROC_SERVER,
shell.IID_IActiveDesktop)
iad.SetWallpaper(abs_path_to_image, 0)
opts = (shellcon.AD_APPLY_ALL
# | shellcon.AD_APPLY_FORCE
# | shellcon.AD_APPLY_BUFFERED_REFRESH
# | shellcon.AD_APPLY_DYNAMICREFRESH
)
iad.ApplyChanges(opts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment