C# 定时关机小程序
C# 定时关机小程序
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace 定时关机小程序
{
public partial class MyForm : Form
{
Timer time = new Timer(); //实例化一个计数器
DateTime ds;//定时变量
string path = @"c:/windows/system32/shutdown.exe";//路径
public MyForm()
{
InitializeComponent();
time.Interval = 800;//计时器 每800毫秒 发生一下 Tick 事件。
time.Start();//启动计时器
time.Tick += time_Tick;//计时器Tick事件,当指定的计时器间隔已过去而且计时器处于启用状态时发生。
txtdssj.Text = DateTime.Now.ToString();// 获取当前时间 为 定时时间 文本框 显示的时间.
}
private void time_Tick(object sender, EventArgs e)//time.Tick 事件处理器.(每800毫秒 调用一次)
{
txtdqsj.Text = DateTime.Now.ToString();//获取当前时间 为 当前时间 文本框 显示的时间。
DateTime dq = Convert.ToDateTime(txtdqsj.Text);//将 当前时间 文本框 的时间(字符串) 转换为 Datetime 类型,并且赋值给 dq 变量
if (dq == ds)//判断当前时间 是否 等于 定时时间
{
Process.Start(path,"-s -t 10");//相等 则调用进程 ,关机程序,在10毫秒后关机.
}
}
private void btnqd_Click(object sender, EventArgs e) //确认按钮 事件
{
string dszfc = txtdssj.Text;//获取定时关机 时间
if (DateTime.TryParse(dszfc, out ds))//判断 定时时间 是否为 时间格式
{
if (DateTime.Now < ds)//判断定时时间是否大于当前时间
{
MessageBox.Show("关机时间设定成功.", "提示", MessageBoxButtons.OK);//大于 则 提示定时关机 时间成功
}
else
{
MessageBox.Show("关机时间不能小于当前时间.", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);//否则 则提示 错误。
}
}
else
{
MessageBox.Show("关机时间设定错误,已恢复默认时间.","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);// 不是时间格式 提示
txtdssj.Text = DateTime.Now.AddSeconds(-1).ToString();//时间恢复 到 当前时间的 前一秒.
}
}
private void btnqx_Click(object sender, EventArgs e)//取消 按钮 单击事件
{
Process.Start(path,"-a");// -a 停止关机程序。
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) //托盘窗体图标 双击 事件
{
if (WindowState == FormWindowState.Minimized)//判断当前窗体是否为最小化
{
WindowState = FormWindowState.Normal;//恢复窗体为 默认大小
this.Activate();//激活当前窗体
this.ShowInTaskbar = true;//任务栏显示 为真
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();//释放资源
this.Close();//关闭窗体
}
else
{
e.Cancel = true;//取消 为真
}
}
private void 显示ToolStripMenuItem_Click(object sender, EventArgs e) //托盘窗体图标 显示 单击 事件
{
if (WindowState == FormWindowState.Minimized)//判断窗体是否为 最小化
{
WindowState = FormWindowState.Normal;//窗体恢复默认 大小
this.Activate();//激活窗体
this.ShowInTaskbar = true;//任务栏窗体显示
notifyIcon1.Visible = false;//托盘窗体图标不显示
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)// 托盘窗体图标 退出 单击 事件
{
if (MessageBox.Show("是否确认退出程序?", "退出",MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.OK)//判断是否 单击 OK按钮
{
this.Dispose();//释放资源
this.Close();//关闭窗体
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)//链接标签 单击 事件
{
Process.Start("IExplore", "http://www.daobk.com");//通过使用 IE浏览器 打开 网址.
}
}
}输出结果:
完整源码下载:
解压密码:www.daobk.com
定时关机小程序下载:
参考资料《博客园》
