前言
基于c#开发语言,笔者提供了一般类型的计时器、数码管类型的计时器以及倒计时程序,总计三个程序。上传的文件包含了完整文件,有需要的人可以自行下载。如果有错误的地方,还希望各位博友指出。
界面效果图
普通倒计时程序的界面效果图如图1所示。
数码管计时器的界面效果图如图2所示。
倒计时界面的效果图如图3所示。
部分代码
Timer
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;
namespace Timer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//当前时间值(以秒为单位)
private int _nowSecond = 0;
//“开始”按钮的点击响应事件方法
private void buttonStart_Click(object sender, EventArgs e)
{
//初始化当前的时间值(以秒为单位)
_nowSecond = 0;
//启动定时器
timer1.Enabled = true;
timer2.Enabled = true;
//启用或禁用按钮
buttonStart.Enabled = false;
buttonPauseContinue.Enabled = true;
buttonStop.Enabled = true;
//显示冒号
labelColon1.Visible = true;
labelColon2.Visible = true;
}
//“暂停/继续”按钮的点击响应事件方法
private void buttonPauseContinue_Click(object sender, EventArgs e)
{
//判断按钮为“暂停”按钮还是“继续”按钮
if (buttonPauseContinue.Text == "暂停")
{
//停止定时器
timer1.Enabled = false;
timer2.Enabled = false;
//改变按钮文字
buttonPauseContinue.Text = "继续";
}
else
{
//启动定时器
timer1.Enabled = true;
timer2.Enabled = true;
//改变按钮文字
buttonPauseContinue.Text = "暂停";
}
}
//“停止”按钮的点击响应事件方法
private void buttonStop_Click(object sender, EventArgs e)
{
//停止定时器
timer1.Enabled = false;
timer2.Enabled = false;
//启用或禁用按钮
buttonStart.Enabled = true;
buttonPauseContinue.Enabled = false;
buttonStop.Enabled = false;
//修改按钮文字
buttonPauseContinue.Text = "暂停";
}
//timer1的Tick响应事件方法
private void timer1_Tick_1(object sender, EventArgs e)
{
//当前时间值(以秒为单位)加1
_nowSecond++;
//把当前时间值(以秒为单位)转为hh:mm:ss形式
int hour = _nowSecond / 36000;
int minute = (_nowSecond % 36000) / 600;
double second = (_nowSecond % 36000) % 600 / 10.0;
//以hh:mm:ss形式显示当前时间值
if (hour <= 9)
labelHour.Text = "0" + hour.ToString();
else
labelHour.Text = hour.ToString();
if (minute <= 9)
labelMinute.Text = "0" + minute.ToString();
else
labelMinute.Text = minute.ToString();
if (second < 10)
labelSecond.Text = "0" + second.ToString();
else
labelSecond.Text = second.ToString();
}
//timer2的Tick响应事件方法
private void timer2_Tick(object sender, EventArgs e)
{
//让冒号闪烁起来
if (labelColon1.Visible == true)
{
labelColon1.Visible = false;
labelColon2.Visible = false;
}
else
{
labelColon1.Visible = true;
labelColon2.Visible = true;
}
}
//窗口的Move事件响应方法
private void Form1_Move(object sender, EventArgs e)
{
//停靠屏幕左右边框
int screenRight = Screen.PrimaryScreen.Bounds.Right;
int formRight = this.Left + this.Width;
if (Math.Abs(screenRight - formRight) <= 100)
this.Left = screenRight - this.Size.Width;
if (Math.Abs(this.Left) <= 100)
this.Left = 0;
//停靠屏幕上下边框
int screenBottom = Screen.PrimaryScreen.Bounds.Bottom;
int formBottom = this.Top + this.Height;
if (Math.Abs(screenBottom - formBottom) <= 60)
this.Top = screenBottom - this.Size.Height;
if (Math.Abs(this.Top) <= 100)
this.Top = 0;
}
}
}
Timer_LED
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;
namespace Timer_LED
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//当前时间值(以秒为单位)
private int _nowSecond = 0;
//冒号标记符号
private bool _colonFlag = true;
//“开始”按钮的点击响应事件方法
private void buttonStart_Click(object sender, EventArgs e)
{
//开启定时器
timer1.Enabled = true;
timer2.Enabled = true;
//启用或关闭按钮
buttonStart.Enabled = false;
buttonParseContinue.Enabled = true;
buttonStop.Enabled = true;
}
//“暂停/继续”按钮的点击响应事件方法
private void buttonPauseContinue_Click(object sender, EventArgs e)
{
//判断当前是“暂停”或“继续”
if (buttonParseContinue.Text == "暂停")
{
//修改按钮文字
buttonParseContinue.Text = "继续";
//关闭定时器
timer1.Enabled = false;
timer2.Enabled = false;
}
else
{
//修改按钮文字
buttonParseContinue.Text = "暂停";
//开启定时器
timer1.Enabled = true;
timer2.Enabled = true;
}
}
//“停止”按钮的点击响应事件方法
private void buttonStop_Click(object sender, EventArgs e)
{
//重置时间值(以秒为单位)
_nowSecond = 0;
//关闭定时器
timer1.Enabled = false;
timer2.Enabled = false;
//启用或禁用按钮
buttonStart.Enabled = true;
buttonParseContinue.Enabled = false;
buttonStop.Enabled = false;
}
//timer1的Tick事件响应方法
private void timer1_Tick(object sender, EventArgs e)
{
//时间值(以秒为单位)加1
_nowSecond++;
//把当前时间值(以秒为单位)转为hh:mm:ss形式
int hour = _nowSecond / 3600;
int minute = (_nowSecond % 3600) / 60;
int second = (_nowSecond % 3600) % 60;
//以hh:mm:ss形式显示当前时间值
if (hour <= 9)
{
pictureBoxHour1.Image = Image.FromFile(@"..\..\..\DigitalNums\0.bmp");
pictureBoxHour2.Image = Image.FromFile(@"..\..\..\DigitalNums\" + hour.ToString() + ".bmp");
}
else
{
pictureBoxHour1.Image = Image.FromFile(@"..\..\..\DigitalNums\" + (hour / 10).ToString() + ".bmp");
pictureBoxHour2.Image = Image.FromFile(@"..\..\..\DigitalNums\" + (hour % 10).ToString() + ".bmp");
}
if (minute <= 9)
{
pictureBoxMinute1.Image = Image.FromFile(@"..\..\..\DigitalNums\0.bmp");
pictureBoxMinute2.Image = Image.FromFile(@"..\..\..\DigitalNums\" + (minute % 10).ToString() + ".bmp");
}
else
{
}
if (second <= 9)
{
pictureBoxSecond1.Image = Image.FromFile(@"..\..\..\DigitalNums\0.bmp");
pictureBoxSecond2.Image = Image.FromFile(@"..\..\..\DigitalNums\" + (second % 10).ToString() + ".bmp");
}
else
{
pictureBoxSecond1.Image = Image.FromFile(@"..\..\..\DigitalNums\" + (second / 10).ToString() + ".bmp");
pictureBoxSecond2.Image = Image.FromFile(@"..\..\..\DigitalNums\" + (second % 10).ToString() + ".bmp");
}
}
//timer1的Tick事件响应方法
private void timer2_Tick(object sender, EventArgs e)
{
//让冒号闪烁起来
if (_colonFlag == true)
{
pictureBoxColon1.Image = Image.FromFile(@"..\..\..\DigitalNums\dot2.bmp");
pictureBoxColon2.Image = Image.FromFile(@"..\..\..\DigitalNums\dot2.bmp");
_colonFlag = false;
}
else
{
pictureBoxColon1.Image = Image.FromFile(@"..\..\..\DigitalNums\blank.bmp");
pictureBoxColon2.Image = Image.FromFile(@"..\..\..\DigitalNums\blank.bmp");
_colonFlag = true;
}
}
//窗口的Move事件响应方法
private void Form1_Move(object sender, EventArgs e)
{
//停靠屏幕左右边框
int screenRight = Screen.PrimaryScreen.Bounds.Right;
int formRight = this.Left + this.Width;
if (Math.Abs(screenRight - formRight) <= 100)
this.Left = screenRight - this.Size.Width;
if (Math.Abs(this.Left) <= 100)
this.Left = 0;
//停靠屏幕上下边框
int screenBottom = Screen.PrimaryScreen.Bounds.Bottom;
int formBottom = this.Top + this.Height;
if (Math.Abs(screenBottom - formBottom) <= 60)
this.Top = screenBottom - this.Size.Height;
if (Math.Abs(this.Top) <= 100)
this.Top = 0;
}
}
}
Countdown
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;
namespace Countdown
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//当前时间值(以秒为单位)
private int _nowSecond = 0;
//“开始”按钮的点击响应事件方法
private void buttonStart_Click(object sender, EventArgs e)
{
//判断是否输入时间值
if (textBoxSecond.Text != "")
{
//初始化当前的时间值(以秒为单位),从文本框获取时间值
_nowSecond = Convert.ToInt32(textBoxSecond.Text) * 10;
//启动定时器
timer1.Enabled = true;
timer2.Enabled = true;
//启用或禁用按钮
buttonStart.Enabled = false;
buttonPauseContinue.Enabled = true;
buttonStop.Enabled = true;
//显示冒号
labelColon1.Visible = true;
labelColon2.Visible = true;
}
else
{
//给予提示
MessageBox.Show("请输入时间!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//“暂停/继续”按钮的点击响应事件方法
private void buttonPauseContinue_Click(object sender, EventArgs e)
{
//判断按钮为“暂停”按钮还是“继续”按钮
if (buttonPauseContinue.Text == "暂停")
{
//停止定时器
timer1.Enabled = false;
timer2.Enabled = false;
//改变按钮文字
buttonPauseContinue.Text = "继续";
}
else
{
//启动定时器
timer1.Enabled = true;
timer2.Enabled = true;
//改变按钮文字
buttonPauseContinue.Text = "暂停";
}
}
//“停止”按钮的点击响应事件方法
private void buttonStop_Click(object sender, EventArgs e)
{
//停止定时器
timer1.Enabled = false;
timer2.Enabled = false;
//启用或禁用按钮
buttonStart.Enabled = true;
buttonPauseContinue.Enabled = false;
buttonStop.Enabled = false;
//修改按钮文字
buttonPauseContinue.Text = "暂停";
}
//timer1的Tick响应事件方法
private void timer1_Tick(object sender, EventArgs e)
{
//判断时间值是否是0
if(_nowSecond != 0)
{
//把当前时间值(以秒为单位)转为hh:mm:ss形式
int hour = _nowSecond / 36000;
int minute = (_nowSecond % 36000) / 600;
double second = (_nowSecond % 36000) % 600 / 10.0;
//以hh:mm:ss形式显示当前时间值
if (hour <= 9)
labelHour.Text = "0" + hour.ToString();
else
labelHour.Text = hour.ToString();
if (minute <= 9)
labelMinute.Text = "0" + minute.ToString();
else
labelMinute.Text = minute.ToString();
if (second < 10)
labelSecond.Text = "0" + second.ToString();
else
labelSecond.Text = second.ToString();
//当前时间值(以秒为单位)减1
_nowSecond--;
}
else
{
//关闭定时器
timer1.Enabled = false;
timer2.Enabled = false;
//启用或禁用按钮
buttonStart.Enabled = true;
buttonPauseContinue.Enabled = false;
buttonStop.Enabled = false;
//重置labelSecond的值
labelSecond.Text = "00.0";
//给予提示
MessageBox.Show("倒计时结束!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//timer2的Tick响应事件方法
private void timer2_Tick(object sender, EventArgs e)
{
//让冒号闪烁起来
if (labelColon1.Visible == true)
{
labelColon1.Visible = false;
labelColon2.Visible = false;
}
else
{
labelColon1.Visible = true;
labelColon2.Visible = true;
}
}
//窗口的Move事件响应方法
private void Form1_Move(object sender, EventArgs e)
{
//停靠屏幕左右边框
int screenRight = Screen.PrimaryScreen.Bounds.Right;
int formRight = this.Left + this.Width;
if (Math.Abs(screenRight - formRight) <= 100)
this.Left = screenRight - this.Size.Width;
if (Math.Abs(this.Left) <= 100)
this.Left = 0;
//停靠屏幕上下边框
int screenBottom = Screen.PrimaryScreen.Bounds.Bottom;
int formBottom = this.Top + this.Height;
if (Math.Abs(screenBottom - formBottom) <= 60)
this.Top = screenBottom - this.Size.Height;
if (Math.Abs(this.Top) <= 100)
this.Top = 0;
}
}
}