using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Timers; using System.IO; using System.Security.Cryptography; namespace BGChanger { class BackgroundChanger { private string folder; private TimeSpan time; private Timer changeTimer; public TimeSpan Time { get { return this.time; } } public string Folder { get { return this.folder; } } public delegate void BackgroundChanged(string background); public event BackgroundChanged OnBackgroudChanged; [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int SystemParametersInfo( int uAction, int uParam, string lpvParam, int fuWinIni); // systemParametersInfo(20, 0, "file", 0x1 | 0x2 ); public BackgroundChanger(string folder, int days, int hours, int minutes, int seconds) { this.folder = folder; this.time = new TimeSpan(days, hours, minutes, seconds); this.changeTimer = new Timer(this.time.TotalMilliseconds); this.changeTimer.Elapsed += new ElapsedEventHandler(setRandomBackground); } public void Start() { this.changeTimer.Start(); } public void Stop() { this.changeTimer.Stop(); } private void setRandomBackground(object sender, ElapsedEventArgs e) { Random rnd = new Random(); // Build list of possible backgrounds List files = new List(); foreach (string str in Directory.GetFiles(this.folder, "*", SearchOption.AllDirectories)) { if (str.EndsWith(".jpg") || str.EndsWith(".jpeg") || str.EndsWith(".png") || str.EndsWith(".bmp")) { files.Add(str); } } // "Randomly" select a background string bg = files[rnd.Next(0, files.Count - 1)]; // Set the background // systemParametersInfo(20, 0, "file", 0x1 | 0x2); SystemParametersInfo(20, 0, bg, 0x1 | 0x2); this.OnBackgroudChanged(bg); } } }