技术思绪摘录旅行笔记
想法来自于我得到了几百张高清好看的风景图,我想把他们用起来,但是手动一张一张设计成背景,太麻烦了,所以利用当前开发环境,做了这个工具。.NET 4.6+DevExpress 18.1完成,效果还可以。

首先我电脑安装了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);


微信截图_20191205171328.png


WindowsDesktop.zip


CarsonIT 微信扫码关注公众号 策略、创意、技术

留下您的脚步

 

最近评论

查看更多>>

站点统计

总文章数:275 总分类数:18 总评论数:88 总浏览数:124.93万

精选推荐

阅读排行

友情打赏

请打开您的微信,扫一扫