Search code examples
c#windows-10desktop-applicationdesktop-background

How to programmatically trigger "Next desktop background" in Windows using C#?


I'm developing a customized application for personal use on my system. It's a system controller that runs on startup and appears when I press the F1 and F2 keys simultaneously. I initially built it to trigger specific system functionality without needing to dive into the settings every time.

I’d like to add the ability to cycle through desktop backgrounds. Specifically, I want to mimic the functionality of the "Next desktop background" option in the Windows context menu:

Right click desktop context menu with option "Next desktop background" shown

Is there a way to programmatically access this setting in C#?

I already have experience with PInvoke and similar C++ functionality, but I’m still relatively new to this and might need guidance on implementing it properly. What can I try next?


Solution

  • You can use the IDesktopWallpaper.AdvanceSlideshow method, which is a COM interface from the shlobj header, and should be registered in CLSID on your system.

    Install CsWin32 NuGet package. Alternatively write up the whole interface manually.

    [ComImport]
    [Guid("C2CF3110-460E-4FC1-B9D0-8A1C0C9CC4BD")]
    public class DesktopWallpaper;
    
    var wallpaper = new DesktopWallpaper();
    wallpaper.AdvanceSlideshow(null, 0);