C#小鬧鐘 v2.0

來源:互聯網
上載者:User

接上一篇,這次加了一個瀏覽本地wav檔案的功能 介面稍稍做了點小改動

程式碼

View Code

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;
}
}
}
}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.