我試圖自動更改桌面牆紙每5分鐘(爲了調試目的,它被配置爲5秒)。自動桌面牆紙更換
我發現了一些標準的方法從.net代碼調用SystemParametersInfo()API與它的標準參數。
我做到了。但是我發現它只拾取Bmp文件。我有一大堆我喜歡放在桌面上的JPG。
那麼我發現了一些建議,使用Image.Save()方法將JPG轉換爲Bmp。我不喜歡這個。
什麼是在桌面上設置Jpg的直接方法?我猜User32.dll應該提供一個方法。
這是給你參考代碼:
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 = 0x01;
const int SPIF_SENDWININICHANGE = 0x02;
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, images[currentImage++].FullName, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
currentImage = (currentImage >= images.Length) ? 0 : currentImage;
}
}
}
也很有用:http://www.blackwasp.co.uk/Wallpaper.aspx – prasanthv 2013-05-03 02:27:20