这几天用winform做了一个设置壁纸的小工具, 为图片添加当月的日历并设为壁纸,可以手动设置壁纸,也可以定时设置壁纸,最主要的特点是在图片上生成当前月的日历信息。

工具和桌面设置壁纸后的效果如下:

在图片上画日历的类代码Calendar.cs如下:

using System;

using System.Collections.Generic;

using System.Text;

using System.Drawing;

using System.IO;

using System.Drawing.Imaging;

namespace SetWallpaper

{

public class Calendar

{

///

/// 计算星期几: 星期日至星期六的值为0-6

///

public static int GetWeeksOfDate(int year, int month, int day)

{

DateTime dt = new DateTime(year, month, day);

DayOfWeek d = dt.DayOfWeek;

return Convert.ToInt32(d);

}

///

/// 获取指定年月的天数

///

public static int GetDaysOfMonth(int year, int month)

{

DateTime dtCur = new DateTime(year, month, 1);

int days = dtCur.AddMonths(1).AddDays(-1).Day;

return days;

}

///

/// 获取在图片上生成日历的图片

///

public static Bitmap GetCalendarPic(Image img)

{

Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb);

bmp.SetResolution(72, 72);

using (Graphics g = Graphics.FromImage(bmp))

{

g.DrawImage(img, 0, 0, img.Width, img.Height);

DateTime dtNow = DateTime.Now;

int year = dtNow.Year;

int month = dtNow.Month;

int day = dtNow.Day;

int day1st = Calendar.GetWeeksOfDate(year, month, 1); //第一天星期几

int days = Calendar.GetDaysOfMonth(year, month); //获取想要输出月份的天数

int startX = img.Width / 2; //开始的X轴位置

int startY = img.Height / 4; //开始的Y轴位置

int posLen = 50; //每次移动的位置长度

int x = startX + day1st * posLen; //1号的开始X轴位置

int y = startY + posLen * 2;//1号的开始Y轴位置

Calendar.DrawStr(g, dtNow.ToString("yyyy年MM月dd日"), startX, startY);

string[] weeks = { "日", "一", "二", "三", "四", "五", "六" };

for (int i = 0; i < weeks.Length; i++)

Calendar.DrawStr(g, weeks[i], startX + posLen * i, startY + posLen);

for (int j = 1; j <= days; j++)

{

if (j == day)//如果是今天,设置背景色

Calendar.DrawStrToday(g, j.ToString().PadLeft(2, ' '), x, y);

else

Calendar.DrawStr(g, j.ToString().PadLeft(2, ' '), x, y);

//星期六结束到星期日时换行,X轴回到初始位置,Y轴增加

if ((day1st + j) % 7 == 0)

{

x = startX;

y = y + posLen;

}

else

x = x + posLen;

}

return bmp;

}

}

///

/// 绘制字符串

///

public static void DrawStr(Graphics g, string s, float x, float y)

{

Font font = new Font("宋体", 25, FontStyle.Bold);

PointF pointF = new PointF(x, y);

g.DrawString(s, font, new SolidBrush(Color.Yellow), pointF);

}

///

/// 绘制有背景颜色的字符串

///

public static void DrawStrToday(Graphics g, string s, float x, float y)

{

Font font = new Font("宋体", 25, FontStyle.Bold);

PointF pointF = new PointF(x, y);

SizeF sizeF = g.MeasureString(s, font);

g.FillRectangle(Brushes.White, new RectangleF(pointF, sizeF));

g.DrawString(s, font, Brushes.Black, pointF);

}

}

}

主窗体设置壁纸等的代码FrmMain.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.Drawing.Drawing2D;

using Microsoft.Win32;

using System.Collections;

using System.Runtime.InteropServices;

using System.Xml;

using System.Drawing.Imaging;

namespace SetWallpaper

{

public partial class FrmMain : Form

{

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

int screenWidth = Screen.PrimaryScreen.Bounds.Width;

int screenHeight = Screen.PrimaryScreen.Bounds.Height;

FileInfo[] picFiles;

public FrmMain()

{

InitializeComponent();

}

private void FrmMain_Load(object sender, EventArgs e)

{

List list = new List(){

new DictionaryEntry(1, "居中显示"),

new DictionaryEntry(2, "平铺显示"),

new DictionaryEntry(3, "拉伸显示")

};

cbWallpaperStyle.DisplayMember = "Value";

cbWallpaperStyle.ValueMember = "Key";

cbWallpaperStyle.DataSource = list;

txtPicDir.Text = XmlNodeInnerText("");

timer1.Tick += new EventHandler(timer_Tick);

Text = string.Format("设置桌面壁纸(当前电脑分辨率{0}×{1})", screenWidth, screenHeight);

}

///

/// 浏览单个图片

///

private void btnBrowse_Click(object sender, EventArgs e)

{

using (OpenFileDialog openFileDialog = new OpenFileDialog())

{

openFileDialog.Filter = "Images (*.BMP;*.JPG)|*.BMP;*.JPG;";

openFileDialog.AddExtension = true;

openFileDialog.RestoreDirectory = true;

if (openFileDialog.ShowDialog() == DialogResult.OK)

{

Bitmap img = (Bitmap)Bitmap.FromFile(openFileDialog.FileName);

pictureBox1.Image = img;

string msg = (img.Width != screenWidth || img.Height != screenHeight) ? ",建议选择和桌面分辨率一致图片" : "";

lblStatus.Text = string.Format("当前图片分辨率{0}×{1}{2}", img.Width, img.Height, msg);

}

}

}

///

/// 手动设置壁纸

///

private void btnSet_Click(object sender, EventArgs e)

{

if (pictureBox1.Image == null)

{

MessageBox.Show("请先选择一张图片。");

return;

}

Image img = pictureBox1.Image;

SetWallpaper(img);

}

private void SetWallpaper(Image img)

{

Bitmap bmp = Calendar.GetCalendarPic(img);

string filename = Application.StartupPath + "/wallpaper.bmp";

bmp.Save(filename, ImageFormat.Bmp);

string tileWallpaper = "0";

string wallpaperStyle = "0";

string selVal = cbWallpaperStyle.SelectedValue.ToString();

if (selVal == "1")

tileWallpaper = "1";

else if (selVal == "2")

wallpaperStyle = "2";

//写到注册表,避免系统重启后失效

RegistryKey regKey = Registry.CurrentUser;

regKey = regKey.CreateSubKey("Control Panel\\Desktop");

//显示方式,居中:0 0, 平铺: 1 0, 拉伸: 0 2

regKey.SetValue("TileWallpaper", tileWallpaper);

regKey.SetValue("WallpaperStyle", wallpaperStyle);

regKey.SetValue("Wallpaper", filename);

regKey.Close();

SystemParametersInfo(20, 1, filename, 1);

}

///

/// 浏览文件夹

///

private void btnBrowseDir_Click(object sender, EventArgs e)

{

string defaultfilePath = XmlNodeInnerText("");

using (FolderBrowserDialog dialog = new FolderBrowserDialog())

{

if (defaultfilePath != "")

dialog.SelectedPath = defaultfilePath;

if (dialog.ShowDialog() == DialogResult.OK)

XmlNodeInnerText(dialog.SelectedPath);

txtPicDir.Text = dialog.SelectedPath;

}

}

///

/// 获取或设置配置文件中的选择目录

///

///

///

public string XmlNodeInnerText(string text)

{

string filename = Application.StartupPath + "/config.xml";

XmlDocument doc = new XmlDocument();

if (!File.Exists(filename))

{

XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

doc.AppendChild(dec);

XmlElement elem = doc.CreateElement("WallpaperPath");

elem.InnerText = text;

doc.AppendChild(elem);

doc.Save(filename);

}

else

{

doc.Load(filename);

XmlNode node = doc.SelectSingleNode("//WallpaperPath");

if (node != null)

{

if (string.IsNullOrEmpty(text))

return node.InnerText;

else

{

node.InnerText = text;

doc.Save(filename);

}

}

}

return text;

}

///

/// 定时自动设置壁纸

///

private void btnAutoSet_Click(object sender, EventArgs e)

{

string path = txtPicDir.Text;

if (!Directory.Exists(path))

{

MessageBox.Show("选择的文件夹不存在");

return;

}

DirectoryInfo dirInfo = new DirectoryInfo(path);

picFiles = dirInfo.GetFiles("*.jpg");

if (picFiles.Length == 0)

{

MessageBox.Show("选择的文件夹里面没有图片");

return;

}

if (btnAutoSet.Text == "开始")

{

timer1.Start();

btnAutoSet.Text = "停止";

lblStatus.Text = string.Format("定时自动换壁纸中...");

}

else

{

timer1.Stop();

btnAutoSet.Text = "开始";

lblStatus.Text = "";

}

}

///

/// 定时随机设置壁纸

///

private void timer_Tick(object sender, EventArgs e)

{

timer1.Interval = 1000 * 60 * (int)numericUpDown1.Value;

FileInfo[] files = picFiles;

if (files.Length > 0)

{

Random random = new Random();

int r = random.Next(1, files.Length);

Bitmap img = (Bitmap)Bitmap.FromFile(files[r].FullName);

pictureBox1.Image = img;

SetWallpaper(img);

}

}

///

/// 双击托盘图标显示窗体

///

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)

{

ShowForm();

}

///

/// 隐藏窗体,并显示托盘图标

///

private void HideForm()

{

this.Visible = false;

this.WindowState = FormWindowState.Minimized;

notifyIcon1.Visible = true;

}

///

/// 显示窗体

///

private void ShowForm()

{

this.Visible = true;

this.WindowState = FormWindowState.Normal;

notifyIcon1.Visible = false;

}

private void ToolStripMenuItemShow_Click(object sender, EventArgs e)

{

ShowForm();

}

///

/// 退出

///

private void toolStripMenuItemExit_MouseDown(object sender, MouseEventArgs e)

{

Application.Exit();

}

///

/// 最小化时隐藏窗体,并显示托盘图标

///

private void FrmMain_SizeChanged(object sender, EventArgs e)

{

if (this.WindowState == FormWindowState.Minimized)

{

HideForm();

}

}

}

}

完整的项目代码下载:

python 日历壁纸_winform壁纸工具:为图片添加当月的日历并设为壁纸 .相关推荐

  1. winform壁纸工具:为图片添加当月的日历并设为壁纸

    这几天用winform做了一个设置壁纸的小工具, 为图片添加当月的日历并设为壁纸,可以手动设置壁纸,也可以定时设置壁纸,最主要的特点是在图片上生成当前月的日历信息. 工具和桌面设置壁纸后的效果如下: ...

  2. 如何使用DW工具给图片添加热点

    一.准备一张图片     准备一张需要给不同区域添加不同热点的图片 二.插入图片 三.找到地图工具: 单击鼠标左键点击图片,这时候软件下方的属性面板就会变成和图片相关的属性,注意看左下角部分,如图一中 ...

  3. Python爬虫新手入门教学(十三):爬取高质量超清壁纸

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理. Python爬虫.数据分析.网站开发等案例教程视频免费在线观看 https://space. ...

  4. 找壁纸不用愁了:壁纸网站/APP/工具合集

    苏生不惑第261篇原创文章,将本公众号设为星标,第一时间看最新文章. 之前分享过那些好用的无版权免费图片网站,今天分享些好看的壁纸网站和工具,在公众号后台回复 壁纸 获取软件. wallhaven 这 ...

  5. 时尚美妆图片,让你饱眼福的唯美壁纸

    时尚又唯美的图片,让人看到幸福感倍增!日常我们最长看到图片的出处就是手机屏幕和电脑壁纸! 高清有满足尺寸要求的图片哪里去找呢? 高图网 www.gaopic.com 无论你是喜欢哪类的高清图片作为壁纸 ...

  6. Wallpaper Engine pkg壁纸文件提取工具

    简介: RePKG-GUI 基于RePKG开发,适用于Wallpaper Engine壁纸的用户,可以帮助用户轻松下载Wallpaper Engine中的壁纸图片,让用户可以获取到自己喜欢的壁纸,软件 ...

  7. 图片数据增强,包括模糊,亮度,裁剪,旋转,平移,镜像 ,python ,LabelImg,LabelMe工具

    图片数据增强,包括模糊,亮度,裁剪,旋转,平移,镜像 ,python ,LabelImg,LabelMe工具 1 对象检测图片数据增强(使用labelImg工具) 2 对象分割图片数据增强(使用lab ...

  8. Mac桌面壁纸文件如何提取里面的壁纸图片?heic动态桌面壁纸怎么导出jpg的图片形式?

    Mac桌面壁纸文件如何提取里面的壁纸图片?heic动态桌面壁纸怎么导出jpg的图片形式?这里用到一款Mac软件--Joyoshare HEIC Converter for Mac(图像转换软件),利用 ...

  9. python趣味小工具,图片转Execl,图片转TXT工具

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. WHJWNAVY | 作者 Demo大师 | 来源 python 趣味实用 ...

最新文章

  1. C++调用openssl使用sha256,并取结果前64位作为uint64
  2. shell 去掉字符串中的字母
  3. 什么是缓存里的脏数据.
  4. 【51CTO/BBS】请教: SQL里有没有字符串组合Join的函数??
  5. 优先级调度算法实现_一篇讲透嵌入式操作系统任务调度
  6. HTML DOM之节点操作方法(1)
  7. 最后一周!导师推荐的转录组和可视化学习捷径
  8. ORA-00376: file X cannot be read at this time 问题解决
  9. vue ---- webpack扩展
  10. php _get返回,php如何无刷新获取get返回值
  11. SpringBoot实战(十四):Spring Boot Admin 集成安全模块
  12. queue初始化java,如何在java中实例化一个Queue对象?
  13. dosbox运行C语言,[转载]dosbox的使用方法
  14. 关于 美国大片 与 客户演示PPT
  15. Flash学习资源下载列表
  16. 程序员年薪45万,国企年薪20万,该不该跳槽去国企?
  17. Aerospike SSD模式下写入swb内的记录格式
  18. 简单易学的win10安装教程,值得收藏
  19. 碰到ConvergenceWarning的情况
  20. ipados远程linux软件,JingOS Linux平板系统v0.6下载发布 支持多点触摸手势

热门文章

  1. python爬虫案例——东方财富股票数据采集
  2. 【毕业设计】深度学习人脸识别系统 - python opencv 卷积神经网络
  3. 无任何网络提供程序接受指定的网络路径的解决
  4. Tomcat 6数据源的配置
  5. 机电控制基础之相位滞后校正
  6. SUMO仿真教程(1) ——安装环境的设置(Windows 10系统)
  7. DNAT(目的地址转换)-IP映射和端口映射
  8. 智力竞赛抢答器Verilog HDL设计
  9. JS - 日期 - 使用setDate(0)获取上个月的最大一天
  10. 盒子科技研发部刘恒:聚合支付系统演进