使用以下代码设置桌面墙纸时遇到问题。 SystemParametersInfo返回true,但不会更改壁纸。与以前一样,没有任何改变。但我希望代码定期从* .bmp文件目录中更换壁纸。请让我知道我犯了什么错误。定期更换桌面壁纸
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;
}
}
不相关的当前的问题,但你的下一个问题是:你会得到'IndexOutOfRangeException'就行'Console.WriteLine(图像[currentImage] .FullName);'。 – 2013-02-24 11:44:15
是的,我明白了这一点,但我并不太在意这一点。我添加了该陈述仅供参考。 – 2013-02-24 12:27:57
是否有任何异常被抛出? – 2013-02-24 14:43:26