191 lines
6.3 KiB
C#
191 lines
6.3 KiB
C#
using System;
|
|
using Microsoft.Win32;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.Runtime.InteropServices;
|
|
using System.IO;
|
|
|
|
namespace Wallpaperchan
|
|
{
|
|
public partial class MainForm : Form
|
|
{
|
|
|
|
int remainingSeconds = 0;
|
|
int currentImageIndex = 0;
|
|
string[] files;
|
|
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
|
|
public static extern int SystemParametersInfo(UAction uAction, int uParam, StringBuilder lpvParam, int fuWinIni);
|
|
|
|
public enum UAction
|
|
{
|
|
/// <summary>
|
|
/// set the desktop background image
|
|
/// </summary>
|
|
SPI_SETDESKWALLPAPER = 0x0014,
|
|
/// <summary>
|
|
/// set the desktop background image
|
|
/// </summary>
|
|
SPI_GETDESKWALLPAPER = 0x0073,
|
|
}
|
|
|
|
public static IEnumerable<FileInfo> GetFilesByExtensions(DirectoryInfo dir, params string[] extensions)
|
|
{
|
|
if (extensions == null)
|
|
throw new ArgumentNullException("extensions");
|
|
IEnumerable<FileInfo> files = dir.EnumerateFiles();
|
|
return files.Where(f => extensions.Contains(f.Extension));
|
|
}
|
|
|
|
public static int SetBackgroud(string fileName)
|
|
{
|
|
int result = 0;
|
|
if (File.Exists(fileName))
|
|
{
|
|
StringBuilder s = new StringBuilder(fileName);
|
|
result = SystemParametersInfo(UAction.SPI_SETDESKWALLPAPER, 0, s, 0x2);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public MainForm()
|
|
{
|
|
InitializeComponent();
|
|
|
|
}
|
|
|
|
private void LoadImagesFromFolder(string folder)
|
|
{
|
|
//this.files = System.IO.Directory.GetFiles(folder, "*.png");
|
|
|
|
DirectoryInfo dInfo = new DirectoryInfo(folder);
|
|
this.files = GetFilesByExtensions(dInfo, ".jpg", ".png", ".jpeg", ".jfif").Select(f => f.FullName).ToArray();
|
|
|
|
ImageList il = imageList1;
|
|
il.Images.Clear();
|
|
for (int i = 0; i < files.Count(); i++)
|
|
{
|
|
string image = files[i];
|
|
this.status.Text = String.Format("Loading image {0}/{1}", i, files.Count());
|
|
this.status.Refresh();
|
|
//Application.DoEvents();
|
|
try
|
|
{
|
|
Image fullImage = Image.FromFile(image);
|
|
Image thumb = fullImage.GetThumbnailImage(112, 63, () => false, IntPtr.Zero);
|
|
fullImage.Dispose();
|
|
il.Images.Add(thumb);
|
|
} catch
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
listView1.LargeImageList = il;
|
|
listView1.Items.Clear();
|
|
for (int i = 0; i < il.Images.Count; i++)
|
|
{
|
|
ListViewItem lvi = new ListViewItem();
|
|
lvi.ImageIndex = i;
|
|
lvi.Text = this.files[i];
|
|
listView1.Items.Add(lvi);
|
|
}
|
|
}
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
string folder = ChooseFolder();
|
|
Properties.Settings.Default.wallpaperFolder = folder;
|
|
Properties.Settings.Default.Save();
|
|
this.LoadImagesFromFolder(folder);
|
|
|
|
}
|
|
|
|
public string ChooseFolder()
|
|
{
|
|
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
|
|
{
|
|
return folderBrowserDialog1.SelectedPath;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
private void button2_Click(object sender, EventArgs e)
|
|
{
|
|
if (this.listView1.FocusedItem == null) { return; }
|
|
int index = this.listView1.FocusedItem.Index;
|
|
SetBackgroud(files[index]);
|
|
this.currentImageIndex = index;
|
|
this.remainingSeconds = (int)this.delay.Value * 60;
|
|
}
|
|
|
|
private void timer1_Tick(object sender, EventArgs e)
|
|
{
|
|
if (this.files == null || files.Count() == 0)
|
|
{
|
|
return;
|
|
}
|
|
this.status.Text = String.Format("Current Image: {0} \nNext wallpaper in: {1} \nLoaded {2} wallpapers", currentImageIndex, remainingSeconds, listView1.Items.Count);
|
|
this.remainingSeconds--;
|
|
if (this.remainingSeconds <= 0)
|
|
{
|
|
this.remainingSeconds = (int)this.delay.Value * 60;
|
|
this.currentImageIndex++;
|
|
|
|
if (this.currentImageIndex > this.listView1.Items.Count -1)
|
|
{
|
|
this.currentImageIndex = 0;
|
|
}
|
|
|
|
}
|
|
SetBackgroud(files[this.currentImageIndex]);
|
|
}
|
|
|
|
private void delay_ValueChanged(object sender, EventArgs e)
|
|
{
|
|
Properties.Settings.Default.delay = (int)this.delay.Value;
|
|
Properties.Settings.Default.Save();
|
|
}
|
|
|
|
private void btnPause_Click(object sender, EventArgs e)
|
|
{
|
|
this.timer1.Enabled = !this.timer1.Enabled;
|
|
this.btnPause.Text = this.timer1.Enabled ? "Pause" : "Continue";
|
|
}
|
|
|
|
private void MainForm_Load(object sender, EventArgs e)
|
|
{
|
|
this.listView1.Items.Clear();
|
|
}
|
|
|
|
private void MainForm_Shown(object sender, EventArgs e)
|
|
{
|
|
this.Refresh();
|
|
this.delay.Value = Properties.Settings.Default.delay;
|
|
this.path.Text = Properties.Settings.Default.wallpaperFolder;
|
|
if (this.path.Text != "")
|
|
{
|
|
this.LoadImagesFromFolder(this.path.Text);
|
|
}
|
|
this.currentImageIndex = Properties.Settings.Default.lastIndex;
|
|
this.remainingSeconds = (int)this.delay.Value * 60;
|
|
}
|
|
|
|
private void button1_Click_1(object sender, EventArgs e)
|
|
{
|
|
if (listView1.View == View.LargeIcon)
|
|
{
|
|
listView1.View = View.List;
|
|
} else
|
|
{
|
|
listView1.View = View.LargeIcon;
|
|
}
|
|
}
|
|
}
|
|
}
|