Coding active
Coding active

Reputation: 1680

Changing Desktop Wallpaper Periodically

I have a problem setting the desktop wallpaper using the following code. The SystemParametersInfo returns true but it doesn't change the wallpaper. It is the same as before without any changes. But I want the code to change wallpaper periodically from a directory of *.bmp files. Please let me know where I'm making a mistake.

class Program
{
    [DllImport("user32.dll")]
    public static extern bool SystemParametersInfo(UInt32 uiAction, UInt32 uiParam, string pvParam, UInt32 fWinIni);
    static FileInfo[] images;
    static int currentImage;

    static void Main(string[] args)
    {
        DirectoryInfo dirInfo = new DirectoryInfo(@"C:/users/Smart-PC/Desktop");
        images = dirInfo.GetFiles("*.bmp", SearchOption.TopDirectoryOnly);

        currentImage = 0;

        System.Timers.Timer imageChangeTimer = new Timer(5000);
        imageChangeTimer.Elapsed += new ElapsedEventHandler(imageChangeTimer_Elapsed);
        imageChangeTimer.Start();

        Console.ReadLine();
    }

    static void imageChangeTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        const uint SPI_SETDESKWALLPAPER = 30;
        const int SPIF_UPDATEINIFILE = 0x01;
        const int SPIF_SENDWININICHANGE = 0x02;
        bool gk;
        gk = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, images[currentImage++].FullName, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
        Console.Write(gk);
        Console.WriteLine(images[currentImage].FullName);
        currentImage = (currentImage >= images.Length) ? 0 : currentImage;
    }
}

Upvotes: 4

Views: 7445

Answers (3)

Ian R. O'Brien
Ian R. O'Brien

Reputation: 6920

I have written a very similar application and I use these values and it works for me:

public void SetWallpaper(String path)
{
    // DLL Import
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int SystemParametersInfo(
        Int32 uAction, 
        Int32 uParam, 
        String lpvParam, 
        Int32 fuWinIni);

    // Consts
    private const Int32 SPI_SETDESKWALLPAPER = 20;
    private const Int32 SPIF_UPDATEINIFILE = 0x01;
    private const Int32 SPIF_SENDWININICHANGE = 0x02;

    // Changing the background.
    SystemParametersInfo(
        SPI_SETDESKWALLPAPER, 
        0, 
        path, // desktopBackground is the bmp location
        SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}

Upvotes: 1

pinkfloydx33
pinkfloydx33

Reputation: 12739

I just tested this and it works for me. By the way, depending on the OS, it only works with Bitmaps, you need to convert to bitmap if you were to try any other formats.

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32 uiParam, String         pvParam, UInt32 fWinIni);
        private static UInt32 SPI_SETDESKWALLPAPER = 20;
        private static UInt32 SPIF_UPDATEINIFILE = 0x1;
        private static String imageFileName = "c:\\test\\test.bmp";

        static void Main(string[] args)
        {
            SetImage(imageFileName);
            Console.ReadKey();
        }

        private static void SetImage(string filename)
        {
            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, filename, SPIF_UPDATEINIFILE);
        }
    }
}

Upvotes: 2

Akram Berkawy
Akram Berkawy

Reputation: 5060

the value of SPI_SETDESKWALLPAPER should be 0x14 and not 30.

also SPIF_UPDATEINIFILE and SPIF_SENDWININICHANGE should be of uint type.

const uint SPI_SETDESKWALLPAPER = 0x14;
const uint SPIF_UPDATEINIFILE = 0x01;
const uint SPIF_SENDWININICHANGE = 0x02;

Upvotes: 1

Related Questions