1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10 using System.Runtime.InteropServices;
11 using System.Threading;
12
13 namespace Shutdown_Windows_API
14 {
15 public partial class Form1 : Form
16
17
18 {
19 int num = 0;
20 int op = 0;
21 [StructLayout(LayoutKind.Sequential, Pack = 1)]
22 internal struct TokPriv1Luid
23 {
24 public int Count;
25 public long Luid;
26 public int Attr;
27
28 }
29 //锁屏
30 [DllImport("user32 ")]
31 public static extern bool LockWorkStation();
32 //关机/注销/重启
33 [DllImport("kernel32.dll", ExactSpelling = true)]
34 internal static extern IntPtr GetCurrentProcess();
35
36 [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
37 internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
38
39 [DllImport("advapi32.dll", SetLastError = true)]
40 internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
41
42 [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
43 internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
44 ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
45
46 [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
47 internal static extern bool ExitWindowsEx(int flg, int rea);
48
49 internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
50 internal const int TOKEN_QUERY = 0x00000008;
51 internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
52 internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
53 internal const int EWX_LOGOFF = 0x00000000;
54 internal const int EWX_SHUTDOWN = 0x00000001;
55 internal const int EWX_REBOOT = 0x00000002;
56 internal const int EWX_FORCE = 0x00000004;
57 internal const int EWX_POWEROFF = 0x00000008;
58 internal const int EWX_FORCEIFHUNG = 0x00000010;
59
60 public static Thread thread1;
61
62 static void DoExitWin(int flg)
63 {
64 bool ok;
65 TokPriv1Luid tp;
66 IntPtr hproc = GetCurrentProcess();
67 IntPtr htok = IntPtr.Zero;
68 ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
69 tp.Count = 1;
70 tp.Luid = 0;
71 tp.Attr = SE_PRIVILEGE_ENABLED;
72 ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
73 ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
74 ok = ExitWindowsEx(flg, 0);
75 }
76
77 public Form1()
78 {
79 InitializeComponent();
80
81
82 }
83 public void dos(int i)
84 {
85 switch(i)
86 {
87 case 1:
88 DoExitWin(EWX_LOGOFF);
89 System.Environment.Exit(0);
90 break;
91 case 2:
92 DoExitWin(EWX_SHUTDOWN);
93 System.Environment.Exit(0);
94 break;
95 case 3:
96 DoExitWin(EWX_REBOOT);
97 System.Environment.Exit(0);
98 break;
99 case 4:
100 LockWorkStation();
101 break;
102 case 5:
103 DoExitWin(EWX_POWEROFF);
104 System.Environment.Exit(0);
105 break;
106 default:
107 break;
108 }
109 }
110
111
112
113 private void label5_Click(object sender, EventArgs e)
114 {
115 System.Diagnostics.Process.Start("http://weibo.com/u/3805789078");
116
117 }
118
119 public void timer1_Tick(object sender, EventArgs e)
120 {
121
122 int z = Convert.ToInt32(textBox1.Text) * 3600 + Convert.ToInt32(textBox2.Text) * 60 + Convert.ToInt32(textBox3.Text);
123 num++;
124 int t = z - num;
125 int h = t / 3600;
126 int m = (t - 3600 * h) / 60;
127 int s = t - 3600 * h - 60 * m;
128 label9.Text =h.ToString()+":"+m.ToString()+":"+s.ToString();
129 switch(op)
130 {
131 case 1:
132 label7.Text = "注销";
133 break;
134 case 2:
135 label7.Text = "关机";
136 break;
137 case 3:
138 label7.Text = "重启";
139 break;
140 }
141 while(label9.Text == "0:0:0")
142 {
143 dos(op);
144 break;
145
146 }
147
148 }
149 private void button1_Click(object sender, EventArgs e)
150 {
151
152 DialogResult ret = MessageBox.Show(this,"真的要注销吗?","确定注销",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
153 if(ret == DialogResult.OK)
154 {
155 dos(1);
156 }
157 else
158 {
159 dos(6);
160 }
161
162 }
163
164 private void button2_Click(object sender, EventArgs e)
165 {
166 DialogResult ret = MessageBox.Show(this, "真的要关机吗?", "确定关机?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
167 if (ret == DialogResult.OK)
168 {
169 dos(2);
170 }
171 else
172 {
173 dos(6);
174 }
175 }
176
177 private void button3_Click(object sender, EventArgs e)
178 {
179 DialogResult ret = MessageBox.Show(this, "真的要重启吗?", "确定重启?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
180 if (ret == DialogResult.OK)
181 {
182 dos(3);
183 }
184 else
185 {
186 dos(6);
187 }
188 }
189
190 private void button4_Click(object sender, EventArgs e)
191 {
192 DialogResult ret = MessageBox.Show(this, "真的要锁屏吗?", "确定锁屏?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
193 if (ret == DialogResult.OK)
194 {
195 dos(4);
196 }
197 else
198 {
199 dos(6);
200 }
201 }
202
203 private void button5_Click(object sender, EventArgs e)
204 {
205 DialogResult ret = MessageBox.Show(this, "真的要强制关机吗?\n(可能会导致下次启动出现问题 不推荐)", "确定强制关机", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
206 if (ret == DialogResult.OK)
207 {
208 dos(5);
209 }
210 else
211 {
212 dos(6);
213 }
214 }
215
216 private void button10_Click(object sender, EventArgs e)
217 {
218 Form3 x = new Form3();
219 x.Show();
220 }
221
222 private void button6_Click(object sender, EventArgs e)
223 {
224 timer1.Enabled = true;
225 label6.Visible = true;
226 label7.Visible = true;
227 label8.Visible = true;
228 label9.Visible = true;
229 op = 1;
230 }
231
232 private void button7_Click(object sender, EventArgs e)
233 {
234 timer1.Enabled = true;
235 label6.Visible = true;
236 label7.Visible = true;
237 label8.Visible = true;
238 label9.Visible = true;
239 op = 2;
240 }
241
242 private void button8_Click(object sender, EventArgs e)
243 {
244 timer1.Enabled = true;
245 label6.Visible = true;
246 label7.Visible = true;
247 label8.Visible = true;
248 label9.Visible = true;
249 op = 3;
250 }
251
252 private void button9_Click(object sender, EventArgs e)
253 {
254 timer1.Enabled = false;
255 }
256
257 private void Form1_Load(object sender, EventArgs e)
258 {
259 InitialTray();
260 }
261
262 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
263 {
264 DialogResult ret = MessageBox.Show(this, "单击取消最小化到托盘", "真的要退出吗?(将取消定时计划)", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
265 if (ret == DialogResult.OK)
266 {
267
268 e.Cancel = false;
269 }
270 else
271 {
272 notifyIcon1.Visible = true;
273 this.Hide();
274 e.Cancel = true;
275 }
276 }
277 public void exit()
278 {
279 System.Environment.Exit(0);
280 }
281
282 private void InitialTray()
283 {
284 //this.Hide();
285 notifyIcon1.BalloonTipText = "正在后台运行";
286 notifyIcon1.Text = "关机小助手";
287 notifyIcon1.ShowBalloonTip(2000);
288
289 notifyIcon1.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon1_MouseClick);
290 MenuItem exit = new MenuItem("退出");
291 MenuItem show = new MenuItem("打开主面板");
292 exit.Click += new EventHandler(exit_Click);
293 show.Click += new EventHandler(show_event);
294 MenuItem[] childen = new MenuItem[] { show, exit };
295 notifyIcon1.ContextMenu = new ContextMenu(childen);
296 notifyIcon1.Visible = false;
297 //this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
298 }
299 private void exit_Click(object sender, EventArgs e)
300 {
301 //退出程序
302 System.Environment.Exit(0);
303 }
304 private void show_event(object sender, EventArgs e)
305 {
306 this.Visible = true;
307 notifyIcon1.Visible = false;
308 }
309 private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
310 {
311
312 //鼠标左键单击
313 if (e.Button == MouseButtons.Left)
314 {
315 this.Visible = true;
316 notifyIcon1.Visible = false;
317 }
318 }
319
320 }
321
322 }