操作代码:ChangeDesktop.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

namespace ChangeDesktop
{
    public partial class ChangeDesktop : Form
    {
        //图片路径
        string ImgDir = "";
        //间隔时间
        int Speed = 1000 * 30;
        //图片数组
        string[] ImgPath;
        //当前图片索引
        int ImgIndex = 0;

public ChangeDesktop()
        {
            InitializeComponent();
        }

[DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

private void btnChImgDir_Click(object sender, EventArgs e)
        {
            if (fldBrower.ShowDialog() == DialogResult.OK)
            {
                if (Directory.Exists(fldBrower.SelectedPath))
                {
                    this.ImgDir = fldBrower.SelectedPath;
                    this.lblMsg.Text = "路径: " + this.ImgDir;
                    this.txtChImgDir.Text = this.ImgDir;

//把路径读进数组
                    string[] FileInfos = Directory.GetFiles(this.ImgDir);
                    if (FileInfos.Length > 0)
                    {
                        string ImgPaths = "";
                        string[] FileName;
                        for (int i = 0; i < FileInfos.Length; i++)
                        {
                            FileName = FileInfos[i].Split('.');
                            //MessageBox.Show(FileInfos[i]);
                           
if (FileName[1].ToLower() == "bmp" || FileName[1].ToLower() == "jpeg"
|| FileName[1].ToLower() == "gif" || FileName[1].ToLower() == "jpg")
                            {
                                ImgPaths += FileInfos[i] + ",";
                            }
                        }

ImgPaths = (ImgPaths.Length != 0 ? ImgPaths.Substring(0, ImgPaths.Length - 1) : "");

if (ImgPaths.IndexOf(',') != -1)
                        {
                            ImgPath = ImgPaths.Split(',');
                        }
                        else if (ImgPaths.Length > this.ImgDir.Length)
                        {
                            ImgPath = new string[] { ImgPaths };
                        }
                        else
                        {
                            this.lblMsg.Text = "没有查找到任何可用作桌面背景的文件!";
                        }
                        this.lblMsg.Text += "\n共有文件" + this.ImgPath.Length + "个.";
                    }
                    else
                    {
                        this.lblMsg.Text = "没有查找到任何文件!";
                    }
                }
                else
                {
                    this.lblMsg.Text = "您并未选中任何文件夹!";
                }
            }
        }

private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

private void timer_Tick(object sender, EventArgs e)
        {
            if (this.ImgIndex < this.ImgPath.Length)
            {
                string[] tmp = this.ImgPath[this.ImgIndex].Split('.');
                string imgPath;
                if (tmp[1].ToLower() != "bmp")
                {
                    Bitmap bmp = new Bitmap(this.ImgPath[this.ImgIndex]);
                    imgPath = tmp[0] + ".bmp";
                    bmp.Save(imgPath,System.Drawing.Imaging.ImageFormat.Bmp);
                }
                else
                {
                    imgPath = this.ImgPath[this.ImgIndex];
                }
                if (File.Exists(imgPath))
                {
                    int nResult;
                    nResult = SystemParametersInfo(20, 1, imgPath, 0x1 | 0x2);
                    if (nResult == 0)
                    {
                       
this.lblMsg.Text = "文件路径:\n" + imgPath + "\n设置第" + (this.ImgIndex+1) +
"/" + this.ImgPath.Length + "张图片,没有更新桌面背景成功!";
                    }
                    else
                    {
                       
this.lblMsg.Text = "文件路径:\n" + imgPath + "\n设置第" + (this.ImgIndex+1) +
"/" + this.ImgPath.Length + "张图片,正在更新桌面背景!";   
                    }
                }
                this.ImgIndex++;
            }
            else
            {
                this.ImgIndex = 0;
            }

}

private void btnStart_Click(object sender, EventArgs e)
        {
            if (Regex.IsMatch(this.txtTime.Text, @"\d"))
            {
                this.Speed = 1000 * Convert.ToInt32(this.txtTime.Text);
            }

if (Directory.Exists(this.txtChImgDir.Text) && this.timer.Enabled == false)
            {
                this.txtChImgDir.Enabled = false;
                this.btnChImgDir.Enabled = false;
                this.btnStart.Text = "停止";
                this.txtTime.Enabled = false;

this.timer.Interval = this.Speed;
                this.timer.Enabled = true;
                this.timer.Start();
            }
            else if (this.btnStart.Text == "停止" && this.timer.Enabled == true)
            {
                this.txtChImgDir.Enabled = true;
                this.btnChImgDir.Enabled = true;
                this.btnStart.Text = "开始";
                this.lblMsg.Text = "桌面背景更改已停止";
                this.txtTime.Enabled = true;

this.timer.Enabled = false;
                this.timer.Stop();
            }
            else
            {
                this.lblMsg.Text = "没有选择相关的显示图片!";
            }
        }

private void ChangeDesktop_Load(object sender, EventArgs e)
        {
            this.StartPosition = FormStartPosition.CenterParent;
        }

void ChangeDesktop_SizeChanged(object sender, System.EventArgs e)
        {

if (this.WindowState == FormWindowState.Normal || this.WindowState == FormWindowState.Maximized)
            {
                this.ShowInTaskbar = true;
                this.nfyDesktop.Visible = false;
            }
            else
            {
                this.ShowInTaskbar = false;
                this.nfyDesktop.Visible = true;
            }
        }

private void nfyDesktop_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }

void cmuShow_Click(object sender, System.EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }

void cmuExit_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

private void cmuDisplay_Click(object sender, EventArgs e)
        {
            this.nfyDesktop.Visible = false;
        }
    }
}

Program.cs文件:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ChangeDesktop
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new ChangeDesktop());
        }
    }
}

有系统托盘,可隐藏起来在后台运行,很方便.

就是得要.net 2.0 的环境.

广告交易平台

[C#源码]自动更改桌面背景的更多相关文章

  1. Python 一键拉取Git分支源码自动解析并执行SQL语句

    基于Python实现自动拉取Git分支源码自动解析并执行SQL语句 by:授客 QQ:1033553122 1.代码用途 开发过程中,研发人员会提交SQL更新脚本到Git源码库,然后测试负责去拉取这些 ...

  2. 用Enterprise Architect从源码自动生成类图

    http://blog.csdn.net/zhouyong0/article/details/8281192 /*references:感谢资源分享者.info:简单记录如何通过工具从源码生成类图,便 ...

  3. mysql5.6源码自动安装脚本

    将脚本与源码安装包放在同一目录下,执行脚本即可(执行脚本会使用yum安装依赖包) 安装完成之后,既可以使用mysql -uroot -p登录   脚本内容如下: [root@mysql src]# c ...

  4. GCC源码自动编译-python脚本

    一.前言 目前因机器OS GCC版本太老,导致无法编译一些新版本软件,所以写了一个自动编译GCC的python脚本,操作系统是比较老的suse 10, 很多系统自动软件版本都很低,所以此脚本一般可适用 ...

  5. IDEA call Hierarchy 双击跳转源码后绿色选中背景不消失问题

    1.版本,2019.2.2. 2.这个问题貌似是个bug,就是选中变色后会一直在,目前没有找到对应方法或者配置,如果你找到了,欢迎在评论中分享一下. 3.我这里只能先简单粗暴处理下,通过设置选中时不设 ...

  6. Python3实现Win10桌面背景自动切换

    [本文出自天外归云的博客园] 得空写了个自动切换桌面背景图片的小程序.再不写python就要扔键盘了,对vue还有那么一点好感,天天php真是有够烦. 准备工作 准备个文件夹放在桌面上,平时看到什么高 ...

  7. 源码学习系列之SpringBoot自动配置(篇二)

    源码学习系列之SpringBoot自动配置(篇二)之HttpEncodingAutoConfiguration 源码分析 继上一篇博客源码学习系列之SpringBoot自动配置(篇一)之后,本博客继续 ...

  8. SpringBoot源码学习1——SpringBoot自动装配源码解析+Spring如何处理配置类的

    系列文章目录和关于我 一丶什么是SpringBoot自动装配 SpringBoot通过SPI的机制,在我们程序员引入一些starter之后,扫描外部引用 jar 包中的META-INF/spring. ...

  9. 转--2014年最新810多套android源码2.46GB免费一次性打包下载

    转载自:http://www.eoeandroid.com/thread-497046-1-1.html 感谢该博客主人无私奉献~~ 下面的源码是从今年3月份开始不断整理源码区和其他网站上的安卓例子源 ...

随机推荐

  1. Ubuntu如何开启root账户登录

    1 首先设置root密码,利用现有管理员帐户登陆Ubuntu,在终端执行命令:sudo passwd root,接着输入密码和root密码,重复密码.这样就有了可用的root用户.   2 打开一个终 ...

  2. div拖拽缩放jquery插件编写——带8个控制点

    项目中需要对div进行拖拽缩放,需要有控制面板8个控制点的那种,原以为这么常见的效果应该能搜索到很多相关插件,然而可以完成拖拽的实繁,却找不到我想要的,还是自己动手丰衣足食吧 效果预览(只支持pc端) ...

  3. maven 打包排除配置文件

    如果你想通过pom.xml文件的配置实现的话,你可以这样1.打jar包时过滤配置文件<build><!-- 过滤配置文件 --><resources><res ...

  4. Map-produce算法两个开源实现

    https://github.com/michaelfairley/mincemeatpy https://github.com/denghongcai/mincemeat-node

  5. 为axure生成的html站点添加关闭所有节点的功能

    上一篇随笔:将Axure用于需求分析工具中,我分享了我做了一个axure部件,方便用axure中制作各种uml图. 用axure的朋友可能会发现一个问题,如下图,axure生成的html站点中所有的文 ...

  6. HDU 5855Less Time, More profit

    Less Time, More profit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  7. Python: PS 滤镜--马赛克

    本文利用 Python 实现PS 滤镜中的马赛克效果,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/details/30 ...

  8. POJ1265 Area 多边形内格点数 Pick公式

    POJ1265给定一个多边形 计算边上的格点 内部的格点 以及多边形的面积 利用Pick公式  面积=内部格点数+边上格点数/2-1 将多边形分割为三角形容易证得上述公式 计算面积用叉积,计算边上格点 ...

  9. nginx配置改变默认访问路径

    在安装完nginx服务后,url访问的默认路径是安装的路径html文件夹下的内容,如果需要指定自定义的路径,需要配置nginx.conf文件内容,这样通过url访问就可以了,比如: http://12 ...

  10. 【转载】批量部署系统之kickstart

    一.安装各服务: ftp服务提供软件软件源,tftp提供引导文件,dhcp提供PXE文件位置,syslinux提供PXE文件 [root@node1~]# yum -y installtftp-ser ...