[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DateTime
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public System.TimeSpan Ts_sumtime;//指定时间 与现在时间的时间差
public System.TimeSpan Ts_sumtimeCD;//倒计时加上小时和分钟数 与现在时间的时间差
System.DateTime dtpnewdatenosec;//记录按照指定时间后 指定时间的seconds归零
System.DateTime CDdtpnewdatenosec;//记录按照倒计时后 指定时间的seconds归零
public void btn_start_Click(object sender, EventArgs e)
{
StartExecute();
}
public void StartExecute()
{
if (cob_type.SelectedIndex == 1)//如果选择的是倒计时
{
CDdtpnewdatenosec = System.DateTime.Now.AddHours(dtp.Value.Hour).AddMinutes(dtp.Value.Minute); //new System.DateTime(System.DateTime.Now.Hour + dtp.Value.Hour+dtp.Value.Minute);
}
if (btn_start.Text == "取消执行")//取消执行要做的事情
{
InitializePro(true, true, true, false, false, "开始执行", 0);
timer1.Stop();
//窗体传值的全局变量要初始化为“”
textBox1Value = "";
textBox2Value = "";
}
else
{
//初始化
InitializePro(false, false, false, true, true, "取消执行", 0);
dtpnewdatenosec = new System.DateTime(dtp.Value.Year, dtp.Value.Month, dtp.Value.Day, dtp.Value.Hour, dtp.Value.Minute, 0);
Ts_sumtime = dtpnewdatenosec - System.DateTime.Now;
Ts_sumtimeCD = CDdtpnewdatenosec - System.DateTime.Now;
if (Ts_sumtime.TotalSeconds < 0 && cob_type.SelectedIndex == 0)
{
MessageBox.Show("指定的时间必须大于当前时间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
InitializePro(true, true, true, false, false, "开始执行", 0);
}
else if (cob_type.SelectedIndex == 0)
{
pgb_time.Maximum = (int)Ts_sumtime.TotalSeconds;
btn_start.Text = "取消执行";
timer1.Start();
timer1.Interval = 1000;
}
else if (cob_type.SelectedIndex == 1)
{
pgb_time.Maximum = (int)Ts_sumtimeCD.TotalSeconds;
btn_start.Text = "取消执行";
timer1.Start();
timer1.Interval = 1000;
}
}
}
private void cob_type_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cob_type.SelectedIndex)
{
case 0:
dtp.CustomFormat = "dd日 HH时 mm分";
dtp.Value = System.DateTime.Now;
break;
case 1:
dtp.Value = System.DateTime.Now;
dtp.CustomFormat = "HH小时 mm分钟";
dtp.Value = new System.DateTime(2000, 1, 1, 0, 30, 0);
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
dtp.Format = DateTimePickerFormat.Custom;
dtp.CustomFormat = "dd日 HH时 mm分";
cob_type.SelectedIndex = 0;
cob_action.SelectedIndex = 0;
//DateTime d = new DateTime();
}
private void timer1_Tick(object sender, EventArgs e)
{
AppointTime();
}
private void AppointTime()
{
System.TimeSpan currentTS = System.TimeSpan.Zero;//记录时间差
if (cob_type.SelectedIndex == 1)//如果是倒计时模式
{
currentTS = CDdtpnewdatenosec - System.DateTime.Now;
}
else if (cob_type.SelectedIndex == 0)//如果是指定时间
{
currentTS = dtpnewdatenosec - System.DateTime.Now;
}
if (pgb_time.Value < pgb_time.Maximum)
{
pgb_time.Value += 1;
}
else//到终点 开始执行预先设置
{
timer1.Stop();
InitializePro(true, true, true, false, false, "开始执行", 0);
Action();
}
lbl_msg.Text = currentTS.Days + "天" + currentTS.Hours + "小时" + currentTS.Minutes + "分" + currentTS.Seconds + "秒" + "后系统将执行" + cob_action.SelectedItem.ToString();
}
public void InitializePro(bool action_Enabled, bool Type_enable, bool dtp_Enable, bool lbl_msg_Visible,
bool pgb_time_Visible, string btn_text, int pgb_Value)
{
cob_action.Enabled = action_Enabled;
cob_type.Enabled = Type_enable;
dtp.Enabled = dtp_Enable;
lbl_msg.Text = "";//操作显示提醒
lbl_msg.Visible = lbl_msg_Visible;
pgb_time.Visible = pgb_time_Visible;//进度条可见
btn_start.Text = btn_text;
pgb_time.Value = pgb_Value;
}
public void Action()
{
switch (cob_action.SelectedIndex)
{
case 0://关机
System.Diagnostics.Process.Start("shutdown.exe","-s -f -t 0");
break;
case 1://重启
System.Diagnostics.Process.Start("shutdown.exe", "-r -f -t 0");
break;
case 2:
//执行其它功能
OtherFunc();
break;
default:
break;
}
}
private static string textBox1Value;//窗体传值 接收path
private static string textBox2Value;//窗体传值 接收PID
//窗体传值
public static string TextBox1Value { get => textBox1Value; set => textBox1Value = value; }
public static string TextBox2Value { get => textBox2Value; set => textBox2Value = value; }
public void OtherFunc()//执行其它功能的选项
{
if (textBox1Value!="")
{
ProcessStartInfo psi = new ProcessStartInfo(textBox1Value);
Process.Start(psi);
}
else if(textBox2Value!="")
{
Process p = Process.GetProcessById(Convert.ToInt32(textBox2Value));
p.Kill();
}
}
public static int ShowTime=0;
public static fm_OtherAction fm;
private void cob_action_SelectedIndexChanged(object sender, EventArgs e)
{
//执行其它时间需要加载对应窗口 单例模式
if (cob_action.SelectedIndex==2)
{
if (fm==null)
{
fm = new fm_OtherAction();
fm.Show();
}
else
{
fm.Visible = true;
fm.Show();
}
//fm_OtherAction fm = new fm_OtherAction();
ShowTime = 1;
}
}
}
}