当前位置: 移动技术网 > IT编程>开发语言>c# > winform壁纸工具为图片添加当前月的日历信息

winform壁纸工具为图片添加当前月的日历信息

2019年07月18日  | 移动技术网IT编程  | 我要评论
这几天用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
{
/// <summary>
/// 计算星期几: 星期日至星期六的值为0-6
/// </summary>
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);
}
/// <summary>
/// 获取指定年月的天数
/// </summary>
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;
}
/// <summary>
/// 获取在图片上生成日历的图片
/// </summary>
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;
}
}
/// <summary>
/// 绘制字符串
/// </summary>
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);
}
/// <summary>
/// 绘制有背景颜色的字符串
/// </summary>
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<dictionaryentry> list = new list<dictionaryentry>(){
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);
}
/// <summary>
/// 浏览单个图片
/// </summary>
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);
}
}
}
/// <summary>
/// 手动设置壁纸
/// </summary>
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);
}
/// <summary>
/// 浏览文件夹
/// </summary>
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;
}
}
/// <summary>
/// 获取或设置配置文件中的选择目录
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 定时自动设置壁纸
/// </summary>
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 = "";
}
}
/// <summary>
/// 定时随机设置壁纸
/// </summary>
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);
}
}
/// <summary>
/// 双击托盘图标显示窗体
/// </summary>
private void notifyicon1_mousedoubleclick(object sender, mouseeventargs e)
{
showform();
}
/// <summary>
/// 隐藏窗体,并显示托盘图标
/// </summary>
private void hideform()
{
this.visible = false;
this.windowstate = formwindowstate.minimized;
notifyicon1.visible = true;
}
/// <summary>
/// 显示窗体
/// </summary>
private void showform()
{
this.visible = true;
this.windowstate = formwindowstate.normal;
notifyicon1.visible = false;
}
private void toolstripmenuitemshow_click(object sender, eventargs e)
{
showform();
}
/// <summary>
/// 退出
/// </summary>
private void toolstripmenuitemexit_mousedown(object sender, mouseeventargs e)
{
application.exit();
}
/// <summary>
/// 最小化时隐藏窗体,并显示托盘图标
/// </summary>
private void frmmain_sizechanged(object sender, eventargs e)
{
if (this.windowstate == formwindowstate.minimized)
{
hideform();
}
}
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网