winform倒计时
public partial class Form1 : Form { private int Seconds; public Form1() { InitializeComponent();
// 任务栏是否显示 this.ShowInTaskbar = false; timer1.Interval = 1000; timer1.Enabled = false; // 定时间隔:25分钟 Seconds = 60 * 60; ShowTime(); } private void button1_Click(object sender, EventArgs e) {
// 倒计时开始 timer1.Start(); } private void ShowTime() { // 显示剩余时间 label1.Text = string.Format("{0:d2}:{1:d2}", Seconds / 60, Seconds % 60); } private void timer1_Tick(object sender, EventArgs e) { Seconds--; if (Seconds < 0) { // 停止倒计时 timer1.Stop(); if (WindowState == FormWindowState.Minimized) { //还原窗体显示 this.Show(); WindowState = FormWindowState.Normal; //激活窗体并给予它焦点 this.Activate(); //任务栏区显示图标 //this.ShowInTaskbar = true; //托盘区图标隐藏 notifyIcon1.Visible = false; } return; } ShowTime(); } private void label1_Click(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { Seconds = 60 * 60; ShowTime(); } private void Form1_Load(object sender, EventArgs e) { } private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
// 托盘图标的双击事件 this.Show(); //还原窗体显示 this.WindowState = FormWindowState.Normal; this.Activate(); //任务栏区显示图标 //this.ShowInTaskbar = true; //托盘区图标隐藏 notifyIcon1.Visible = false; } private void Form1_Deactivat(object sender, EventArgs e) {
//借助失去焦点事件判断窗口最小化 if (this.WindowState == FormWindowState.Minimized) { this.notifyIcon1.Visible = true; this.Hide(); this.ShowInTaskbar = false; } } }