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 }



源碼打包下載:這裏
相關文章
相關標籤/搜索