winform 為圖片添加當月的日歷并設為壁紙,可以手動設置壁紙,也可以定時設置壁紙;最主要的特色是在圖片上生成當前月的日歷信息。
定時自動換壁紙需要自己手工制定壁紙文件夾,不是從網(wǎng)上自動下載。
部分代碼:
/// <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);
}
}