更改桌面壁纸定期 [英] Changing Desktop Wallpaper Periodically

查看:116
本文介绍了更改桌面壁纸定期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的代码有问题设置桌面墙纸。该SystemParametersInfo返回true,但它不会改变墙纸。这是一样的,没有任何更改。但我想代码从* .BMP文件的目录定期更改壁纸。请让我知道我犯了一个错误。

 类节目
{
函数[DllImport(user32.dll中)]
公静态的extern BOOL SystemParametersInfo(UInt32的uiAction,UInt32的uiParam,串pvParam,UInt32的fWinIni);
静态的FileInfo []图像;
静态INT currentImage;

静态无效的主要(字串[] args)
{
DirectoryInfo的dirInfo =新的DirectoryInfo(@C:/用户/智能PC /桌面);
图像= dirInfo.GetFiles(* BMP,SearchOption.TopDirectoryOnly);

currentImage = 0;

System.Timers.Timer imageChangeTimer =新定时器(5000);
imageChangeTimer.Elapsed + =新ElapsedEventHandler(imageChangeTimer_Elapsed);
imageChangeTimer.Start();

到Console.ReadLine();
}

静态无效imageChangeTimer_Elapsed(对象发件人,ElapsedEventArgs E)
{
常量UINT SPI_SETDESKWALLPAPER = 30;
const int的SPIF_UPDATEINIFILE = 0×01;
const int的SPIF_SENDWININICHANGE = 0×02;
布尔GK;
GK = SystemParametersInfo(SPI_SETDESKWALLPAPER,0,图像[currentImage ++]全名,SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
Console.Write(GK);
Console.WriteLine(图像[currentImage] .FullName);
currentImage =(currentImage> = images.Length)? 0:currentImage;
}
}


解决方案

我刚刚测试这一点,它为我工作。顺便说一句,这取决于操作系统上,它只能使用位图,你需要转换为位图,如果你尝试任何其他格式。

 使用系统;使用System.Runtime.InteropServices 
;

命名ConsoleApplication1
{
类节目
{
函数[DllImport(user32.dll中,字符集= CharSet.Auto)]
私人静态外部的Int32 SystemParametersInfo(UInt32的uiAction,UInt32的uiParam,字符串pvParam,UInt32的fWinIni);
私有静态UInt32的SPI_SETDESKWALLPAPER = 20;
私有静态UInt32的SPIF_UPDATEINIFILE =为0x1;
私人静态字符串映像文件名称=C:\\test\\test.bmp

静态无效的主要(字串[] args)
{
SetImage(映像文件名称);
Console.ReadKey();
}

私有静态无效SetImage(字符串文件名)
{
SystemParametersInfo(SPI_SETDESKWALLPAPER,0,文件名,SPIF_UPDATEINIFILE);
}
}
}


I have a problem setting the desktop wallpaper using the following code. The SystemParametersInfo returns true but it doesn't change the wallpaper. It is the same as before without any changes. But I want the code to change wallpaper periodically from a directory of *.bmp files. Please let me know where I'm making a mistake.

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;
    }
}

解决方案

I just tested this and it works for me. By the way, depending on the OS, it only works with Bitmaps, you need to convert to bitmap if you were to try any other formats.

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32 uiParam, String         pvParam, UInt32 fWinIni);
        private static UInt32 SPI_SETDESKWALLPAPER = 20;
        private static UInt32 SPIF_UPDATEINIFILE = 0x1;
        private static String imageFileName = "c:\\test\\test.bmp";

        static void Main(string[] args)
        {
            SetImage(imageFileName);
            Console.ReadKey();
        }

        private static void SetImage(string filename)
        {
            SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, filename, SPIF_UPDATEINIFILE);
        }
    }
}

这篇关于更改桌面壁纸定期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆