首先我电脑安装了DevExpress 18.1 你也可以不用这个套件,只是界面不同,功能完全能实现。
选文件和定时执行我就不贴代码了,无论是定时器控件还是任务调度都可以。
原理:把图片转换为bmp格式,然后把路径写入注册表中。
以下是C#设置Win10壁纸的代码
using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Runtime.InteropServices; using System.Threading.Tasks; using Microsoft.Win32; namespace WindowsDesktop.Util { /// <summary> /// Class Wallpaper. This class cannot be inherited. /// </summary> public static class Wallpaper { /// <summary> /// The spi setdeskwallpaper /// </summary> const int SpiSetdeskwallpaper = 20; /// <summary> /// The spif updateinifile /// </summary> const int SpifUpdateinifile = 0x01; /// <summary> /// The spif sendwininichange /// </summary> const int SpifSendwininichange = 0x02; /// <summary> /// Systems the parameters information. /// </summary> /// <param name="uAction">The u action.</param> /// <param name="uParam">The u parameter.</param> /// <param name="lpvParam">The LPV parameter.</param> /// <param name="fuWinIni">The fu win ini.</param> /// <returns>System.Int32.</returns> [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni); /// <summary> /// Enum Style /// </summary> public enum Style : int { Fill, Fit, Span, Stretch, Tile, Center } /// <summary> /// Sets the specified path. /// </summary> /// <param name="path">The path.</param> /// <param name="style">The style.</param> public static bool Set(string path, Style style) { Task.Run(() => { try { string filename = AppConstHelper.GetMd5(path); Image img = Image.FromFile(path); string rootpath = $"{Directory.GetCurrentDirectory()}\\temp\\"; if (!Directory.Exists(rootpath)) { Directory.CreateDirectory(rootpath); } string tempPath = Path.Combine(rootpath, $"{filename}.bmp"); if (!File.Exists(tempPath)) { img.Save(tempPath, ImageFormat.Bmp); } RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true); if (style == Style.Fill) { key.SetValue(@"WallpaperStyle", 10.ToString()); key.SetValue(@"TileWallpaper", 0.ToString()); } else if (style == Style.Fit) { key.SetValue(@"WallpaperStyle", 6.ToString()); key.SetValue(@"TileWallpaper", 0.ToString()); } else if (style == Style.Span) // Windows 8 or newer only! { key.SetValue(@"WallpaperStyle", 22.ToString()); key.SetValue(@"TileWallpaper", 0.ToString()); } else if (style == Style.Stretch) { key.SetValue(@"WallpaperStyle", 2.ToString()); key.SetValue(@"TileWallpaper", 0.ToString()); } else if (style == Style.Tile) { key.SetValue(@"WallpaperStyle", 0.ToString()); key.SetValue(@"TileWallpaper", 1.ToString()); } else if (style == Style.Center) { key.SetValue(@"WallpaperStyle", 0.ToString()); key.SetValue(@"TileWallpaper", 0.ToString()); } SystemParametersInfo(SpiSetdeskwallpaper, 0, tempPath, SpifUpdateinifile | SpifSendwininichange); } catch { } }); return true; } } }
调用
Wallpaper.Set(FullFileName, Wallpaper.Style.Fill);
留下您的脚步
最近评论