六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 39|回复: 0

C#winform的小闹钟

[复制链接]

升级  46.67%

30

主题

30

主题

30

主题

秀才

Rank: 2

积分
120
 楼主| 发表于 2013-1-26 14:11:26 | 显示全部楼层 |阅读模式
   最近比较忙,老师忘记一些事情,整天对着电脑,所以为了提醒自己,做个C#的winform程序练练手吧!
我实现的闹钟功能比较简单,主要是用到了timer定时控件,还有闹铃时引用了System.Media命名空间,播放wav文件,试过了只能是这种类型的文件,不晓得能不能播放MP3的,目前我不会,主要代码如下
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)        {            if (string.IsNullOrEmpty(cmbRing.Text))            {                MessageBox.Show("请选择播放的铃声!!");                return;            }            playSound();        }        /// <summary>        /// 播放wav声音文件        /// </summary>        private void playSound()        {                        //用new出来的实例点SoundLocation指定想要播放的音乐名称            player.SoundLocation = cmbRing.Text;//(将播放音乐放在应用程序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)            {                playSound();                //开启后停止线程                timer2.Stop();            }        }        private void btnStop_Click(object sender, EventArgs e)        {            timer2.Stop();            player.Stop();        }        }} 

 
 
 本程序全部源码下载!
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表