2010-09-10 63 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; 
     } 
    } 
}