Reputation: 49
I found a class for working with desktop and wallpaper, I can change the background image with the code below
public static void SetDesktopWallpaper(string imagePath,
DesktopWallpaperPosition wallpaperPosition)
{
IDesktopWallpaper wallpaper = GetWallpaper();
wallpaper.SetPosition(wallpaperPosition);
wallpaper.SetWallpaper(null, imagePath);
Marshal.ReleaseComObject(wallpaper);
}
But I want to use multiple images as slideshow. But I don't know how to do this. I'm not familiar with the slideshow input SetSlideshow(IntPtr items) and I don't know how to give it a list of image addresses
public class DesktopWallpaper
{
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public enum DesktopSlideshowOptions
{
ShuffleImages = 0x01
}
public enum DesktopSlideshowState
{
Enabled = 0x01,
Slideshow = 0x02,
DisabledByRemoteSession = 0x04
}
public enum DesktopSlideshowDirection
{
Forward = 0,
Backward = 1
}
public enum DesktopWallpaperPosition
{
Center = 0,
Tile = 1,
Stretch = 2,
Fit = 3,
Fill = 4,
Span = 5
}
[ComImport, Guid("B92B56A9-8B55-4E14-9A89-0199BBB6F93B"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDesktopWallpaper
{
void SetWallpaper([MarshalAs(UnmanagedType.LPWStr)] string monitorID, [MarshalAs(UnmanagedType.LPWStr)] string wallpaper);
[return: MarshalAs(UnmanagedType.LPWStr)]
string GetWallpaper([MarshalAs(UnmanagedType.LPWStr)] string monitorID);
[return: MarshalAs(UnmanagedType.LPWStr)]
string GetMonitorDevicePathAt(uint monitorIndex);
[return: MarshalAs(UnmanagedType.U4)]
uint GetMonitorDevicePathCount();
[return: MarshalAs(UnmanagedType.Struct)]
Rect GetMonitorRECT([MarshalAs(UnmanagedType.LPWStr)] string monitorID);
void SetBackgroundColor([MarshalAs(UnmanagedType.U4)] uint color);
[return: MarshalAs(UnmanagedType.U4)]
uint GetBackgroundColor();
void SetPosition([MarshalAs(UnmanagedType.I4)] DesktopWallpaperPosition position);
[return: MarshalAs(UnmanagedType.I4)]
DesktopWallpaperPosition GetPosition();
void SetSlideshow(IntPtr items);
IntPtr GetSlideshow();
void SetSlideshowOptions(DesktopSlideshowDirection options, uint slideshowTick);
[PreserveSig]
uint GetSlideshowOptions(out DesktopSlideshowDirection options, out uint slideshowTick);
void AdvanceSlideshow([MarshalAs(UnmanagedType.LPWStr)] string monitorID, [MarshalAs(UnmanagedType.I4)] DesktopSlideshowDirection direction);
DesktopSlideshowDirection GetStatus();
bool Enable();
}
public static class WallpaperWrapper
{
static readonly Guid CLSID_DesktopWallpaper = new Guid("{C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD}");
public static IDesktopWallpaper GetWallpaper()
{
Type typeDesktopWallpaper = Type.GetTypeFromCLSID(CLSID_DesktopWallpaper);
return (IDesktopWallpaper)Activator.CreateInstance(typeDesktopWallpaper);
}
}
}
Upvotes: 0
Views: 336
Reputation: 25
To use multiple images as a SlideShow, you need to create a list with the Paths for those images, and then convert them to a valid IntPtr object as the Interface requires. Please give the code below a go, and see if you're able to achieve your goals:
(I commented most of the stuff so you know what each part of the code is doing)
public static void Main(string[] args)
{
IDesktopWallpaper wallpaper = (IDesktopWallpaper)new WallpaperManager();
List<WallpaperItem> slideshowItems = new List<WallpaperItem>
{
new WallpaperItem { Path = @"C:\path\to\image1.jpg", Duration = 10 },
new WallpaperItem { Path = @"C:\path\to\image2.jpg", Duration = 5 },
// Add more images with their respective durations.
};
// Create a managed array of structures to pass to the COM method.
var itemsArray = slideshowItems.ToArray();
// Pin the managed array in memory to get a pointer to it.
GCHandle itemsHandle = GCHandle.Alloc(itemsArray, GCHandleType.Pinned);
IntPtr itemsPtr = itemsHandle.AddrOfPinnedObject();
// Call the SetSlideshow method.
wallpaper.SetSlideshow(itemsPtr);
// Release the pinned handle.
itemsHandle.Free();
}
}
Upvotes: 0