闹钟定时案例

该文章描述了一个Windows应用程序,主窗体可设置闹钟定时,使用分钟后提示窗体会显示并播放wav格式的闹钟声。用户可以选择关闭提示窗体或等待十秒自动关闭,主窗体会在提示窗体关闭后重新显示。如果在主窗体上点击关闭,程序会最小化到托盘。要完全退出程序,需从托盘菜单选择退出。代码示例展示了如何实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 主窗体设置闹钟定时分钟

  1. 闹钟提醒窗体在定时道道后会展示出来,并播放闹铃(还可以选择更换闹钟提示窗体的图像)

  1. 当闹钟提示窗体出现后,主窗体会隐藏直至十秒的闹钟响完之后,提示窗体关闭,主窗体重新显示出来

  1. 当手动点击关闭闹钟提示窗口按钮时,提示窗口关闭,主窗体也会重新显示出来

  1. 当手动关闭主窗体按钮时,电脑右下角会弹出托盘提示框

  1. 如果想关闭程序和显示程序窗体,需要在电脑托盘里,鼠标右击闹钟程序图标选择操作(点击退出才会真正关闭闹钟定时程序)

使用闹钟需要用的定时、文件选择、托盘和绑定操作控件时,有些属性设置需要参考前几篇案例文章
闹钟铃声必须是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程序中添加设置图片代码即可(两种获取图片方式如下图:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值