开发者

Automatic desktop wallpaper change

I am trying to change the desktop wallpaper in every 5 mins automatically (for debugging purpose it's configured to 5 seconds).

I found some standard method for calling SystemParametersInfo() API from .net code with standard parameters to it.

I did them. But I found that it picks up only the Bmp files. I am having a huge collection of Jpg which I prefer to put on Desktop.

Well I found some suggestions to convert the Jpg into Bmp using Image.Save() method. I don't prefer this.

What is the direct method to have the Jpg set on the desktop? I guess User32.dll should provide a way to it.

Here is the code for your reference:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Timers;

namespace ChangeWallpaper
{
    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:\TEMP");
            images = dirInfo.GetFiles("*.jpg", SearchOption.TopDirectoryOnly);

            currentImage = 0;

            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 = 20;
            const int SPIF_UPDATEINIFILE = 0开发者_开发百科x01;
            const int SPIF_SENDWININICHANGE = 0x02;

            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, images[currentImage++].FullName, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);            
            currentImage = (currentImage >= images.Length) ? 0 : currentImage;
        }
    }
}


Setting and Retrieving the Desktop Wallpaper
http://www.geekpedia.com/tutorial209_Setting-and-Retrieving-the-Desktop-Wallpaper.html


Here is the sample to change Wallpaper the same above code is little modified and written for Windows form based application. Here use a Timer-Control and 'ShowInTaskbar' option of Form to 'False' and 'WindowState' to 'Minimized'.

using System;
using System.Collections.Generic; 
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
//using System.Timers;

namespace Screen
{
    public partial class Form1 : Form
   {
        public Form1()
        {
            InitializeComponent();
        }
        [DllImport("user32.dll")]
        public static extern bool SystemParametersInfo(UInt32 uiAction, UInt32 uiParam,     string pvParam, UInt32 fWinIni);
        static FileInfo[] images;
        static int currentImage;

        private void timer1_Tick(object sender, EventArgs e)
        {            
            const uint SPI_SETDESKWALLPAPER = 20;
            const int SPIF_UPDATEINIFILE = 0x01;
            const int SPIF_SENDWININICHANGE = 0x02;
            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0,     images[currentImage++].FullName, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
            currentImage = (currentImage >= images.Length) ? 0 : currentImage;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(@"C:\TEMP");
            images = dirInfo.GetFiles("*.jpg", SearchOption.TopDirectoryOnly);
            currentImage = 0;
        }
    }
}


This might help: http://code.msdn.microsoft.com/windowsdesktop/CSSetDesktopWallpaper-2107409c/sourcecode?fileId=21700&pathId=734742078

It details how jpgs can be used after Vista, and also touches on Wallpaper styles. However, looks like you need to use the registry to change wallpaper style though (center, tile, stretch, etc).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜