-
主窗体设置闹钟定时分钟
-
闹钟提醒窗体在定时道道后会展示出来,并播放闹铃(还可以选择更换闹钟提示窗体的图像)
-
当闹钟提示窗体出现后,主窗体会隐藏直至十秒的闹钟响完之后,提示窗体关闭,主窗体重新显示出来
-
当手动点击关闭闹钟提示窗口按钮时,提示窗口关闭,主窗体也会重新显示出来
-
当手动关闭主窗体按钮时,电脑右下角会弹出托盘提示框
-
如果想关闭程序和显示程序窗体,需要在电脑托盘里,鼠标右击闹钟程序图标选择操作(点击退出才会真正关闭闹钟定时程序)
使用闹钟需要用的定时、文件选择、托盘和绑定操作控件时,有些属性设置需要参考前几篇案例文章
闹钟铃声必须是wav类型的(MP3文件无法使用)
完整实现代码如下:
-
主窗体:Main.cs

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 Day12_W
{
public partial class Main : Form
{
private int secondStart = 0;
public Main()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
secondStart = 0;
button1.Enabled = false;
timer1.Enabled = true;
timer1.Start();
button2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = true;
timer1.Stop();
button2.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
int num = (int)numericUpDown1.Value;
int second = num * 6;
secondStart++;
if (secondStart >= second && second != 0)
{
button2_Click(sender, e);
Tip tip = new Tip(num, this);
tip.ShowDialog();//当这个窗口打开后,其它窗体不能再进行操作了
}
}
private void Main1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
Hide();
//notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(3000);
}
}
private void 显示闹钟ToolStripMenuItem_Click(object sender, EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Main_Load(object sender, EventArgs e)
{
}
}
}
闹钟提示窗体:Tip.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Day12_W
{
public partial class Tip : Form
{
static string path = Application.StartupPath + "y1645.wav";
SoundPlayer sp = new SoundPlayer(path);
Main main = null;
int numM = 0;
int numClose = 10;
public Tip(int num, Main main1)
{
InitializeComponent();
numM = num;
main = main1;
}
public void timer1_Tick(object sender, EventArgs e)
{
numClose--;
label2.Text = $"{numClose}秒后闹钟关闭";
if (numClose <= 0)
{
timer1.Stop();
this.Close();
main.Show();
main.button1_Click(sender, e);
}
}
private void Tip_Load(object sender, EventArgs e)
{
main.Hide();
timer1.Enabled = true;
timer1.Start();
label1.Text = $"你已经工作{numM}分钟了,休息一会!";
label2.Text = $"{numClose}秒后闹钟关闭";
sp.Play();
}
private void Tip_FormClosing(object sender, FormClosingEventArgs e)
{
timer1.Stop();
main.Show();
sp.Stop();
}
}
}

-
使用可以选择文件的窗体时,需要在窗体中添加文件选择控件和选择按钮控件,在Tip.cs程序中添加设置图片代码即可(两种获取图片方式如下图:)
