因为发现自己经常计划好了做些什么事儿,到点儿了有时候经常忘,于是自己用C#写了一个系统闹钟程序。
目前是V1.0版本 后期还会继续优化。。
程序实例可在本网资源中下载http://download.csdn.net/detail/tskyfree/4239406
软件名称:Clock v1.0
软件功能:设定闹钟
具体详解:
左侧显示系统当前日期
显示系统当前时间:图形化表盘+数字化显示
区间模式:
设定任务名称
设定任务起止时间
显示当前任务状态
填写合理时间并设定,任务中可随时终止或退出程序
计时模式:
设定任务名称
设定距任务执行的倒计时时间
显示当前任务状态
填写合理时间并设定,任务中可随时终止或退出程序
窗体最小化后不显示在任务栏中,在右下侧托盘区显示小图标,点击小图标可还原 窗体
任务计时到达终止条件时:自动弹出提示,窗体置顶
支持快捷键操作 Alt+相应快捷键
作者:TskyFree
开发环境:visual studio 2010
日期:2012年4月
截图展示:
代码实现:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace ClockApp { public partial class Form1 : Form { //定义计时字段 private string startTime; private string endTime; private int countDownLimit; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //初始化窗体 initFrom(); //功能初始化 initClockPlay(); initAreaModel(); initCountDownModel(); } //初始化窗体 private void initFrom() { this.ShowInTaskbar = true; //显示在任务栏中 this.WindowState = FormWindowState.Normal; //窗体正常大小显示 notifyIcon1.Visible = false; //隐藏小图标 } //窗体最小化处理 private void Form1_SizeChanged(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized) { //窗体隐藏 this.ShowInTaskbar = false; //快捷图标显示 notifyIcon1.Visible = true; //注意顺序 notifyIcon1.BalloonTipText = "小闹钟"; notifyIcon1.BalloonTipTitle = "提示"; notifyIcon1.ShowBalloonTip(50); //气球提示淡出 } } //托盘图标点击事件 private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { this.ShowInTaskbar = true; //显示在任务栏中 this.WindowState = FormWindowState.Normal; //窗体正常大小显示 notifyIcon1.Visible = false; //隐藏小图标 } //窗体退出事件 private void From_Close(object sender, EventArgs e) { this.Close(); } //******************************************************************** //左侧钟表计时 private void timer1_Tick(object sender, EventArgs e) { //更新显示系统时间 DateTime dateNow = DateTime.Now; label2.Text = dateNow.ToString("HH:mm:ss"); ClockPos posNow = new ClockPos(); competeClockPos(dateNow, ref posNow); //更新时、分、秒针 lineShape1.X1 = posNow.horX1; lineShape1.Y1 = posNow.horY1; lineShape1.X2 = posNow.horX2; lineShape1.Y2 = posNow.horY2; lineShape2.X1 = posNow.minX1; lineShape2.Y1 = posNow.minY1; lineShape2.X2 = posNow.minX2; lineShape2.Y2 = posNow.minY2; lineShape3.X1 = posNow.secX1; lineShape3.Y1 = posNow.secY1; lineShape3.X2 = posNow.secX2; lineShape3.Y2 = posNow.secY2; } //左侧钟表初始化 private void initClockPlay() { //更新显示系统时间 DateTime dateNow = DateTime.Now; label2.Text = dateNow.ToString("HH:mm:ss"); ClockPos posNow = new ClockPos(); competeClockPos(dateNow, ref posNow); //更新时、分、秒针 lineShape1.X1 = posNow.horX1; lineShape1.Y1 = posNow.horY1; lineShape1.X2 = posNow.horX2; lineShape1.Y2 = posNow.horY2; lineShape2.X1 = posNow.minX1; lineShape2.Y1 = posNow.minY1; lineShape2.X2 = posNow.minX2; lineShape2.Y2 = posNow.minY2; lineShape3.X1 = posNow.secX1; lineShape3.Y1 = posNow.secY1; lineShape3.X2 = posNow.secX2; lineShape3.Y2 = posNow.secY2; //钟表开始计时 timer1.Start(); //显示当前日期 DateTime date = DateTime.Now; label1.Text = date.ToString("yyyy年MM月dd日"); //显示当前时间 label2.Text = date.ToString("HH:mm:ss"); } //计算表钟位置 private void competeClockPos(DateTime dateTime,ref ClockPos pos) { //计算钟表圆心 int clockX = ovalShape1.Location.X + ovalShape1.Height / 2; int clockY = ovalShape1.Location.Y + ovalShape1.Height / 2; //得到当前时分秒 int hor = dateTime.Hour%12; int min = dateTime.Minute; int sec = dateTime.Second; //计算坐标 pos.horX1 = pos.minX1 = pos.secX1 = clockX; pos.horY1 = pos.minY1 = pos.secY1 = clockY; pos.horX2 = (int)(pos.horX1 + (pos.horLength * Math.Sin((hor / 12.0) * 2.0 * Math.PI))); pos.horY2 = (int)(pos.horY1 - (pos.horLength * Math.Cos((hor / 12.0) * 2.0 * Math.PI))); pos.minX2 = (int)(pos.minX1 + (pos.minLength * Math.Sin((min / 60.0) * 2.0 * Math.PI))); pos.minY2 = (int)(pos.minY1 - (pos.minLength * Math.Cos((min / 60.0) * 2.0 * Math.PI))); pos.secX2 = (int)(pos.secX1 + (pos.secLength * Math.Sin((sec / 60.0) * 2.0 * Math.PI))); pos.secY2 = (int)(pos.secY1 - (pos.secLength * Math.Cos((sec / 60.0) * 2.0 * Math.PI))); } //******************************************************************** //区间模式计时 private void timer2_Tick(object sender, EventArgs e) { //检查是否时间到 if (String.Compare(endTime,label2.Text) <= 0) { //停止计时,提示用户 timer2.Stop(); //初始化窗体 initFrom(); this.TopMost = true; //窗体置顶 DialogResult msgBtn = MessageBox.Show("哎呀!"+textBox1.Text+"时间到了~","时间到!",MessageBoxButtons.OK,MessageBoxIcon.Information); while (DialogResult == DialogResult.OK) { MessageBox.Show("\a"); } //区间模式初始化 initAreaModel(); } } //初始化区间模式 private void initAreaModel() { //初始化当前状态 label7.Text = "无任务"; //初始化访问限制 textBox1.Enabled = true; dateTimePicker1.Enabled = true; dateTimePicker2.Enabled = true; button1.Enabled = true; //可设定 button2.Enabled = false; //不可终止 } //区间模式点击设定 private void areaTimeSet(object sender, EventArgs e) { //获取数据 startTime = dateTimePicker1.Text; endTime = dateTimePicker2.Text; //检验起始和终止时间的合法性 if ((String.Compare(startTime, label2.Text) <= 0) && (String.Compare(endTime, label2.Text) >= 0)) { //开始计时 timer2.Start(); //修改其他访问性 textBox1.Enabled = false; dateTimePicker1.Enabled = false; dateTimePicker2.Enabled = false; button1.Enabled = false; button2.Enabled = true; //可随时终止 //修改状态 label7.Text = "任务中...\n\n开始于:" + startTime + "\n\n预计结束于:" + endTime; } else MessageBox.Show("时间的设定与当前时间不合乎常理。\n请仔细检查,重新设定!", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error); } //区间模式点击终止 private void areaTimeEnd(object sender, EventArgs e) { //停止任务计时 timer2.Stop(); //重新初始化 initAreaModel(); } //******************************************************************** //初始化倒计时模式 private void initCountDownModel() { //初始化当前状态 label14.Text = "无任务"; //初始化访问限制 textBox2.Enabled = true; textBox3.Enabled = true; textBox4.Enabled = true; textBox5.Enabled = true; button4.Enabled = true; //可设定 button6.Enabled = false; //不可终止 } //倒计时模式计时 private void timer3_Tick(object sender, EventArgs e) { //更新状态 countDownLimit--; int horLeft = countDownLimit / (60 * 60); //剩余小时 int minLeft = (countDownLimit - horLeft*60*60) / 60; //剩余分钟 int secLeft = countDownLimit - horLeft * 60 * 60 - minLeft * 60; //剩余秒数 label14.Text = "任务中...\n\n剩余时间:"+horLeft.ToString()+"小时"+minLeft+"分钟"+secLeft+"秒"; //检查是否时间到 if (0 == countDownLimit) { timer3.Stop(); //初始化窗体 initFrom(); this.TopMost = true; //窗体置顶 MessageBox.Show("哎呀!" + textBox2.Text + "时间到了~", "时间到!", MessageBoxButtons.OK, MessageBoxIcon.Information); //区间模式初始化 initCountDownModel(); } } //倒计时模式点击设定 private void CountDownTimeSet(object sender, EventArgs e) { //获取数据 countDownLimit = int.Parse(textBox3.Text)*60*60 + int.Parse(textBox4.Text)*60 + int.Parse(textBox5.Text); //检验倒计时时间的合法性 if (countDownLimit <= 0) //不能是负数,不能超过24小时 MessageBox.Show("倒计时不能是零或负数!\n请仔细检查,重新设定!", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error); else if(countDownLimit > 24 * 60 * 60) MessageBox.Show("倒计时不能超过一天!\n请仔细检查,重新设定!", "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error); else //倒计时设定正确 { //开始计时 timer3.Start(); //修改其他访问性 textBox2.Enabled = false; textBox3.Enabled = false; textBox4.Enabled = false; textBox5.Enabled = false; button4.Enabled = false; button6.Enabled = true; //可随时终止 //修改状态 label14.Text = "任务中..."; } } //倒计时模式点击终止 private void CountDownTimeEnd(object sender, EventArgs e) { //停止任务计时 timer3.Stop(); //重新初始化 initCountDownModel(); } } }
public class ClockPos { public int horLength = 40; //时针长度 public int minLength = 55; //分针长度 public int secLength = 65; //秒针长度
//时分秒针的起始坐标 public int secX1; public int secX2; public int secY1; public int secY2; public int minX1; public int minX2; public int minY1; public int minY2; public int horX1; public int horX2; public int horY1; public int horY2; }