扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
有很多的软件都实现了自动关机这样的功能。前一阵在Codeproject上看到一个这样的程序,整理了一下共享给大家。里面实现了关机,重启,注销等功能.
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace TimerComputerShutdown
{
/// <summary>
/// Summary description for TimerComputerShutdown.
/// </summary>
public class TimerComputerShutdown : System.Windows.Forms.Form
{
private System.Windows.Forms.Label labelWhatDoYouWantTheComputerToDo;
private System.Windows.Forms.ComboBox comboBox;
private System.Windows.Forms.Label labelSelectDateTimeToShutdownComputer;
private System.Windows.Forms.DateTimePicker dateTimePicker;
private System.Windows.Forms.Label labelDateTimeNow;
private System.Windows.Forms.Label labelDateTimeShutdown;
private System.Windows.Forms.Label labelNow;
private System.Windows.Forms.Label labelShutdown;
private System.Windows.Forms.Panel panelParent;
private System.Windows.Forms.Panel panelChild;
private System.Windows.Forms.CheckBox checkBox;
private System.Windows.Forms.Button buttonCancel;
private System.Windows.Forms.Button buttonInvoke;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Threading.ThreadStart threadStart;
private System.Threading.Thread thread;
[DllImport("user32.dll")] private static extern
bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")] private static extern
bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] private static extern
bool IsIconic(IntPtr hWnd);
private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;
public TimerComputerShutdown()
{
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(TimerComputerShutdown));
this.labelWhatDoYouWantTheComputerToDo = new System.Windows.Forms.Label();
this.comboBox = new System.Windows.Forms.ComboBox();
this.labelSelectDateTimeToShutdownComputer = new System.Windows.Forms.Label();
this.dateTimePicker = new System.Windows.Forms.DateTimePicker();
this.labelDateTimeNow = new System.Windows.Forms.Label();
this.labelDateTimeShutdown = new System.Windows.Forms.Label();
this.labelNow = new System.Windows.Forms.Label();
this.labelShutdown = new System.Windows.Forms.Label();
this.panelParent = new System.Windows.Forms.Panel();
this.panelChild = new System.Windows.Forms.Panel();
this.buttonCancel = new System.Windows.Forms.Button();
this.buttonInvoke = new System.Windows.Forms.Button();
this.checkBox = new System.Windows.Forms.CheckBox();
this.panelParent.SuspendLayout();
this.panelChild.SuspendLayout();
this.SuspendLayout();
//
// labelWhatDoYouWantTheComputerToDo
//
this.labelWhatDoYouWantTheComputerToDo.Location = new System.Drawing.Point(8, 8);
this.labelWhatDoYouWantTheComputerToDo.Name = "labelWhatDoYouWantTheComputerToDo";
this.labelWhatDoYouWantTheComputerToDo.Size = new System.Drawing.Size(216, 16);
this.labelWhatDoYouWantTheComputerToDo.TabIndex = 9;
this.labelWhatDoYouWantTheComputerToDo.Text = "选择任务:";
this.labelWhatDoYouWantTheComputerToDo.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// comboBox
//
this.comboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox.Items.AddRange(new object[] {
"关机",
"重启",
"待机",
"睡眠",
"注销"});
this.comboBox.Location = new System.Drawing.Point(184, 8);
this.comboBox.Name = "comboBox";
this.comboBox.Size = new System.Drawing.Size(112, 21);
this.comboBox.TabIndex = 0;
this.comboBox.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//
// labelSelectDateTimeToShutdownComputer
//
this.labelSelectDateTimeToShutdownComputer.Location = new System.Drawing.Point(8, 32);
this.labelSelectDateTimeToShutdownComputer.Name = "labelSelectDateTimeToShutdownComputer";
this.labelSelectDateTimeToShutdownComputer.Size = new System.Drawing.Size(144, 16);
this.labelSelectDateTimeToShutdownComputer.TabIndex = 7;
this.labelSelectDateTimeToShutdownComputer.Text = "选择任务时间: ";
this.labelSelectDateTimeToShutdownComputer.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// dateTimePicker
//
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者