C# 自动桌面壁纸更换

C# 自动桌面壁纸更换,c#,C#,我正在尝试每5分钟自动更改一次桌面墙纸(出于调试目的,将其配置为5秒) 我找到了一些从.net代码调用SystemParametersInfo()API的标准方法,其中包含标准参数 我做了。但我发现它只拾取Bmp文件。我有一个巨大的收集Jpg,我喜欢把它放在桌面上 我发现了一些使用Image.Save()方法将Jpg转换成Bmp的建议。我不喜欢这个 在桌面上设置Jpg的直接方法是什么?我想User32.dll应该提供一种方法 以下是代码供您参考: using System; using Syst

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

设置和检索桌面壁纸

以下是更改墙纸的示例。上面的代码几乎没有修改,是为基于Windows窗体的应用程序编写的。 这里使用一个定时器控件和'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;
        }
    }
}
这可能有助于:

它详细介绍了在Vista之后如何使用jpgs,还涉及了壁纸风格。但是,看起来您需要使用注册表来更改墙纸样式(居中、平铺、拉伸等)