代码之家  ›  专栏  ›  技术社区  ›  SaravananArumugam

自动更换桌面壁纸

c#
  •  1
  • SaravananArumugam  · 技术社区  · 14 年前

    我试图改变每5分钟自动桌面壁纸(为调试目的,它配置为5秒)。

    我找到了一些从.net代码调用SystemParametersInfo()API的标准方法,其中包含标准参数。

    我做到了。但我发现它只提取Bmp文件。我有一个巨大的收集Jpg,我喜欢放在桌面上。

    我找到了一些建议,可以使用图像。保存()方法。我不喜欢这个。

    在桌面上设置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 回复  |  直到 14 年前
        2
  •  1
  •   Raaghav    13 年前

    这里使用一个定时器控件和'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;
            }
        }
    }
    
        3
  •  1
  •   prasanthv goat    11 年前

    http://code.msdn.microsoft.com/windowsdesktop/CSSetDesktopWallpaper-2107409c/sourcecode?fileId=21700&pathId=734742078

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