最近在网上爬了一些漂亮小姐姐做壁纸,虽然大多都是喜欢的,难免也有不喜欢的,都过一遍工作量也太大了,我就想有没有方法,在用文件夹幻灯片自动切换壁纸的时候,遇到不喜欢的可以删掉呢?这样用上一段时间,剩下的就都是我的喜欢的了,可是除了学C的时候,后来基本都没有写过exe程序了,也没有做过windows的相关开发,api调用也是完全懵逼的.后来经过一番百度和cv大法,终于用c#实现了.目的勉强达到,还是有点意思的.哈哈😄.
代码
using System;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32;
using System.Timers;
using System.IO;
using Microsoft.VisualBasic.FileIO;
// 编译 csc .\AutoChangeWallpaper.cs /r:Microsoft.VisualBasic.dll
namespace AutoChangeWallpaperApplication
{
class AutoChangeWallpaper
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SystemParametersInfo(uint uAction, uint uParam, StringBuilder lpvParam, uint init);
const uint SPI_GETDESKWALLPAPER = 0x0073;
static String DIR_PATH = "F:/senluoAll";
static Random rd = new Random(Guid.NewGuid().GetHashCode());
static FileInfo[] files = new DirectoryInfo(DIR_PATH).GetFiles();
static String currentPath;
static void Main(string[] args)
{
if (args != null&&args.Length>0) {
DIR_PATH = args[0];
files = new DirectoryInfo(DIR_PATH).GetFiles();
Console.WriteLine("set dir to " + DIR_PATH );
}
System.Timers.Timer timer = new System.Timers.Timer();
timer.Enabled = true;
timer.Interval = 60000; //执行间隔时间,单位为毫秒; 这里实际间隔为1分钟
timer.Start();
timer.Elapsed += new System.Timers.ElapsedEventHandler(changeWallPaper);
while (true)
{
String inputNumber = Console.ReadLine();
if (inputNumber.Equals("d"))
{
// File.Delete(currentPath);
FileSystem.DeleteFile(currentPath,UIOption.OnlyErrorDialogs,RecycleOption.SendToRecycleBin);
files = new DirectoryInfo(DIR_PATH).GetFiles();
realChangeWallPaper();
timer.Stop();
timer.Start();
}
}
}
private static void changeWallPaper(object source, ElapsedEventArgs e)
{
realChangeWallPaper();
}
private static void realChangeWallPaper(){
Wallpaper wallpaper = new Wallpaper();
int i = rd.Next(0, files.Length);
String path = files[i].FullName;
wallpaper.SetWallPaper(path, Wallpaper.Style.Fill);
Console.WriteLine("set WallPaper to " + path + " at: " + DateTime.Now.ToString());
currentPath = path;
}
}
public class Wallpaper
{
const int SPI_SETDESKWALLPAPER = 20;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDWININICHANGE = 0x02;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
public enum Style : int
{
Fill,
Fit,
Span,
Stretch,
Tile,
Center
}
public void SetWallPaper(string wpaper, Style style)
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true))
{
if (style == Style.Fill)
{
key.SetValue(@"WallpaperStyle", 10.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Fit)
{
key.SetValue(@"WallpaperStyle", 6.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Span) // Windows 8 or newer only!
{
key.SetValue(@"WallpaperStyle", 22.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Stretch)
{
key.SetValue(@"WallpaperStyle", 2.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Tile)
{
key.SetValue(@"WallpaperStyle", 0.ToString());
key.SetValue(@"TileWallpaper", 1.ToString());
}
if (style == Style.Center)
{
key.SetValue(@"WallpaperStyle", 0.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
}
SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
wpaper,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}
}
}
编译用csc .\AutoChangeWallpaper.cs /r:Microsoft.VisualBasic.dll
就可以编译出exe执行了.
运行
输入 d 回车就可以删除当前图片了.
很简单的小玩意儿,能拼出来还是有点意外的. 嗯嗯,就是这样,该吃饭了.😄