改桌面背景
来源:互联网 发布:最受欢迎的网络歌手 编辑:程序博客网 时间:2024/04/29 19:17
操作代码: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());
}
}
}
- 改桌面背景
- 改桌面背景图片
- 更换桌面背景
- 设置桌面背景
- 换桌面背景
- 设置smartphone桌面背景图片
- 改变桌面背景图片
- Wallpaper:一张桌面背景
- vc编程更改桌面背景
- linux 桌面背景随心换
- c# 更改桌面背景
- flex更改桌面背景图片
- Android 桌面背景 设置
- 实用批处理--更换桌面背景
- 桌面背景定时换
- C# 替换桌面背景图片
- MFC改变桌面背景
- vc编程更改桌面背景
- poj 2404
- 优化SQL CPU性能
- Oracle LEFT JOIN中ON条件与WHERE条件的区别
- java设置classpath,path,java_home环境变量
- android小游戏 开发-推广-盈利 介绍
- 改桌面背景
- String类的进一步学习二
- SQL2008压缩清除日志方法
- window.location.reload()与window.location.href区别
- Java中的内省
- How to manage a Thread IN ASP.NET
- CRectTracker类的使用 (经典教学!!)
- Unix环境,产生单实例进程方案
- UART控制器(universal asyncronous receiver/transmiter)全称为------通用异步接受/发送控制芯片