C#关机小程序源码
程序员文章站
2023-12-03 15:31:52
下面是运行的效果图核心代码:复制代码 代码如下: using system; using system.collections.generic; using system....
下面是运行的效果图
核心代码:
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
/*
*
* 整理:张晓天
* q q:977602650
* 日期:2012-03-29
*
*/
namespace exitcomputer
{
public partial class form1 : form
{
int gotime = 0;//程序运行时间
string cmd = null;//写入shutdown的命令
int counttime = 0;//到程序执行的时间
public form1()
{
initializecomponent();
}
/// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void form1_load(object sender, eventargs e)
{
nfipic.icon = this.icon;//指定系统的图标
label1.text = datetime.now.tostring("yyyy年 mm月 dd日 hh :mm :ss");//系统时间
nudhour.maximum = 23;
nudminutes.maximum = 59;
nudsecond.maximum = 59;//指定控件的最大值
cbomonth.text = datetime.now.month.tostring();
cboday.text = datetime.now.day.tostring();
nudhour.value = datetime.now.hour;
nudminutes.value = datetime.now.minute;
nudsecond.value = datetime.now.second;//指定控件的值
//指定时间控件发生的频率
tmrtime.interval = 1000;
tmrtime.enabled = true;
//为月和日赋值
for (int i = 1; i < 13; i++)
{
cbomonth.items.add(i);
}
for (int i = 1; i < 32; i++)
{
cboday.items.add(i);
}
//指定不可用的控件
cbomonth.enabled = false;
cboday.enabled = false;
nudhour.enabled = false;
nudminutes.enabled = false;
nudsecond.enabled = false;
btnexit.enabled = false;
}
//定时的属性改写时事件
private void chktiming_checkedchanged(object sender, eventargs e)
{
if (chktiming.checked == true)
{
cbomonth.enabled = true;
cboday.enabled = true;
nudhour.enabled = true;
nudminutes.enabled = true;
nudsecond.enabled = true;
}
else
{
cbomonth.enabled = false;
cboday.enabled = false;
nudhour.enabled = false;
nudminutes.enabled = false;
nudsecond.enabled = false;
}
}
/// <summary>
/// 显示窗体
/// </summary>
private void windowshow()
{
this.show();
this.showintaskbar = true;
this.windowstate = formwindowstate.normal;
}
//重写事件实现气泡提示
protected override void onsizechanged(eventargs e)
{
base.onsizechanged(e);
if (windowstate == formwindowstate.minimized)
{
nfipic.showballoontip(30);
}
}
/// <summary>
/// 隐藏托盘
/// </summary>
private void windowhide()
{
this.hide();
this.showintaskbar = false;
nfipic.visible = true;
nfipic.showballoontip(30);
}
//最小化窗体时的事件
private void form1_resize(object sender, eventargs e)
{
if (this.windowstate == formwindowstate.minimized)
this.windowhide();
}
//鼠标双击托盘图标时的事件
private void nfipic_mousedoubleclick(object sender, mouseeventargs e)
{
this.windowshow();
}
//保证选择的月份是正确的
private void cbomonth_textchanged(object sender, eventargs e)
{
try
{
int temp1 = int.parse(cbomonth.text);
int temp = int.parse(cboday.text);
if (temp1 < 1 || temp1 > 12)
{
cbomonth.text = datetime.now.month.tostring();
}
if (cbomonth.text == "2")
{
if (datetime.isleapyear(datetime.now.year) == true)//判断今年是否为闰年
{
if (temp > 28)
{
cboday.text = datetime.now.day.tostring();
}
}
else if (temp > 29)
{
cboday.text = datetime.now.day.tostring();
}
}
else if (cbomonth.text == "4" || cbomonth.text == "6" || cbomonth.text == "9" || cbomonth.text == "11")
{
if (temp > 30)
{
cboday.text = datetime.now.day.tostring();
}
}
}
catch (exception)
{
cbomonth.text = datetime.now.month.tostring();
}
}
//保证选择的天数是正确的
private void cboday_textchanged(object sender, eventargs e)
{
try
{
int temp = int.parse(cboday.text);
if (temp < 1 || temp > 31)
{
cboday.text = datetime.now.day.tostring();
}
//判断月份
if (cbomonth.text == "2")
{
if (datetime.isleapyear(datetime.now.year) == true)//判断今年是否为闰年
{
if (temp > 28)
{
cboday.text = datetime.now.day.tostring();
}
}
else if (temp > 29)
{
cboday.text = datetime.now.day.tostring();
}
}
else if (cbomonth.text == "4" || cbomonth.text == "6" || cbomonth.text == "9" || cbomonth.text == "11")
{
if (temp > 30)
{
cboday.text = datetime.now.day.tostring();
}
}
}
catch (exception)
{
cboday.text = datetime.now.day.tostring();
}
}
//程序执行时给counttime赋的值
private bool timecontrol()
{
if (chktiming.checked == true)
{
datetime starttime = convert.todatetime(datetime.now.year.tostring() + "/" + cbomonth.text + "/" +
cboday.text + " " + nudhour.value.tostring() + ":" + nudminutes.value.tostring() + ":" + nudsecond.value.tostring());
timespan endtime = starttime - datetime.now;
counttime = endtime.days * 86400 + endtime.hours * 3600 + endtime.minutes * 60 + endtime.seconds;
}
if (counttime < 0)
{
messagebox.show("对不起!您选择的时间有误!", "操作提示", messageboxbuttons.ok, messageboxicon.information);
//让窗体回到初始化时的状态
chktiming.enabled = true;
btnlogout.enabled = true;
btnover.enabled = true;
btnagain.enabled = true;
btnexit.enabled = false;
tmrtime.enabled = false;
return false;
}
tmrtime.enabled = true;
return true;
}
//时间控件每次发生的事件
private void tmrtime_tick(object sender, eventargs e)
{
label1.text = datetime.now.tostring("yyyy年 mm月 dd日 hh :mm :ss");
if (cmd != null)
{
gotime += 1;
if (counttime - gotime + 5 < 0)
{
txtremind.text = "0";
cmd = null;
}
else
{
//在提示文本中显示的内容
int temp = counttime - gotime + 1;
txtremind.text = temp / 86400 + "天 " + temp % 86400 / 3600 + "时 " + temp % 3600 / 60 + "分 " + temp % 60 + "秒 ";
if (counttime - gotime + 1 == 0) //判断时间是否到了
{
system.diagnostics.processstartinfo p1 = new system.diagnostics.processstartinfo("shutdown.exe", cmd);
p1.windowstyle = system.diagnostics.processwindowstyle.hidden;
system.diagnostics.process.start(p1);
application.exitthread();
application.exit();//程序结束
}
}
}
}
/// <summary>
/// 定时关机事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnover_click(object sender, eventargs e)
{
if (!timecontrol()) return;
cmd = "-s -t 0";
lblremind.text = "剩余时间";
chktiming.checked = false;
chktiming.enabled = false;
btnexit.enabled = true;
allow();
}
/// <summary>
/// 重启事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnagain_click(object sender, eventargs e)
{
if (!timecontrol()) return;
cmd = "-r -t 0";
lblremind.text = "剩余时间";
chktiming.checked = false;
chktiming.enabled = false;
btnexit.enabled = true;
allow();
}
private void btnlogout_click(object sender, eventargs e)
{
if (!timecontrol()) return;
cmd = "-l";
lblremind.text = "剩余时间";
chktiming.checked = false;
chktiming.enabled = false;
btnexit.enabled = true;
allow();
}
private void tsmishow_click(object sender, eventargs e)
{
this.windowshow();
}
private void tsmiexit_click(object sender, eventargs e)
{
this.dispose();
}
/// <summary>
/// 单击取消发生的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnexit_click(object sender, eventargs e)
{
btnlogout.enabled = true;
btnover.enabled = true;
btnagain.enabled = true;
btnexit.enabled = false;
cmd = null;
gotime = 0;
counttime = 0;
txtremind.text = "0";
lblremind.text = "关机提示";
chktiming.enabled = true;
messagebox.show("任务已被成功取消", "操作提示", messageboxbuttons.ok, messageboxicon.information);
}
/// <summary>
/// 提醒事件
/// </summary>
public void allow()
{
dialogresult result;
result = messagebox.show("程序将自动最小化到托盘", "操作提示", messageboxbuttons.yesno, messageboxicon.information);
if (result == dialogresult.yes)
{
this.windowhide();
}
}
}
}
源码打包下载
核心代码:
复制代码 代码如下:
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
/*
*
* 整理:张晓天
* q q:977602650
* 日期:2012-03-29
*
*/
namespace exitcomputer
{
public partial class form1 : form
{
int gotime = 0;//程序运行时间
string cmd = null;//写入shutdown的命令
int counttime = 0;//到程序执行的时间
public form1()
{
initializecomponent();
}
/// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void form1_load(object sender, eventargs e)
{
nfipic.icon = this.icon;//指定系统的图标
label1.text = datetime.now.tostring("yyyy年 mm月 dd日 hh :mm :ss");//系统时间
nudhour.maximum = 23;
nudminutes.maximum = 59;
nudsecond.maximum = 59;//指定控件的最大值
cbomonth.text = datetime.now.month.tostring();
cboday.text = datetime.now.day.tostring();
nudhour.value = datetime.now.hour;
nudminutes.value = datetime.now.minute;
nudsecond.value = datetime.now.second;//指定控件的值
//指定时间控件发生的频率
tmrtime.interval = 1000;
tmrtime.enabled = true;
//为月和日赋值
for (int i = 1; i < 13; i++)
{
cbomonth.items.add(i);
}
for (int i = 1; i < 32; i++)
{
cboday.items.add(i);
}
//指定不可用的控件
cbomonth.enabled = false;
cboday.enabled = false;
nudhour.enabled = false;
nudminutes.enabled = false;
nudsecond.enabled = false;
btnexit.enabled = false;
}
//定时的属性改写时事件
private void chktiming_checkedchanged(object sender, eventargs e)
{
if (chktiming.checked == true)
{
cbomonth.enabled = true;
cboday.enabled = true;
nudhour.enabled = true;
nudminutes.enabled = true;
nudsecond.enabled = true;
}
else
{
cbomonth.enabled = false;
cboday.enabled = false;
nudhour.enabled = false;
nudminutes.enabled = false;
nudsecond.enabled = false;
}
}
/// <summary>
/// 显示窗体
/// </summary>
private void windowshow()
{
this.show();
this.showintaskbar = true;
this.windowstate = formwindowstate.normal;
}
//重写事件实现气泡提示
protected override void onsizechanged(eventargs e)
{
base.onsizechanged(e);
if (windowstate == formwindowstate.minimized)
{
nfipic.showballoontip(30);
}
}
/// <summary>
/// 隐藏托盘
/// </summary>
private void windowhide()
{
this.hide();
this.showintaskbar = false;
nfipic.visible = true;
nfipic.showballoontip(30);
}
//最小化窗体时的事件
private void form1_resize(object sender, eventargs e)
{
if (this.windowstate == formwindowstate.minimized)
this.windowhide();
}
//鼠标双击托盘图标时的事件
private void nfipic_mousedoubleclick(object sender, mouseeventargs e)
{
this.windowshow();
}
//保证选择的月份是正确的
private void cbomonth_textchanged(object sender, eventargs e)
{
try
{
int temp1 = int.parse(cbomonth.text);
int temp = int.parse(cboday.text);
if (temp1 < 1 || temp1 > 12)
{
cbomonth.text = datetime.now.month.tostring();
}
if (cbomonth.text == "2")
{
if (datetime.isleapyear(datetime.now.year) == true)//判断今年是否为闰年
{
if (temp > 28)
{
cboday.text = datetime.now.day.tostring();
}
}
else if (temp > 29)
{
cboday.text = datetime.now.day.tostring();
}
}
else if (cbomonth.text == "4" || cbomonth.text == "6" || cbomonth.text == "9" || cbomonth.text == "11")
{
if (temp > 30)
{
cboday.text = datetime.now.day.tostring();
}
}
}
catch (exception)
{
cbomonth.text = datetime.now.month.tostring();
}
}
//保证选择的天数是正确的
private void cboday_textchanged(object sender, eventargs e)
{
try
{
int temp = int.parse(cboday.text);
if (temp < 1 || temp > 31)
{
cboday.text = datetime.now.day.tostring();
}
//判断月份
if (cbomonth.text == "2")
{
if (datetime.isleapyear(datetime.now.year) == true)//判断今年是否为闰年
{
if (temp > 28)
{
cboday.text = datetime.now.day.tostring();
}
}
else if (temp > 29)
{
cboday.text = datetime.now.day.tostring();
}
}
else if (cbomonth.text == "4" || cbomonth.text == "6" || cbomonth.text == "9" || cbomonth.text == "11")
{
if (temp > 30)
{
cboday.text = datetime.now.day.tostring();
}
}
}
catch (exception)
{
cboday.text = datetime.now.day.tostring();
}
}
//程序执行时给counttime赋的值
private bool timecontrol()
{
if (chktiming.checked == true)
{
datetime starttime = convert.todatetime(datetime.now.year.tostring() + "/" + cbomonth.text + "/" +
cboday.text + " " + nudhour.value.tostring() + ":" + nudminutes.value.tostring() + ":" + nudsecond.value.tostring());
timespan endtime = starttime - datetime.now;
counttime = endtime.days * 86400 + endtime.hours * 3600 + endtime.minutes * 60 + endtime.seconds;
}
if (counttime < 0)
{
messagebox.show("对不起!您选择的时间有误!", "操作提示", messageboxbuttons.ok, messageboxicon.information);
//让窗体回到初始化时的状态
chktiming.enabled = true;
btnlogout.enabled = true;
btnover.enabled = true;
btnagain.enabled = true;
btnexit.enabled = false;
tmrtime.enabled = false;
return false;
}
tmrtime.enabled = true;
return true;
}
//时间控件每次发生的事件
private void tmrtime_tick(object sender, eventargs e)
{
label1.text = datetime.now.tostring("yyyy年 mm月 dd日 hh :mm :ss");
if (cmd != null)
{
gotime += 1;
if (counttime - gotime + 5 < 0)
{
txtremind.text = "0";
cmd = null;
}
else
{
//在提示文本中显示的内容
int temp = counttime - gotime + 1;
txtremind.text = temp / 86400 + "天 " + temp % 86400 / 3600 + "时 " + temp % 3600 / 60 + "分 " + temp % 60 + "秒 ";
if (counttime - gotime + 1 == 0) //判断时间是否到了
{
system.diagnostics.processstartinfo p1 = new system.diagnostics.processstartinfo("shutdown.exe", cmd);
p1.windowstyle = system.diagnostics.processwindowstyle.hidden;
system.diagnostics.process.start(p1);
application.exitthread();
application.exit();//程序结束
}
}
}
}
/// <summary>
/// 定时关机事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnover_click(object sender, eventargs e)
{
if (!timecontrol()) return;
cmd = "-s -t 0";
lblremind.text = "剩余时间";
chktiming.checked = false;
chktiming.enabled = false;
btnexit.enabled = true;
allow();
}
/// <summary>
/// 重启事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnagain_click(object sender, eventargs e)
{
if (!timecontrol()) return;
cmd = "-r -t 0";
lblremind.text = "剩余时间";
chktiming.checked = false;
chktiming.enabled = false;
btnexit.enabled = true;
allow();
}
private void btnlogout_click(object sender, eventargs e)
{
if (!timecontrol()) return;
cmd = "-l";
lblremind.text = "剩余时间";
chktiming.checked = false;
chktiming.enabled = false;
btnexit.enabled = true;
allow();
}
private void tsmishow_click(object sender, eventargs e)
{
this.windowshow();
}
private void tsmiexit_click(object sender, eventargs e)
{
this.dispose();
}
/// <summary>
/// 单击取消发生的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnexit_click(object sender, eventargs e)
{
btnlogout.enabled = true;
btnover.enabled = true;
btnagain.enabled = true;
btnexit.enabled = false;
cmd = null;
gotime = 0;
counttime = 0;
txtremind.text = "0";
lblremind.text = "关机提示";
chktiming.enabled = true;
messagebox.show("任务已被成功取消", "操作提示", messageboxbuttons.ok, messageboxicon.information);
}
/// <summary>
/// 提醒事件
/// </summary>
public void allow()
{
dialogresult result;
result = messagebox.show("程序将自动最小化到托盘", "操作提示", messageboxbuttons.yesno, messageboxicon.information);
if (result == dialogresult.yes)
{
this.windowhide();
}
}
}
}
源码打包下载