框架:
在这里插入图片描述
代码:

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;
using System.Media;//添加音乐时命名空间
namespace _02闹钟
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox2.Text = DateTime.Now.ToString();//页面加载时获取当前时间
        }
        bool b = false;//这个变量记录时间是否到了
        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox2.Text = DateTime.Now.ToString();//timer每一秒运行一次更新一次值
            if (b==false)//证明时间没到,进行一次时间判断,时间到了就不要进行判断
            {
                if (h==DateTime.Now.Hour)
                {
                    if (m==DateTime.Now.Minute)
                    {
                        b = true;//证明时间刚好到了
                        SoundPlayer sound = new SoundPlayer();
                        sound.SoundLocation = "../../shengpizi.wav";
                        sound.Play();
                    }
                }
            }
        }
        int h, m;
        private void button1_Click(object sender, EventArgs e)
        {
            b = false;
            //获取设置的闹钟时间的小时数和分钟数
            h = int.Parse(textBox4.Text);
            m = int.Parse(textBox5.Text);
        }
    }
}

posted on 2018-12-27 20:54  豆皮没有豆  阅读(349)  评论(0)    收藏  举报