当前位置: 编程技术>.net/c#/asp.net
winform壁纸工具为图片添加当前月的日历信息
来源: 互联网 发布时间:2014-10-16
本文导语: 这几天用winform做了一个设置壁纸的小工具, 为图片添加当月的日历并设为壁纸,可以手动设置壁纸,也可以定时设置壁纸,最主要的特点是在图片上生成当前月的日历信息。 工具和桌面设置壁纸后的效果如下: 在图片上画...
这几天用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 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();
}
}
}
}
工具和桌面设置壁纸后的效果如下:
在图片上画日历的类代码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 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();
}
}
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。