框架:
代码:
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);
}
}
}