简体   繁体   English

自动更换桌面墙纸

[英]Automatic desktop wallpaper change

I am trying to change the desktop wallpaper in every 5 mins automatically (for debugging purpose it's configured to 5 seconds). 我正在尝试每5分钟自动更改一次桌面墙纸(出于调试目的,将其配置为5秒)。

I found some standard method for calling SystemParametersInfo() API from .net code with standard parameters to it. 我发现了一些从.net代码中调用带有标准参数的标准方法来调用SystemParametersInfo()API。

I did them. 我做了 But I found that it picks up only the Bmp files. 但是我发现它只会拾取Bmp文件。 I am having a huge collection of Jpg which I prefer to put on Desktop. 我收藏了大量的Jpg,我更喜欢将其放在桌面上。

Well I found some suggestions to convert the Jpg into Bmp using Image.Save() method. 好吧,我发现了一些使用Image.Save()方法将Jpg转换为Bmp的建议。 I don't prefer this. 我不喜欢这个

What is the direct method to have the Jpg set on the desktop? 在桌面上设置Jpg的直接方法是什么? I guess User32.dll should provide a way to it. 我猜User32.dll应该提供一种方法。

Here is the code for your reference: 这是供您参考的代码:

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

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

It details how jpgs can be used after Vista, and also touches on Wallpaper styles. 它详细介绍了Vista后如何使用jpg,以及触摸墙纸样式。 However, looks like you need to use the registry to change wallpaper style though (center, tile, stretch, etc). 但是,看起来您需要使用注册表来更改墙纸样式(中心,平铺,拉伸等)。

Here is the sample to change Wallpaper the same above code is little modified and written for Windows form based application. 这是更改墙纸的示例,上面的代码几乎没有修改,并且是为基于Windows窗体的应用程序编写的。 Here use a Timer-Control and 'ShowInTaskbar' option of Form to 'False' and 'WindowState' to 'Minimized'. 在这里,将Form的Timer-Control和“ 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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM