随机换桌面壁纸工具开发

---恢复内容开始---

在使用c# 的过程中,忽然想到开发 一个可以随机换桌面壁纸的工具,当然,这样的工具已经有很多了,但是使用别人的尽管很完美但是不如使用自己的有意思。

界面如下:

很简单的功能, 点击上传图片,然后随机选中图片设置为桌面壁纸。
设置桌面壁纸的代码很简单,不过需要外部引用一个 user32.dll

 [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
        public static extern int SystemParametersInfo(
          int uAction,
          int uParam,
          string lpvParam,
          int fuWinIni
          );
        /// <summary>
        /// 設置圖片桌面
        /// </summary>
        /// <param name="randomFileName"></param>
        public static void UpdateWindowsDesk(int randomFileName)
        {          
            var PicName = AppDomain.CurrentDomain.BaseDirectory + @"UpLoadImages"+ randomFileName+".bmp";
          
            SystemParametersInfo(20, 0, PicName, 0x2);
        }

在此前提下,获取所需图片路径设置即可。由于是本地的windowsForm程序,图片文件夹直接就建在Bin文件夹下了。

上传图片:由于设置桌面壁纸图片格式需要使用bmp,所以上传后的图片需要统一改为bmp格式。

代码如下

 /// <summary>
        /// 上傳文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UploadImage_Click(object sender, EventArgs e)
        {
            //初始化openFileDialog
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择要上传的图片";
            ofd.Filter = "图像文件(*.jpg;*.gif;*.png)|*.jpg;*.gif;*.png";
            ofd.Multiselect = false;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string oldFilePath = ofd.FileName;
                int position = oldFilePath.LastIndexOf("\");
                string OldfileName = oldFilePath.Substring(position + 1);
                string[] splitName = oldFilePath.Split('.');
                string ext = splitName[splitName.Length - 1];

                
                var index = 1;
            cc: string newName = index + ".bmp";// + ext;

                //判断根目录下是否含有指定文件夹,若没有则创建一个新的
                string path = AppDomain.CurrentDomain.BaseDirectory + "/UpLoadImages";
                DirectoryInfo di = new DirectoryInfo(path);
                if (!di.Exists)
                {
                    di.Create();
                }
                string newFilePath = AppDomain.CurrentDomain.BaseDirectory + "/UpLoadImages/" + newName;
                if (File.Exists(newFilePath))
                {
                    index = index + 1;
                    goto cc;
                }
                File.Copy(oldFilePath, newFilePath, false);
                Image img = Image.FromFile(newFilePath);
                //tureBox1.Image = img;
                MessageBox.Show("上传成功", "Tip");
            }
        }

在上传图片的时候命名上我取了个巧,使用数字直接命名,使用了goto语法来排除重名问题,

然后只要随机取文件名就可以设置随机壁纸了。

随机取文件名首先要判断当前文件夹下文件的数量,代码如下

 /// <summary>
        /// 讀取文件夾下文件數量
        /// </summary>
        /// <param name="dirInfo"></param>
        /// <returns></returns>
        public static int GetFilesCount(DirectoryInfo dirInfo)
        {

            int totalFile = 0;
            //totalFile += dirInfo.GetFiles().Length;//获取全部文件
            totalFile += dirInfo.GetFiles("*.bmp").Length;//获取某种格式
            foreach (System.IO.DirectoryInfo subdir in dirInfo.GetDirectories())
            {
                totalFile += GetFilesCount(subdir);
            }
            return totalFile;
        }

由于此文件夹下只有一种文件格式,所以直接判断一种格式数量即可,其他文件不影响此类文件。

由此,只要传入随机的文件名就可以设置随机壁纸了。

此项目非常简单,但是对于一个入门型程序员来说也可以算是值得一看的小项目了。

项目链接:https://pan.baidu.com/s/1og9pyLAP5ZExTkVP1eupMw

提取密码:g17l

---恢复内容结束---

原文地址:https://www.cnblogs.com/widows/p/10901039.html