2010-09-10 65 views
1

我試圖自動更改桌面牆紙每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; 
     } 
    } 
} 

回答

1

這裏是改變壁紙樣品相同上面的代碼小修改和寫入爲基於Windows窗體的應用程序。 這裏使用Form的'Timer'和'ShowInTaskbar'選項將'False'和'WindowState'設置爲'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; 
     } 
    } 
}