图象如下:
代码如下:
using
Microsoft.Win32;
using System.IO;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Timers;
namespace WinShutDown
{
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
private System.ComponentModel.IContainer components;
[DllImport("kernel32.dll", ExactSpelling=true) ]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok );
[DllImport("advapi32.dll", SetLastError=true) ]
internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid );
[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );
[DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool ExitWindowsEx( int flg, int rea );
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.DateTimePicker dtpDay1;
private System.Windows.Forms.DateTimePicker dtpTime1;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.Button btnCancel;
private DateTime shutdownTime=DateTime.Now;
private System.Windows.Forms.RadioButton rb_shutdown;
private System.Windows.Forms.RadioButton rb_reboot;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.TextBox txtTime;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.txtTime = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.dtpTime1 = new System.Windows.Forms.DateTimePicker();
this.dtpDay1 = new System.Windows.Forms.DateTimePicker();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.rb_reboot = new System.Windows.Forms.RadioButton();
this.rb_shutdown = new System.Windows.Forms.RadioButton();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// txtTime
//
this.txtTime.Location = new System.Drawing.Point(48, 16);
this.txtTime.Name = "txtTime";
this.txtTime.Size = new System.Drawing.Size(192, 21);
this.txtTime.TabIndex = 0;
this.txtTime.Text = "";
//
// groupBox1
//
this.groupBox1.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.groupBox1.Controls.Add(this.txtTime);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(280, 48);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "当前系统时间";
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.dtpTime1);
this.groupBox2.Controls.Add(this.dtpDay1);
this.groupBox2.Location = new System.Drawing.Point(8, 64);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(280, 48);
this.groupBox2.TabIndex = 3;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "设置时间";
//
// dtpTime1
//
this.dtpTime1.DropDownAlign = System.Windows.Forms.LeftRightAlignment.Right;
this.dtpTime1.Location = new System.Drawing.Point(144, 16);
this.dtpTime1.Name = "dtpTime1";
this.dtpTime1.ShowUpDown = true;
this.dtpTime1.Size = new System.Drawing.Size(112, 21);
this.dtpTime1.TabIndex = 1;
this.dtpTime1.Value = new System.DateTime(2004, 10, 14, 0, 0, 0, 0);
//
// dtpDay1
//
this.dtpDay1.Location = new System.Drawing.Point(24, 16);
this.dtpDay1.Name = "dtpDay1";
this.dtpDay1.Size = new System.Drawing.Size(104, 21);
this.dtpDay1.TabIndex = 0;
//
// groupBox3
//
this.groupBox3.Controls.Add(this.rb_reboot);
this.groupBox3.Controls.Add(this.rb_shutdown);
this.groupBox3.Location = new System.Drawing.Point(10, 128);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(278, 48);
this.groupBox3.TabIndex = 4;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "选择功能";
//
// rb_reboot
//
this.rb_reboot.Location = new System.Drawing.Point(120, 16);
this.rb_reboot.Name = "rb_reboot";
this.rb_reboot.TabIndex = 1;
this.rb_reboot.Text = "重新启动";
this.rb_reboot.CheckedChanged += new System.EventHandler(this.rb_reboot_CheckedChanged);
//
// rb_shutdown
//
this.rb_shutdown.Checked = true;
this.rb_shutdown.Location = new System.Drawing.Point(24, 16);
this.rb_shutdown.Name = "rb_shutdown";
this.rb_shutdown.Size = new System.Drawing.Size(56, 24);
this.rb_shutdown.TabIndex = 0;
this.rb_shutdown.TabStop = true;
this.rb_shutdown.Text = "关机";
this.rb_shutdown.CheckedChanged += new System.EventHandler(this.rb_shutdown_CheckedChanged);
//
// btnOK
//
this.btnOK.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.btnOK.Location = new System.Drawing.Point(56, 192);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(72, 24);
this.btnOK.TabIndex = 6;
this.btnOK.Text = "开始";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
this.btnCancel.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.btnCancel.Location = new System.Drawing.Point(168, 192);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 24);
this.btnCancel.TabIndex = 7;
this.btnCancel.Text = "退出";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// notifyIcon1
//
this.notifyIcon1.ContextMenu = this.contextMenu1;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem5,
this.menuItem4,
this.menuItem1,
this.menuItem2,
this.menuItem3});
//
// menuItem5
//
this.menuItem5.Index = 0;
this.menuItem5.Text = "重启";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem4
//
this.menuItem4.Index = 1;
this.menuItem4.Text = "关机";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem1
//
this.menuItem1.Index = 2;
this.menuItem1.Text = "退出";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem2
//
this.menuItem2.Index = 3;
this.menuItem2.Text = "隐藏";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 4;
this.menuItem3.Text = "关于";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.ClientSize = new System.Drawing.Size(294, 252);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "定时关机重起v1.0";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private static void DoExitWin(int flg)
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );
ok = ExitWindowsEx( flg, 0 );
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.notifyIcon1.Visible=true;
DateTime shutdownTime=DateTime.Now;
txtTime.Text=System.DateTime.Now.ToString();
dtpDay1.Format = DateTimePickerFormat.Custom;
dtpDay1.CustomFormat = "yyyy-MM-d";
dtpTime1.Format = DateTimePickerFormat.Custom;
dtpTime1.CustomFormat = "HH:mm:ss";
}
private void timer1_Tick(object sender, System.EventArgs e)
{
txtTime.Text=System.DateTime.Now.ToString();
string CurrDate=System.DateTime.Today.ToShortDateString() ;
string CurrTime=System.DateTime.Now.ToLongTimeString();
if(CurrDate==dtpDay1.Value.ToShortDateString() && CurrTime==dtpTime1.Value.ToLongTimeString())
{
if(rb_reboot.Checked==true)
{
DoExitWin(EWX_REBOOT);
Application.Exit();
}
else
{
DoExitWin(EWX_SHUTDOWN);
Application.Exit();
}
}
}
private void btnOK_Click(object sender, System.EventArgs e)
{
this.Visible=false;
this.notifyIcon1.Visible=true;
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
notifyIcon1.Visible=false;
this.Close();
Application.Exit();
}
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
{
this.Visible=true;
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel=true;
this.Visible=false;
this.notifyIcon1.Visible=true;
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
this.Visible=false;
notifyIcon1.Visible=false;
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
notifyIcon1.Visible=false;
this.Close();
Application.Exit();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
Form2 frm2=new Form2();
frm2.Show();
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
DoExitWin(EWX_POWEROFF);
Application.Exit();
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
DoExitWin(EWX_REBOOT);
Application.Exit();
}
bool isNumberic(string s)
{
try
{
int.Parse(s);
return true;
}
catch
{
return false;
}
}
private void rb_reboot_CheckedChanged(object sender, System.EventArgs e)
{
if(rb_reboot.Checked==true)
{
rb_shutdown.Checked=false;
}
else
{
rb_shutdown.Checked=true;
}
}
private void rb_shutdown_CheckedChanged(object sender, System.EventArgs e)
{
if(rb_shutdown.Checked==true)
{
rb_reboot.Checked=false;
}
else
{
rb_reboot.Checked=true;
}
}
}
}
using System.IO;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Timers;
namespace WinShutDown
{
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
private System.ComponentModel.IContainer components;
[DllImport("kernel32.dll", ExactSpelling=true) ]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok );
[DllImport("advapi32.dll", SetLastError=true) ]
internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid );
[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );
[DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool ExitWindowsEx( int flg, int rea );
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.DateTimePicker dtpDay1;
private System.Windows.Forms.DateTimePicker dtpTime1;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.Button btnCancel;
private DateTime shutdownTime=DateTime.Now;
private System.Windows.Forms.RadioButton rb_shutdown;
private System.Windows.Forms.RadioButton rb_reboot;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.TextBox txtTime;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.txtTime = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.dtpTime1 = new System.Windows.Forms.DateTimePicker();
this.dtpDay1 = new System.Windows.Forms.DateTimePicker();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.rb_reboot = new System.Windows.Forms.RadioButton();
this.rb_shutdown = new System.Windows.Forms.RadioButton();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// txtTime
//
this.txtTime.Location = new System.Drawing.Point(48, 16);
this.txtTime.Name = "txtTime";
this.txtTime.Size = new System.Drawing.Size(192, 21);
this.txtTime.TabIndex = 0;
this.txtTime.Text = "";
//
// groupBox1
//
this.groupBox1.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.groupBox1.Controls.Add(this.txtTime);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(280, 48);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "当前系统时间";
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.dtpTime1);
this.groupBox2.Controls.Add(this.dtpDay1);
this.groupBox2.Location = new System.Drawing.Point(8, 64);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(280, 48);
this.groupBox2.TabIndex = 3;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "设置时间";
//
// dtpTime1
//
this.dtpTime1.DropDownAlign = System.Windows.Forms.LeftRightAlignment.Right;
this.dtpTime1.Location = new System.Drawing.Point(144, 16);
this.dtpTime1.Name = "dtpTime1";
this.dtpTime1.ShowUpDown = true;
this.dtpTime1.Size = new System.Drawing.Size(112, 21);
this.dtpTime1.TabIndex = 1;
this.dtpTime1.Value = new System.DateTime(2004, 10, 14, 0, 0, 0, 0);
//
// dtpDay1
//
this.dtpDay1.Location = new System.Drawing.Point(24, 16);
this.dtpDay1.Name = "dtpDay1";
this.dtpDay1.Size = new System.Drawing.Size(104, 21);
this.dtpDay1.TabIndex = 0;
//
// groupBox3
//
this.groupBox3.Controls.Add(this.rb_reboot);
this.groupBox3.Controls.Add(this.rb_shutdown);
this.groupBox3.Location = new System.Drawing.Point(10, 128);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(278, 48);
this.groupBox3.TabIndex = 4;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "选择功能";
//
// rb_reboot
//
this.rb_reboot.Location = new System.Drawing.Point(120, 16);
this.rb_reboot.Name = "rb_reboot";
this.rb_reboot.TabIndex = 1;
this.rb_reboot.Text = "重新启动";
this.rb_reboot.CheckedChanged += new System.EventHandler(this.rb_reboot_CheckedChanged);
//
// rb_shutdown
//
this.rb_shutdown.Checked = true;
this.rb_shutdown.Location = new System.Drawing.Point(24, 16);
this.rb_shutdown.Name = "rb_shutdown";
this.rb_shutdown.Size = new System.Drawing.Size(56, 24);
this.rb_shutdown.TabIndex = 0;
this.rb_shutdown.TabStop = true;
this.rb_shutdown.Text = "关机";
this.rb_shutdown.CheckedChanged += new System.EventHandler(this.rb_shutdown_CheckedChanged);
//
// btnOK
//
this.btnOK.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.btnOK.Location = new System.Drawing.Point(56, 192);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(72, 24);
this.btnOK.TabIndex = 6;
this.btnOK.Text = "开始";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
this.btnCancel.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.btnCancel.Location = new System.Drawing.Point(168, 192);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 24);
this.btnCancel.TabIndex = 7;
this.btnCancel.Text = "退出";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// notifyIcon1
//
this.notifyIcon1.ContextMenu = this.contextMenu1;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem5,
this.menuItem4,
this.menuItem1,
this.menuItem2,
this.menuItem3});
//
// menuItem5
//
this.menuItem5.Index = 0;
this.menuItem5.Text = "重启";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem4
//
this.menuItem4.Index = 1;
this.menuItem4.Text = "关机";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem1
//
this.menuItem1.Index = 2;
this.menuItem1.Text = "退出";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem2
//
this.menuItem2.Index = 3;
this.menuItem2.Text = "隐藏";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 4;
this.menuItem3.Text = "关于";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.ClientSize = new System.Drawing.Size(294, 252);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "定时关机重起v1.0";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private static void DoExitWin(int flg)
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );
ok = ExitWindowsEx( flg, 0 );
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.notifyIcon1.Visible=true;
DateTime shutdownTime=DateTime.Now;
txtTime.Text=System.DateTime.Now.ToString();
dtpDay1.Format = DateTimePickerFormat.Custom;
dtpDay1.CustomFormat = "yyyy-MM-d";
dtpTime1.Format = DateTimePickerFormat.Custom;
dtpTime1.CustomFormat = "HH:mm:ss";
}
private void timer1_Tick(object sender, System.EventArgs e)
{
txtTime.Text=System.DateTime.Now.ToString();
string CurrDate=System.DateTime.Today.ToShortDateString() ;
string CurrTime=System.DateTime.Now.ToLongTimeString();
if(CurrDate==dtpDay1.Value.ToShortDateString() && CurrTime==dtpTime1.Value.ToLongTimeString())
{
if(rb_reboot.Checked==true)
{
DoExitWin(EWX_REBOOT);
Application.Exit();
}
else
{
DoExitWin(EWX_SHUTDOWN);
Application.Exit();
}
}
}
private void btnOK_Click(object sender, System.EventArgs e)
{
this.Visible=false;
this.notifyIcon1.Visible=true;
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
notifyIcon1.Visible=false;
this.Close();
Application.Exit();
}
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
{
this.Visible=true;
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel=true;
this.Visible=false;
this.notifyIcon1.Visible=true;
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
this.Visible=false;
notifyIcon1.Visible=false;
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
notifyIcon1.Visible=false;
this.Close();
Application.Exit();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
Form2 frm2=new Form2();
frm2.Show();
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
DoExitWin(EWX_POWEROFF);
Application.Exit();
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
DoExitWin(EWX_REBOOT);
Application.Exit();
}
bool isNumberic(string s)
{
try
{
int.Parse(s);
return true;
}
catch
{
return false;
}
}
private void rb_reboot_CheckedChanged(object sender, System.EventArgs e)
{
if(rb_reboot.Checked==true)
{
rb_shutdown.Checked=false;
}
else
{
rb_shutdown.Checked=true;
}
}
private void rb_shutdown_CheckedChanged(object sender, System.EventArgs e)
{
if(rb_shutdown.Checked==true)
{
rb_reboot.Checked=false;
}
else
{
rb_reboot.Checked=true;
}
}
}
}