分享
 
 
 

半夜听到黄家驹的声音, 忽醒,一身冷汗,哦,原来是计算机忘了关,还在播放Beyond演唱会.于是做了个较简单的自动关机程序 :)

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

(一).说明

我平时喜欢听几首歌再关机睡觉,夏天还好,冬天光着身子去关机要冻个半死 :(,因此为这事做了个最简单的自动关机程序,读者如果用得着,就下载用一下吧

最近用这个程序爽多了

. 基于以下原因:

1. 睡觉之前不用顾虑: "

哎呀,计算机还开着,等下还得去关掉"

2.省电. 一天两天忘了关机交电费还撑的住,如果长期下去,一大笔电费咋交得起啊.

就算有钱也不心甘交这冤枉钱,给鬼听啊.

3.夏天跑去关机还好,如果是冬天的话,要冻个半死

4.恐怖啊! 半夜听到<黄家驹>的声音, 忽醒,一身冷汗,原因是计算机忘了关,它还在播放Beyond演唱会

跟读者开了些玩笑.

(二).代码

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Runtime.InteropServices;

namespace ShutDownComputer

{

/// <summary>

/// Form1 的摘要说明。

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();

//

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码

//

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.label1 = new System.Windows.Forms.Label();

this.comboBox1 = new System.Windows.Forms.ComboBox();

this.label2 = new System.Windows.Forms.Label();

this.label3 = new System.Windows.Forms.Label();

this.timer1 = new System.Timers.Timer();

((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();

this.SuspendLayout();

//

// button1

//

this.button1.BackColor = System.Drawing.Color.Olive;

this.button1.Location = new System.Drawing.Point(88, 184);

this.button1.Name = "button1";

this.button1.TabIndex = 0;

this.button1.Text = "开始";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// label1

//

this.label1.Location = new System.Drawing.Point(24, 80);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(120, 23);

this.label1.TabIndex = 2;

this.label1.Text = "您想让你的计算机在";

//

// comboBox1

//

this.comboBox1.Items.AddRange(new object[] {

"10",

"20",

"30",

"40",

"50",

"60",

"70",

"80",

"90",

"100",

"110",

"120"});

this.comboBox1.Location = new System.Drawing.Point(64, 104);

this.comboBox1.Name = "comboBox1";

this.comboBox1.Size = new System.Drawing.Size(88, 20);

this.comboBox1.TabIndex = 3;

this.comboBox1.Text = "30";

//

// label2

//

this.label2.Location = new System.Drawing.Point(152, 112);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(104, 16);

this.label2.TabIndex = 4;

this.label2.Text = "分之后自动关机";

//

// label3

//

this.label3.Location = new System.Drawing.Point(32, 32);

this.label3.Name = "label3";

this.label3.TabIndex = 5;

//

// timer1

//

this.timer1.Interval = 1000;

this.timer1.SynchronizingObject = this;

this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));

this.ClientSize = new System.Drawing.Size(280, 238);

this.Controls.Add(this.label3);

this.Controls.Add(this.label2);

this.Controls.Add(this.comboBox1);

this.Controls.Add(this.label1);

this.Controls.Add(this.button1);

this.Name = "Form1";

this.Text = "Form1";

this.Load += new System.EventHandler(this.Form1_Load);

((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// 应用程序的主入口点。

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)

{

}

[StructLayout(LayoutKind.Sequential, Pack=1)]

internal struct TokPriv1Luid

{

public int Count;

public long Luid;

public int Attr;

}

[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;

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Label label1;

private System.Windows.Forms.ComboBox comboBox1;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.Label label3;

private System.Timers.Timer timer1;

internal const int EWX_FORCEIFHUNG = 0x00000010;

private 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 );

}

DateTime ShutDownItTime = new DateTime();

int status = 0; //0: 表示还没有开始计时关机; 1: 已经开始计时关机

private void button1_Click(object sender, System.EventArgs e)

{

if(status == 1)

{

this.timer1.Enabled = false;

status = 0;

this.button1.Text = "开始";

return;

}

int intTime = 0;

if(this.comboBox1.Text.Length == 0)

{

this.label3.Text = "请输入时间!";

return;

}

try

{

intTime = int.Parse(this.comboBox1.Text);

}

catch

{

this.label3.Text = "时间只能为整数,请重新输入!";

}

this.ShutDownItTime=DateTime.Now.AddMinutes(intTime);

this.timer1.Enabled = true;

status = 1;

this.button1.Text = "停止";

}

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

if(DateTime.Now >= this.ShutDownItTime)

{

this.label3.Text = "正在关机...";

DoExitWin(EWX_SHUTDOWN);

}

}

}

}

(三).源程序代码下载

http://www.cnblogs.com/Files/ChengKing/ShutDownComputer.rar

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有