C#关机小程序

这是我自己用所学习的C#  做的一个关机小程序,功能不多,只是为了练手下面是运行的效果图:



源码:
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.Windows.Forms; 9 10 11 /* 12 * 13 * 整理:张晓天

 

 14  * Q Q:977602650
 15  * 日期:2012-03-29
 16  * 
 17  */
 18 namespace ExitComputer
 19 {
 20     public partial class Form1 : Form
 21     {
 22         int goTime = 0;//程序运行时间
 23         string cmd = null;//写入shutdown的命令
 24         int countTime = 0;//到程序执行的时间
 25         public Form1()
 26         {
 27             InitializeComponent();
 28         }
 29 
 30         /// <summary>
 31         /// 窗体加载事件
 32         /// </summary>
 33         /// <param name="sender"></param>
 34         /// <param name="e"></param>
 35         private void Form1_Load(object sender, EventArgs e)
 36         {
 37             nfiPic.Icon = this.Icon;//指定系统的图标
 38 
 39             label1.Text = DateTime.Now.ToString("yyyy年 MM月 dd日 HH :mm :ss");//系统时间
 40             nudHour.Maximum = 23;
 41             nudMinutes.Maximum = 59;
 42             nudSecond.Maximum = 59;//指定控件的最大值
 43             cboMonth.Text = DateTime.Now.Month.ToString();
 44             cboDay.Text = DateTime.Now.Day.ToString();
 45             nudHour.Value = DateTime.Now.Hour;
 46             nudMinutes.Value = DateTime.Now.Minute;
 47             nudSecond.Value = DateTime.Now.Second;//指定控件的值
 48             //指定时间控件发生的频率
 49             tmrTime.Interval = 1000;
 50             tmrTime.Enabled = true;
 51             //为月和日赋值
 52             for (int i = 1; i < 13; i++)
 53             {
 54                 cboMonth.Items.Add(i);
 55             }
 56 
 57             for (int i = 1; i < 32; i++)
 58             {
 59                 cboDay.Items.Add(i);
 60             }
 61             //指定不可用的控件
 62             cboMonth.Enabled = false;
 63             cboDay.Enabled = false;
 64             nudHour.Enabled = false;
 65             nudMinutes.Enabled = false;
 66             nudSecond.Enabled = false;
 67             btnExit.Enabled = false;
 68 
 69         }
 70 
 71         //定时的属性改写时事件
 72         private void chkTiming_CheckedChanged(object sender, EventArgs e)
 73         {
 74             if (chkTiming.Checked == true)
 75             {
 76                 cboMonth.Enabled = true;
 77                 cboDay.Enabled = true;
 78                 nudHour.Enabled = true;
 79                 nudMinutes.Enabled = true;
 80                 nudSecond.Enabled = true;
 81 
 82             }
 83             else
 84             {
 85                 cboMonth.Enabled = false;
 86                 cboDay.Enabled = false;
 87                 nudHour.Enabled = false;
 88                 nudMinutes.Enabled = false;
 89                 nudSecond.Enabled = false;
 90 
 91             }
 92 
 93         }
 94 
 95         /// <summary>
 96         /// 显示窗体
 97         /// </summary>
 98         private void windowShow()
 99         {
100             this.Show();
101             this.ShowInTaskbar = true;
102             this.WindowState = FormWindowState.Normal;
103         }
104 
105         //重写事件实现气泡提示
106         protected override void OnSizeChanged(EventArgs e)
107         {
108             base.OnSizeChanged(e);
109             if (WindowState == FormWindowState.Minimized)
110             {
111                 nfiPic.ShowBalloonTip(30);
112             }
113         }
114 
115         /// <summary>
116         /// 隐藏托盘
117         /// </summary>
118         private void windowHide()
119         {
120             this.Hide();
121             this.ShowInTaskbar = false;
122             nfiPic.Visible = true;
123             nfiPic.ShowBalloonTip(30);
124         }
125 
126         //最小化窗体时的事件
127         private void Form1_Resize(object sender, EventArgs e)
128         {
129             if (this.WindowState == FormWindowState.Minimized)
130 
131                 this.windowHide();
132         }
133 
134         //鼠标双击托盘图标时的事件
135         private void nfiPic_MouseDoubleClick(object sender, MouseEventArgs e)
136         {
137             this.windowShow();
138         }
139 
140         //保证选择的月份是正确的
141         private void cboMonth_TextChanged(object sender, EventArgs e)
142         {
143             try
144             {
145                 int temp1 = int.Parse(cboMonth.Text);
146                 int temp = int.Parse(cboDay.Text);
147                 if (temp1 < 1 || temp1 > 12)
148                 {
149                     cboMonth.Text = DateTime.Now.Month.ToString();
150                 }
151 
152                 if (cboMonth.Text == "2")
153                 {
154                     if (DateTime.IsLeapYear(DateTime.Now.Year) == true)//判断今年是否为闰年
155                     {
156                         if (temp > 28)
157                         {
158                             cboDay.Text = DateTime.Now.Day.ToString();
159                         }
160                     }
161                     else if (temp > 29)
162                     {
163                         cboDay.Text = DateTime.Now.Day.ToString();
164                     }
165                 }
166                 else if (cboMonth.Text == "4" || cboMonth.Text == "6" || cboMonth.Text == "9" || cboMonth.Text == "11")
167                 {
168                     if (temp > 30)
169                     {
170                         cboDay.Text = DateTime.Now.Day.ToString();
171                     }
172                 }
173             }
174             catch (Exception)
175             {
176 
177                 cboMonth.Text = DateTime.Now.Month.ToString();
178             }
179 
180         }
181 
182         //保证选择的天数是正确的
183         private void cboDay_TextChanged(object sender, EventArgs e)
184         {
185             try
186             {
187                 int temp = int.Parse(cboDay.Text);
188                 if (temp < 1 || temp > 31)
189                 {
190                     cboDay.Text = DateTime.Now.Day.ToString();
191                 }
192                 //判断月份
193                 if (cboMonth.Text == "2")
194                 {
195                     if (DateTime.IsLeapYear(DateTime.Now.Year) == true)//判断今年是否为闰年
196                     {
197                         if (temp > 28)
198                         {
199                             cboDay.Text = DateTime.Now.Day.ToString();
200                         }
201                     }
202                     else if (temp > 29)
203                     {
204                         cboDay.Text = DateTime.Now.Day.ToString();
205                     }
206                 }
207                 else if (cboMonth.Text == "4" || cboMonth.Text == "6" || cboMonth.Text == "9" || cboMonth.Text == "11")
208                 {
209                     if (temp > 30)
210                     {
211                         cboDay.Text = DateTime.Now.Day.ToString();
212                     }
213                 }
214             }
215             catch (Exception)
216             {
217 
218                 cboDay.Text = DateTime.Now.Day.ToString();
219             }
220         }
221 
222         //程序执行时给CountTime赋的值
223         private bool timeControl()
224         {
225             if (chkTiming.Checked == true)
226             {
227                 DateTime startTime = Convert.ToDateTime(DateTime.Now.Year.ToString() + "/" + cboMonth.Text + "/" +
228                  cboDay.Text + " " + nudHour.Value.ToString() + ":" + nudMinutes.Value.ToString() + ":" + nudSecond.Value.ToString());
229                 TimeSpan endtime = startTime - DateTime.Now;
230                 countTime = endtime.Days * 86400 + endtime.Hours * 3600 + endtime.Minutes * 60 + endtime.Seconds;
231             }
232             if (countTime < 0)
233             {
234 
235                 MessageBox.Show("对不起!您选择的时间有误!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
236                 //让窗体回到初始化时的状态
237                 chkTiming.Enabled = true;
238                 btnLogout.Enabled = true;
239                 btnOver.Enabled = true;
240                 btnAgain.Enabled = true;
241                 btnExit.Enabled = false;
242                 tmrTime.Enabled = false;
243                 return false;
244             }
245             tmrTime.Enabled = true;
246             return true;
247         }
248 
249         //时间控件每次发生的事件
250         private void tmrTime_Tick(object sender, EventArgs e)
251         {
252             label1.Text = DateTime.Now.ToString("yyyy年 MM月 dd日 HH :mm :ss");
253             if (cmd != null)
254             {
255                 goTime += 1;
256                 if (countTime - goTime + 5 < 0)
257                 {
258                     txtRemind.Text = "0";
259                     cmd = null;
260                 }
261                 else
262                 {
263                     //在提示文本中显示的内容
264                     int temp = countTime - goTime + 1;
265                     txtRemind.Text = temp / 86400 + "" + temp % 86400 / 3600 + "" + temp % 3600 / 60 + "" + temp % 60 + "";
266                     if (countTime - goTime + 1 == 0)  //判断时间是否到了
267                     {
268                         System.Diagnostics.ProcessStartInfo p1 = new System.Diagnostics.ProcessStartInfo("shutdown.exe", cmd);
269                         p1.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
270                         System.Diagnostics.Process.Start(p1);
271                         Application.ExitThread();
272                         Application.Exit();//程序结束
273                     }
274                 }
275             }
276         }
277 
278         /// <summary>
279         /// 定时关机事件
280         /// </summary>
281         /// <param name="sender"></param>
282         /// <param name="e"></param>
283         private void btnOver_Click(object sender, EventArgs e)
284         {
285             if (!timeControl()) return;
286             cmd = "-s -t 0";
287             lblRemind.Text = "剩余时间";
288             chkTiming.Checked = false;
289             chkTiming.Enabled = false;
290             btnExit.Enabled = true;
291             Allow();
292 
293         }
294 
295         /// <summary>
296         /// 重启事件
297         /// </summary>
298         /// <param name="sender"></param>
299         /// <param name="e"></param>
300         private void btnAgain_Click(object sender, EventArgs e)
301         {
302             if (!timeControl()) return;
303             cmd = "-r -t 0";
304             lblRemind.Text = "剩余时间";
305             chkTiming.Checked = false;
306             chkTiming.Enabled = false;
307             btnExit.Enabled = true;
308             Allow();
309         }
310 
311         private void btnLogout_Click(object sender, EventArgs e)
312         {
313             if (!timeControl()) return;
314             cmd = "-l";
315             lblRemind.Text = "剩余时间";
316             chkTiming.Checked = false;
317             chkTiming.Enabled = false;
318             btnExit.Enabled = true;
319             Allow();
320         }
321 
322         private void tsmiShow_Click(object sender, EventArgs e)
323         {
324             this.windowShow();
325         }
326 
327         private void tsmiExit_Click(object sender, EventArgs e)
328         {
329             this.Dispose();
330         }
331 
332         /// <summary>
333         /// 单击取消发生的事件
334         /// </summary>
335         /// <param name="sender"></param>
336         /// <param name="e"></param>
337         private void btnExit_Click(object sender, EventArgs e)
338         {
339             btnLogout.Enabled = true;
340             btnOver.Enabled = true;
341             btnAgain.Enabled = true;
342             btnExit.Enabled = false;
343             cmd = null;
344             goTime = 0;
345             countTime = 0;
346             txtRemind.Text = "0";
347             lblRemind.Text = "关机提示";
348             chkTiming.Enabled = true;
349             MessageBox.Show("任务已被成功取消", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
350 
351         }
352 
353 
354         /// <summary>
355         /// 提醒事件
356         /// </summary>
357         public void Allow()
358         {
359             DialogResult result;
360             result = MessageBox.Show("程序将自动最小化到托盘", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
361             if (result == DialogResult.Yes)
362             {
363                 this.windowHide();
364             }
365         }
366 
367     }
368 }



源码打包下载:这里
posted @ 2012-09-18 10:58  Java_Swing  阅读(393)  评论(0编辑  收藏  举报