一、下载地址
http://pan.baidu.com/s/1hqJ0f0S
二、程序运行效果
1)先输入时间信息,年(4位)、月(2位)、日(2位)、时(2位)、分(2位)、秒(2位)
有效输入形如:2014|10|05|23|59|59
2)程序显示出倒计时效果
3)关闭程序时,程序会弹窗提示,询问是否要最小化到托盘,如果最小化到托盘,则双击托盘上的图标可以弹出气泡显示倒计时,右键托盘上的图标可以拉出菜单,选择显示界面或退出程序
三、重要控件说明
拉到窗体上的控件是9个PictureBox,从左到右依次为pcb1到pcb9
四、资源说明
程序中一共有10张图片,都是用画图绘制的,数字0-9。这些图片保存在编译环境的\Resources目录下
五、程序代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//需要手动添加引用 Microsoft.VisualBasic
using Microsoft.VisualBasic;
namespace CountDown
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
//输入时间信息
string s = Interaction.InputBox(
"请输入时间信息(如2014|10|04|23|59|59)", "目标时间",
(DateTime.Now.Year).ToString().PadLeft(4, '0') + "|" +
(DateTime.Now.Month).ToString().PadLeft(2, '0') + "|" +
(DateTime.Now.Day + 1).ToString().PadLeft(2, '0') + "|23|59|59");
//解析读出的时间信息到DateTime中
int year, month, day, hour, minute, second;
bool isLegal = isLegalFormat(
s, out year, out month, out day, out hour, out minute, out second);
//输入不合法则直接退出程序
if (!isLegal)
{
MessageBox.Show("非法输入");
Environment.Exit(0);
}
//如果输入的时间过早或过晚,则提示后退出程序
dtInput = new DateTime(year, month, day, hour, minute, second);
if ((dtInput - DateTime.Now).TotalSeconds <= 0)
{
MessageBox.Show("这个时间已经过去了");
Environment.Exit(0);
}
if ((dtInput - DateTime.Now).TotalSeconds >= 1000000000)
{
MessageBox.Show("这个时间太远了");
Environment.Exit(0);
}
this.Text += " | 目标时间:" + dtInput.ToString();
//初始化各个图片框
this.pcb1.Image = Properties.Resources._0;
this.pcb2.Image = Properties.Resources._0;
this.pcb3.Image = Properties.Resources._0;
this.pcb4.Image = Properties.Resources._0;
this.pcb5.Image = Properties.Resources._0;
this.pcb6.Image = Properties.Resources._0;
this.pcb7.Image = Properties.Resources._0;
this.pcb8.Image = Properties.Resources._0;
this.pcb9.Image = Properties.Resources._0;
//建立右下角的托盘图标
NotifyIcon ntf = new NotifyIcon();
ntf.BalloonTipTitle = "倒计时工具";
ntf.BalloonTipText = "目标时间:" + dtInput.ToString() + "\n" +
"剩余时间:" + ((int)(dtInput - DateTime.Now).TotalSeconds) + "秒";
ntf.BalloonTipIcon = ToolTipIcon.Info;
ntf.Icon = this.Icon;
ntf.Visible = true;
ntf.ShowBalloonTip(2000);
//双击托盘图标显示气泡
ntf.DoubleClick += (obj, args) =>
{
ntf.BalloonTipText = "目标时间:" + dtInput.ToString() + "\n" +
"剩余时间:" + ((int)(dtInput - DateTime.Now).TotalSeconds) + "秒";
ntf.ShowBalloonTip(2000);
};
//托盘小图标右键触发的菜单
ContextMenuStrip cms = new ContextMenuStrip();
//显示面板按钮
ToolStripItem tsiShow = new ToolStripButton("显示面板");
tsiShow.Click += (obj, args) =>
{
this.Show();
this.ShowInTaskbar = true;
};
cms.Items.Add(tsiShow);
//退出程序按钮
ToolStripItem tsiExit = new ToolStripButton("退出程序");
tsiExit.Click += (obj, args) =>
{
Environment.Exit(0);
};
cms.Items.Add(tsiExit);
//将按钮添加到菜单中
ntf.ContextMenuStrip = cms;
//每隔200毫秒检查一次时间
Timer tmr = new Timer();
tmr.Interval = 200;
tmr.Tick += (obj, args) =>
{
int span = (int)(dtInput - DateTime.Now).TotalSeconds;
if (span != 0)
{
RefreshNum(span);
}
else
{
RefreshNum(span);
tmr.Stop(); //时间到,关闭计时器
MessageBox.Show("时间到!");
Environment.Exit(0);
}
};
tmr.Enabled = true;
tmr.Start();
}
public DateTime dtInput; //输入的时间
/// <summary>
/// 判断输入的年月日是否合法
/// </summary>
/// <param name="s">输入字符串</param>
/// <param name="year">年</param>
/// <param name="month">月</param>
/// <param name="day">日</param>
/// <param name="hour">时</param>
/// <param name="minute">分</param>
/// <param name="second">秒</param>
/// <returns></returns>
private bool isLegalFormat(string s,
out int year, out int month, out int day,
out int hour, out int minute, out int second)
{
year = 0; month = 0; day = 0;
hour = 0; minute = 0; second = 0;
if (s.Length != 19)
{
return false;
}
int.TryParse(s.Substring(0, 4), out year);
int.TryParse(s.Substring(5, 2), out month);
int.TryParse(s.Substring(8, 2), out day);
int.TryParse(s.Substring(11, 2), out hour);
int.TryParse(s.Substring(14, 2), out minute);
int.TryParse(s.Substring(17, 2), out second);
//检查年信息是否合法
if (year <= 0 || year > 9999)
{
return false;
}
//检查月信息是否合法
if (month <= 0 || month > 12)
{
return false;
}
//检查日信息是否合法
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
//大月
if (day <= 0 || day > 31)
{
return false;
}
}
break;
case 4:
case 6:
case 9:
case 11:
{
//小月
if (day <= 0 || day > 30)
{
return false;
}
}
break;
case 2:
{
//是闰年的情况
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
if (day <= 0 || day > 29)
{
return false;
}
}
else //不是闰年的情况
{
if (day <= 0 || day > 28)
{
return false;
}
}
}
break;
}
//检查时信息是否合法
if (hour < 0 || hour > 23)
{
return false;
}
//检查分信息是否合法
if (minute < 0 || minute > 59)
{
return false;
}
//检查秒信息是否合法
if (second < 0 || second > 59)
{
return false;
}
return true;
}
/// <summary>
/// 让各个图片显示出数字n
/// </summary>
/// <param name="n">被显示数字</param>
public void RefreshNum(int n)
{
pcb1.Image = SelectResource(n % 1000000000 / 100000000);
pcb2.Image = SelectResource(n % 100000000 / 10000000);
pcb3.Image = SelectResource(n % 10000000 / 1000000);
pcb4.Image = SelectResource(n % 1000000 / 100000);
pcb5.Image = SelectResource(n % 100000 / 10000);
pcb6.Image = SelectResource(n % 10000 / 1000);
pcb7.Image = SelectResource(n % 1000 / 100);
pcb8.Image = SelectResource(n % 100 / 10);
pcb9.Image = SelectResource(n % 10);
}
/// <summary>
/// 根据给定的数字n,选择一张图片
/// </summary>
/// <param name="n">个位数字n</param>
/// <returns>选中的图片</returns>
public Bitmap SelectResource(int n)
{
switch (n)
{
case 0: return Properties.Resources._0;
case 1: return Properties.Resources._1;
case 2: return Properties.Resources._2;
case 3: return Properties.Resources._3;
case 4: return Properties.Resources._4;
case 5: return Properties.Resources._5;
case 6: return Properties.Resources._6;
case 7: return Properties.Resources._7;
case 8: return Properties.Resources._8;
case 9: return Properties.Resources._9;
default: return null;
}
}
/// <summary>
/// 关闭窗口前询问是退出还是最小化到托盘
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
if (MessageBox.Show("确实要退出吗?\"是\"退出程序,\"否\"最小化到托盘",
"提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) ==
DialogResult.OK)
{
Environment.Exit(0);
}
else
{
this.Hide();
this.ShowInTaskbar = false;
}
}
}
}
END