当前位置:首页 > C#学习 > C#软件 > 正文内容

C# 桌面小闹钟1.0版:当前时间,定时,取消定时,托盘显示

小道5年前 (2018-11-08)C#软件4155

C#版 桌面小闹钟1.0 

显示当前时间,可以定时,取消定时,最小化托盘显示.关闭提示信息.


源码+注释:

using System;
using System.Windows.Forms; //窗体需要使用的
using System.Media;//声音播放使用的
using System.Diagnostics;//网站打开使用的

namespace 桌面小闹钟
{
    public partial class MyForm : Form
    {
        Timer time = new Timer();//实例化一个计时器
        DateTime ds;// 定时时间 变量
        SoundPlayer sound = new SoundPlayer();//实例化一个 声音播放
        public MyForm()
        {
            InitializeComponent();
            sound.Stream = Properties.Resources.Ringin;//在资源文件夹(Resources)中获取 Ringin.wav 声音文件.
            time.Interval = 800;//计时器 每800毫秒 发生一下 Tick 事件。
            time.Start();//启动计时器
            time.Tick += time_Tick;//计时器Tick事件,当指定的计时器间隔已过去而且计时器处于启用状态时发生。
            txtsztime.Text = DateTime.Now.ToString();// 获取当前时间 为 定时时间 文本框 显示的时间. 
        }

        private void time_Tick(object sender, EventArgs e) //time.Tick 事件处理器.(每800毫秒 调用一次)
        {
            txttimenow.Text = DateTime.Now.ToString();//获取当前时间 为 当前时间 文本框 显示的时间。
            DateTime xzsj = Convert.ToDateTime(txttimenow.Text);//将 当前时间 文本框 的时间(字符串) 转换为 Datetime 类型,并且赋值给 xzsj 变量
            if (xzsj == ds)//判断当前时间 是否 等于 定时时间
            {
                sound.PlayLooping();//播放声音.
                if (MessageBox.Show("时间到了!!!当前时间:"+ds, "提示", MessageBoxButtons.OK,MessageBoxIcon.Warning) == DialogResult.OK) //弹出提示,并判断是否按下 OK 键。
                {
                    sound.Stop();//关闭播放声音.
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)//确认定时 按钮 单击事件
        {
            string szsj = txtsztime.Text;//获取 定时时间 文本框 的时间(字符串)
            if (DateTime.TryParse(szsj, out ds))//判断 定时时间 文本框 时间(字符串) 是否可以转换成 时间格式.
            {
                if (DateTime.Now >= ds)//判断当前时间 是否 大于或等于 定时时间.
                {
                    MessageBox.Show("定时时间小于当前时间了.", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);//提示 定时时间 不能小于当前时间.
                }
                else
                {
                    MessageBox.Show("定时成功.响铃时间:" + ds, "提示", MessageBoxButtons.OK);//否则,提示定时时间成功.
                }
            }
            else
            {
                MessageBox.Show("定时时间格式不正确,已恢复为默认时间.", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);//提示 定时时间 错误.
                txtsztime.Text = DateTime.Now.AddSeconds(-1).ToString();//将 定时时间 文本框设置为 当前时间 减去 1秒。
            }

        }
        private void button2_Click(object sender, EventArgs e) //取消定时  按钮 单击事件
        {
            txtsztime.Text = DateTime.Now.AddSeconds(-1).ToString();//将 定时时间 文本框设置为 当前时间 减去 1秒。
            ds = Convert.ToDateTime(txtsztime.Text);//转换 定时时间 文本框字符串, 并赋值给 ds 变量.
            MessageBox.Show("定时取消成功.", "提示", MessageBoxButtons.OK);//提示定时取消。
        }
        private void button3_Click(object sender, EventArgs e) //30分钟 按钮 单击事件
        {
            txtsztime.Text = DateTime.Now.AddMinutes(30).ToString();//将当前时间 加上 30分钟 赋值给 定时时间 文本框
            ds = Convert.ToDateTime(txtsztime.Text);//转换 定时时间 文本框字符串, 并赋值给 ds 变量.
            MessageBox.Show("定时成功.响铃时间:"+ds, "提示", MessageBoxButtons.OK);//提示定时成功.
        }

        private void button4_Click(object sender, EventArgs e)//1小时 按钮 单击事件 ,以下同 30分钟 按钮 单击事件
        {
            txtsztime.Text = DateTime.Now.AddHours(1).ToString();
            ds = Convert.ToDateTime(txtsztime.Text);
            MessageBox.Show("定时成功.响铃时间:" + ds, "提示", MessageBoxButtons.OK);
        }

        private void button5_Click(object sender, EventArgs e)//2小时 按钮 单击事件 ,以下同 30分钟 按钮 单击事件
        {
            txtsztime.Text = DateTime.Now.AddHours(2).ToString();
            ds = Convert.ToDateTime(txtsztime.Text);
            MessageBox.Show("定时成功.响铃时间:" + ds, "提示", MessageBoxButtons.OK);
        }

        private void button8_Click(object sender, EventArgs e)//5小时 按钮 单击事件 ,以下同 30分钟 按钮 单击事件
        {
            txtsztime.Text = DateTime.Now.AddHours(5).ToString();
            ds = Convert.ToDateTime(txtsztime.Text);
            MessageBox.Show("定时成功.响铃时间:" + ds, "提示", MessageBoxButtons.OK);
        }

        private void button7_Click(object sender, EventArgs e)//10小时 按钮 单击事件 ,以下同 30分钟 按钮 单击事件
        {
            txtsztime.Text = DateTime.Now.AddHours(10).ToString();
            ds = Convert.ToDateTime(txtsztime.Text);
            MessageBox.Show("定时成功.响铃时间:" + ds, "提示", MessageBoxButtons.OK);
        }

        private void button6_Click(object sender, EventArgs e)//15小时 按钮 单击事件 ,以下同 30分钟 按钮 单击事件
        {
            txtsztime.Text = DateTime.Now.AddHours(15).ToString();
            ds = Convert.ToDateTime(txtsztime.Text);
            MessageBox.Show("定时成功.响铃时间:" + ds, "提示", MessageBoxButtons.OK);
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)//任务栏托盘图标 双击事件
        {
            if (WindowState == FormWindowState.Minimized)//判断当前窗体是否处于 最小化
            {
                WindowState = FormWindowState.Normal;//将窗体 还原为默认大小
                this.Activate();//激活窗体 并给予它焦点.
                this.ShowInTaskbar = true;//任务栏显示窗体.  获取或设置一个值,该值指示是否在 Windows 任务栏中显示窗体。
                notifyIcon1.Visible = false;//托盘区 隐藏 窗体图标.  获取或设置一个值,指示图标在任务栏的通知区域中是否可见。
            }
        }

        private void MyForm_SizeChanged(object sender, EventArgs e) //窗体大小改变事 事件
        {
            if (WindowState == FormWindowState.Minimized)//判断当前 窗体大小 是否为 最小化
            {
                this.ShowInTaskbar = false;// 窗体最小化,则任务栏 隐藏窗体
                notifyIcon1.Visible = true;//窗体最小化, 则 托盘区 显示 窗体图标.
            }
        }

        private void MyForm_FormClosing(object sender, FormClosingEventArgs e) //关闭窗体前 发生 事件
        {
            if (MessageBox.Show("是否确认退出程序?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)//弹出提示,并判断是否按下 OK键.
            {
                this.Dispose();//按下 OK 键 则 释放资源.
                this.Close();//按下 OK 键 则关闭窗口.
            }
            else//否则
            {
                e.Cancel = true;//取消关闭窗口 为真.
            }
        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)//托盘右键菜单,单击 显示 菜单 事件 
        {
            WindowState = FormWindowState.Normal;//将窗体还原为 默认大小.
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)//托盘右键菜单 单击 退出 菜单 事件
        {
            if (MessageBox.Show("是否确认退出程序?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)//弹出提示,并判断是否按下 OK键.
            {
                this.Dispose();//按下 OK 键 则 释放资源.
                this.Close();//按下 OK 键 则关闭窗口.
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) //链接标签 单击 事件
        {
            Process.Start("IExplore", "http://www.daobk.com");//通过使用 IE浏览器 打开 网址.
        }
    }
}

输出结果:

image.png



完整源码下载:

桌面小闹钟 源码.rar

解压密码:www.daobk.com


桌面小闹钟程序:

桌面小闹钟.rar



程序参考资料:《博客园》《博客园》《博客园

扫描二维码推送至手机访问。

版权声明:本文由小道发布,如需转载请注明出处。

本文链接:https://www.daobk.com/post/115.html

分享给朋友:

“C# 桌面小闹钟1.0版:当前时间,定时,取消定时,托盘显示” 的相关文章

小道的世界1.0版(未完成)

小道的世界1.0版(未完成)

小道的世界1.0版(未完成)已基本实现  注册帐号,登录帐号,查看装备和角色基本属性。基本实现如下图:继续学习以后完善。。。小道的世界程序.rar   程序。小道的世界.rar   源码。...

动态数组(ArrayList)小程序

动态数组(ArrayList)小程序

实现:添加元素,删除元素,查找元素,反转数组,根据索引值插入元素,根据索引值删除元素,将文本文档中每行数据导入动态数组中,将动态数组中的每个元素导出到文本文档中。清空动态数组等。using System; using System.Collections; using&nbs...

List小程序

List小程序

使用泛型List来实现:添加元素,删除元素,查找元素,清空元素,查看元素,索引插入元素。using System; using System.Collections; using System.Collections.Generic; using Sys...

C# 定时关机小程序

C# 定时关机小程序

C# 定时关机小程序using System; using System.Diagnostics; using System.Windows.Forms; namespace 定时关机小程序 {     p...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。