自动桌面壁纸更改

时间:2010-09-10 18:40:53

标签: c#

我正在尝试自动每隔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;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

以下是更改壁纸的示例相同的上述代码很少修改并为基于Windows窗体的应用程序编写。 这里使用Form-Control和Form的'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;
        }
    }
}

答案 2 :(得分:1)

这可能会有所帮助:http://code.msdn.microsoft.com/windowsdesktop/CSSetDesktopWallpaper-2107409c/sourcecode?fileId=21700&pathId=734742078

详细介绍了如何在Vista之后使用jpgs,以及触摸壁纸样式。但是,看起来您需要使用注册表来更改壁纸样式(中心,平铺,拉伸等)。

相关问题