C#小闹钟 v2.0
接上一篇,这次加了一个浏览本地wav文件的功能 界面稍稍做了点小改动
程序代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Windows.Forms;
using System.Media;
namespace alarmClock
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SoundPlayer player= new SoundPlayer();
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
//绑定到combobox
for (int i = 0; i <= 23; i++)
{
cmbHour.Items.Add(i);
}
for (int j = 0; j < 60; j++)
{
cmbMinute.Items.Add(j);
}
//绑定铃声
cmbRing.Items.Add("步步高音乐.wav");
cmbRing.Items.Add("背景音乐.wav");
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Interval = 1000;
lblNow.Text = DateTime.Now.ToString();
}
private void btnPreview_Click(object sender, EventArgs e)
{
playJudge();
}
/// <summary>
/// 判断播放本地还是系统自带的
/// </summary>
private void playJudge()
{
if (cmbRing.Text==""&&txtUpload.Text=="")
{
MessageBox.Show("请选择系统铃声或本地铃声");
return;
}
if (cmbRing.Text == "" || txtUpload.Text != "")
{
playSound(txtUpload.Text);
}
if (cmbRing.Text != "" || txtUpload.Text == "")
{
playSound(cmbRing.Text);
}
}
/// <summary>
/// 播放wav声音文件
/// </summary>
private void playSound(string path)
{
//用new出来的实例点SoundLocation指定想要播放的音乐名称
player.SoundLocation = path;//(将播放音乐放在应用程序Debug目录下)
player.Load();
//音乐播放
player.Play();
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (cmbHour.Text==""&&cmbMinute.Text=="")
{
MessageBox.Show("没有设置闹铃的时刻");
return;
}
timer2.Start();
}
private void timer2_Tick(object sender, EventArgs e)
{
timer2.Interval = 1000;
string h = cmbHour.Text;
string m = cmbMinute.Text;
string nowH =DateTime.Now.Hour.ToString();
string nowM = DateTime.Now.Minute.ToString();
if (h == nowH && m == nowM)
{
playJudge();
//开启后停止线程
timer2.Stop();
}
}
private void btnStop_Click(object sender, EventArgs e)
{
timer2.Stop();
player.Stop();
}
/// <summary>
/// 浏览本地铃声
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
//判断是否打开了"打开文件对话框"
if (openFileDialog1.ShowDialog()==DialogResult.OK)
{
string file=openFileDialog1.FileName;
if (file!=null)
{
//判断上传文件是否为wav铃声
//D:\winformTest\alarmClcok\myClock\bin\Debug\步步高音乐.wav
string fileExtend = file.Substring(file.LastIndexOf(".") + 1).ToString().ToLower();
if (fileExtend!="wav")
{
MessageBox.Show("请选择wav类型的文件");
return;
}
txtUpload.Text = file;
}
}
}
}
}