在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 闹钟小程序(C#源码)

闹钟小程序(C#源码)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:44.85M
  • 下载次数:32
  • 浏览次数:428
  • 发布时间:2020-05-29
  • 实例类别:C#语言基础
  • 发 布 人:robot666
  • 文件格式:.zip
  • 所需积分:2
 相关标签:

实例介绍

【实例简介】用C#写的闹钟小程序,可以自定义选择闹钟响铃时间以及响铃的音乐,可以实时查看闹钟响铃倒计时

【实例截图】

 from clipboard

【核心代码】

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 AlarmClock
{
    public partial class AlarmClock : Form
    {
        private bool BeginAlarm = false;
        private DateTime AlarmTime;
        private SoundPlayer AlarmBell;
        private long CountDownTime;

        public AlarmClock()
        {
            InitializeComponent();
        }

        private void Clock_Load(object sender, EventArgs e)
        {
            ToolTip testTip = new ToolTip();
            testTip.IsBalloon = true;
            testTip.SetToolTip(TestButton, "测试选择的音乐是否可用");
            timer1.Interval = 1000; 
            timer1.Enabled = true;
            this.AcceptButton = BeginButton;
            PathBox.Text = Application.StartupPath.ToString()   @"\Music\ring.wav";
        }

        private void Clock_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                SystemSounds.Exclamation.Play();
            }
            catch { }
            if (MessageBox.Show("请确认是否退出程序?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void BeginButton_Click(object sender, EventArgs e)
        {
            if (TestButton.Text == "停止")
            {
                SystemSounds.Hand.Play();
                MessageBox.Show("请先停止测试闹钟铃声!", "提示");
                return;
            }
            if (Time3.Checked)
            {
                try
                {
                    int hour = Convert.ToInt32(HourBox.Text.Trim());
                    int minute = Convert.ToInt32(MinuteBox.Text.Trim());
                    if (hour < 0 || hour > 23 || minute < 0 || minute > 60)
                    {
                        SystemSounds.Hand.Play();
                        MessageBox.Show("起床时间设置有误!", "警告");
                        return;
                    }
                }
                catch (Exception)
                {
                    SystemSounds.Hand.Play();
                    MessageBox.Show("起床时间设置有误!", "警告");
                    return;
                }
            }
            string AlarmTime1 = HourBox.Text.Trim()   ":"   MinuteBox.Text.Trim()   ":"   "00";
            AlarmTime = DateTime.Parse(AlarmTime1);
            if (DateTime.Compare(AlarmTime, DateTime.Now) > 0)
            {
                SystemSounds.Asterisk.Play();
                MessageBox.Show("请务必测试闹钟铃声是否可用!", "提示");

                BeginAlarm = true;

                HourBox.Enabled = false;
                MinuteBox.Enabled = false;
                BeginButton.Visible = false;
                SelectButton.Visible = false;
                TestButton.Visible = false;
                Time1.Visible = false;
                Time2.Visible = false;
                Time3.Visible = false;
                StopButton.Visible = true;
                Count_Down.Visible = true;
                HourLabel.Visible = true;
                MinuteLabel.Visible = true;
                SecondLabel.Visible = true;
                CountDown_Hour.Visible = true;
                CountDown_Minute.Visible = true;
                CountDown_Second.Visible = true;
            }
            else
            {
                SystemSounds.Hand.Play();
                MessageBox.Show("起床时间小于当前时间!", "警告");
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime NowTime = DateTime.Now;
            Now_Time.Text = NowTime.ToString();

            if (BeginAlarm)
            {
                TimeSpan CountDown = AlarmTime - NowTime;
                HourLabel.Text = CountDown.Hours.ToString();
                MinuteLabel.Text = CountDown.Minutes.ToString();
                SecondLabel.Text = CountDown.Seconds.ToString();
                CountDownTime = CountDown.Hours * 3600   CountDown.Minutes * 60   CountDown.Seconds;
                TimeLabel.Text = CountDownTime.ToString();
            }
        }

        private void StopButton_Click(object sender, EventArgs e)
        {
            SystemSounds.Exclamation.Play();
            if (MessageBox.Show("请确认是否结束倒计时?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                BeginAlarm = false;

                HourBox.Enabled = true;
                MinuteBox.Enabled = true;
                BeginButton.Visible = true;
                SelectButton.Visible = true;
                TestButton.Visible = true;
                Time1.Visible = true;
                Time2.Visible = true;
                Time3.Visible = true;
                StopButton.Visible = false;
                Count_Down.Visible = false;
                HourLabel.Visible = false;
                MinuteLabel.Visible = false;
                SecondLabel.Visible = false;
                CountDown_Hour.Visible = false;
                CountDown_Minute.Visible = false;
                CountDown_Second.Visible = false;

                HourLabel.Text = "0";
                MinuteLabel.Text = "0";
                SecondLabel.Text = "0";
                CountDownTime = 1;
                TimeLabel.Text = "000000";
            }
        }

        private void PlayAlarmBell()
        {
            AlarmBell = new SoundPlayer();
            AlarmBell.SoundLocation = PathBox.Text.Trim();
            try
            {
                AlarmBell.PlayLooping();
                DialogResult res = MessageBox.Show("起床啦!起床啦!起床啦!",
                "提示");
                if (res == DialogResult.OK)
                {
                    AlarmBell.Stop();
                    AlarmBell.Dispose();
                }
            }
            catch (Exception)
            {
                SystemSounds.Hand.Play();
                MessageBox.Show("无效闹钟铃声!", "警告");
                AlarmBell.Dispose();
            }
        }

        private void TestAlarmBell()
        {
            if (TestButton.Text == "测试")
            {
                AlarmBell = new SoundPlayer();
                AlarmBell.SoundLocation = PathBox.Text.Trim();
                try
                {
                    AlarmBell.PlayLooping();
                    TestButton.Text = "停止";
                    SelectButton.Enabled = false;
                }
                catch (Exception)
                {
                    SystemSounds.Hand.Play();
                    MessageBox.Show("无效闹钟铃声!", "警告");
                    AlarmBell.Dispose();
                    TestButton.Text = "测试";
                    SelectButton.Enabled = true;
                }
            }
            else
            {
                try
                {
                    AlarmBell.Stop();
                    AlarmBell.Dispose();
                }
                catch (Exception)
                {
                    SystemSounds.Hand.Play();
                    MessageBox.Show("未知错误!", "提示");
                }
                finally
                {
                    TestButton.Text = "测试";
                    SelectButton.Enabled = true;
                }
            }
        }

        private void TestButton_Click(object sender, EventArgs e)
        {
            TestAlarmBell();

            //SystemSounds.Asterisk.Play();
            //SystemSounds.Beep.Play();
            //SystemSounds.Exclamation.Play();
            //SystemSounds.Hand.Play();
            //SystemSounds.Question.Play();
        }

        private void SelectButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = @"F:\Alarm Clock\";  //默认打开目录
            ofd.Filter = "铃声文件|*.wav";  //显示的文件类型
            ofd.RestoreDirectory = true;  //对话框记忆之前打开的目录
            ofd.FilterIndex = 1;  //文件类型的显示顺序
            if (ofd.ShowDialog() == DialogResult.OK)  //选中文件
            {
                string BellPath = ofd.FileName;
                PathBox.Text = BellPath;
            }
        }

        private void TimeLabel_TextChanged(object sender, EventArgs e)
        {
            if (CountDownTime <= 0)
            {
                BeginAlarm = false;
                PlayAlarmBell();

                HourBox.Enabled = true;
                MinuteBox.Enabled = true;
                BeginButton.Visible = true;
                SelectButton.Visible = true;
                TestButton.Visible = true;
                Time1.Visible = true;
                Time2.Visible = true;
                Time3.Visible = true;
                StopButton.Visible = false;
                Count_Down.Visible = false;
                HourLabel.Visible = false;
                MinuteLabel.Visible = false;
                SecondLabel.Visible = false;
                CountDown_Hour.Visible = false;
                CountDown_Minute.Visible = false;
                CountDown_Second.Visible = false;

                HourLabel.Text = "0";
                MinuteLabel.Text = "0";
                SecondLabel.Text = "0";
                CountDownTime = 1;
                TimeLabel.Text = "000000";
            }
        }

        private void Time1_CheckedChanged(object sender, EventArgs e)
        {
            HourBox.Text = "13";
            MinuteBox.Text = "30";
            HourBox.Enabled = false;
            MinuteBox.Enabled = false;
        }

        private void Time2_CheckedChanged(object sender, EventArgs e)
        {
            HourBox.Text = "14";
            MinuteBox.Text = "00";
            HourBox.Enabled = false;
            MinuteBox.Enabled = false;
        }

        private void Time3_CheckedChanged(object sender, EventArgs e)
        {
            HourBox.Enabled = true;
            MinuteBox.Enabled = true;
        }
    }
}

标签:

实例下载地址

闹钟小程序(C#源码)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警