C# 定時關(guān)機(jī)小應(yīng)用(Winform),供大家參考,具體內(nèi)容如下
專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)北湖免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
一、簡述
記--使用winform實現(xiàn)的定時關(guān)機(jī)小應(yīng)用。通過執(zhí)行cmd命令實現(xiàn)的。(Visual Studio 2010旗艦版)
例子打包:鏈接
二、效果
三、工程結(jié)構(gòu)及布局視圖
四、源文件
Form1.cs文件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; namespace SetTime1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); lblNow.BackColor = Color.Gainsboro; } /// <summary> /// 窗體加載 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load(object sender, EventArgs e) { try { //顯示當(dāng)前時間 lblNow.Text = DateTime.Now.ToString("yyyy年MM月dd日hh時mm分ss秒"); Timer timer = new Timer(); timer.Tick += new EventHandler(this.timer_Tick); timer.Enabled = true; //不斷捕獲鼠標(biāo)位置 Timer timer1 = new Timer(); timer1.Tick += new EventHandler(this.timer1_Tick); timer1.Enabled = true; //初始化模式一 InitialModel1(); //初始化重啟模式 InitialRset(); //初始化模式2 InitialModel2(); } catch { } } /// <summary> /// 當(dāng)前時間 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer_Tick(object sender, EventArgs e)//當(dāng)前時間 { try { //在標(biāo)簽上實時顯示當(dāng)前時間 lblNow.Text = DateTime.Now.ToString("yyyy年MM月dd日HH時mm分ss秒"); } catch{} } /// <summary> /// 窗體貼邊 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer1_Tick(object sender, EventArgs e)//窗體貼邊 { try { int ScreenWidth = Screen.PrimaryScreen.WorkingArea.Width; //獲取屏幕寬度 int ScreenRight = Screen.PrimaryScreen.WorkingArea.Right; //獲取屏幕高度 System.Drawing.Point mouse_pos = new Point(Cursor.Position.X, Cursor.Position.Y);//獲取鼠標(biāo)在屏幕的坐標(biāo)點 Rectangle Rects = new Rectangle(this.Left, this.Top, this.Left + this.Width, this.Top + this.Height);//存儲當(dāng)前窗體在屏幕的所在區(qū)域 if ((this.Top < 0) && Win32API.PtInRect(ref Rects, mouse_pos))//當(dāng)鼠標(biāo)在當(dāng)前窗體內(nèi),并且窗體的Top屬性小于0 {//如果窗體已經(jīng)上貼邊了并且鼠標(biāo)在窗體內(nèi)部,上貼邊展開 this.Top = 0;//設(shè)置窗體的Top屬性為0 } else if (this.Top > -5 && this.Top < 5 && !(Win32API.PtInRect(ref Rects, mouse_pos))) {//當(dāng)窗體的上邊框與屏幕的頂端的距離小于5,并且鼠標(biāo)不在窗體內(nèi)部時 this.Top = 5 - this.Height;//將窗體隱藏到屏幕的頂端,即上貼邊 } if ((this.Left >= ScreenWidth - 5) && Win32API.PtInRect(ref Rects, mouse_pos))//當(dāng)鼠標(biāo)在當(dāng)前窗體內(nèi),并且窗體的Left屬性小于ScreenWidth {//如果窗體已經(jīng)右貼邊了并且鼠標(biāo)在窗體內(nèi)部,右貼邊展開 this.Left = ScreenWidth - this.Width;//設(shè)置窗體的Left屬性為ScreenWidth } else if (this.Right >= ScreenWidth && !(Win32API.PtInRect(ref Rects, mouse_pos))) {//當(dāng)窗體的右邊框與屏幕的右端的距離小于+5時,并且鼠標(biāo)不在窗體內(nèi)部,右貼邊 this.Left = ScreenWidth - 5;//將窗體隱藏到屏幕的右端 } } catch { } } #region 無邊框窗體拖動 //-------------------無邊框窗體拖動--------------------------- Point mouseOff;//鼠標(biāo)移動位置變量 bool leftFlag;//標(biāo)志左鍵是否按下 //鼠標(biāo)按下 private void Form_MouseDown(object sender, MouseEventArgs e) { try { if (e.Button == MouseButtons.Left) { mouseOff = new Point(-e.X, -e.Y); //記下鼠標(biāo)移動的偏移量 leftFlag = true; //點擊左鍵按下時標(biāo)注為true; } } catch { } } //鼠標(biāo)移動 private void Form_MouseMove(object sender, MouseEventArgs e) { try { if (leftFlag) { Point mouseSet = Control.MousePosition;//獲取鼠標(biāo)的位置 mouseSet.Offset(mouseOff.X, mouseOff.Y); //設(shè)置移動后的位置 this.Location = mouseSet;//設(shè)置當(dāng)前窗體的位置 } } catch { } } //釋放鼠標(biāo) private void Form_MouseUp(object sender, MouseEventArgs e) { if (leftFlag) { leftFlag = false;//釋放鼠標(biāo)后標(biāo)注為false; } } [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { try { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } catch { } } //------------------------end 無邊框窗體拖動----------------------------------- #endregion /// <summary> /// 關(guān)閉窗口 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnClose_Click(object sender, EventArgs e) { this.Close(); }//關(guān)閉窗口 /// <summary> /// 最小化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnMin_Click(object sender, EventArgs e)//最小化 { //將窗體最小化 this.WindowState = FormWindowState.Minimized; } /// <summary> /// 初始化模式1 /// </summary> void InitialModel1()//初始化模式1 { try { int item = 0; //在小時下拉框添加(0~12)選項 while (item <= 12) { cbbHours1.Items.Add(item); cbbHours1.SelectedIndex = 0; item++; } //在分鐘下拉框添加(0~59)選項 for (item = 0; item <= 0x3b; item++) { cbbMins1.Items.Add(item); cbbMins1.SelectedIndex = 0; } //在秒下拉框添加(0~59)選項 for (item = 0; item <= 0x3b; item++) { cbbSeconds1.Items.Add(item); cbbSeconds1.SelectedIndex = 0; } } catch { } } /// <summary> /// 命令函數(shù) (通過黑窗口執(zhí)行命令) /// </summary> /// <param name="str">命令</param> private void Cmd(string str)//命令函數(shù) { try { using (Process process = new Process()) { process.StartInfo.FileName = "cmd.exe";//調(diào)用cmd.exe程序 process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true;//重定向標(biāo)準(zhǔn)輸入 process.StartInfo.RedirectStandardOutput = true;//重定向標(biāo)準(zhǔn)輸出 process.StartInfo.RedirectStandardError = true;//重定向標(biāo)準(zhǔn)出錯 process.StartInfo.CreateNoWindow = true;//不顯示黑窗口 process.Start();//開始調(diào)用執(zhí)行 process.StandardInput.WriteLine(str + "&exit");//標(biāo)準(zhǔn)輸入str + "&exit",相等于在cmd黑窗口輸入str + "&exit" process.StandardInput.AutoFlush = true;//刷新緩沖流,執(zhí)行緩沖區(qū)的命令,相當(dāng)于輸入命令之后回車執(zhí)行 process.WaitForExit();//等待退出 process.Close();//關(guān)閉進(jìn)程 } } catch { } } /// <summary> /// 模式1確定 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSure1_Click(object sender, EventArgs e)//模式1確定 { try { string strHour = cbbHours1.Items[cbbHours1.SelectedIndex].ToString();//小時 string strMin = cbbMins1.Items[cbbMins1.SelectedIndex].ToString();//分鐘 string strSec = cbbSeconds1.Items[cbbSeconds1.SelectedIndex].ToString();//秒數(shù) if (((cbbHours1.SelectedIndex != 0) || (cbbMins1.SelectedIndex != 0)) || (cbbSeconds1.SelectedIndex != 0)) { this.Cmd("shutdown -a");//取消之前的關(guān)機(jī)任務(wù) //組織關(guān)機(jī)命令 string strCmd = "shutdown -s -t " + (((((Convert.ToInt32(strHour) * 60) * 60) + (Convert.ToInt32(strMin) * 60)) + Convert.ToInt32(strSec))).ToString(); this.Cmd(strCmd);//調(diào)用cmd執(zhí)行命令 //彈出消息框告知用戶 MessageBox.Show("計算機(jī)將在" + strHour + "小時" + strMin + "分" + strSec + "秒后關(guān)機(jī)"); } else { MessageBox.Show("選擇無效!"); } } catch { } } /// <summary> /// 取消關(guān)機(jī)計劃 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCancel1_Click(object sender, EventArgs e)//取消關(guān)機(jī)計劃 { this.Cmd("shutdown -a");//調(diào)用cmd執(zhí)行取消關(guān)機(jī)命令 } /// <summary> /// 重啟模式 /// </summary> void InitialRset()//初始化重啟模式 { try { int item = 0; //在小時下拉框添加(0~12)選項 while (item <= 12) { cbbHoursRset.Items.Add(item); cbbHoursRset.SelectedIndex = 0; item++; } //在分鐘下拉框添加(0~59)選項 for (item = 0; item <= 0x3b; item++) { cbbMinsRset.Items.Add(item); cbbMinsRset.SelectedIndex = 0; } //在秒下拉框添加(0~59)選項 for (item = 0; item <= 0x3b; item++) { cbbSecondsRset.Items.Add(item); cbbSecondsRset.SelectedIndex = 0; } } catch { } } /// <summary> /// 確認(rèn)重啟 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSureRset_Click(object sender, EventArgs e)//重啟模式 { try { //獲取用戶選擇的時間 string strHour = cbbHoursRset.Items[cbbHoursRset.SelectedIndex].ToString();//小時 string strMin = cbbMinsRset.Items[cbbMinsRset.SelectedIndex].ToString();//分鐘 string strSec = cbbSecondsRset.Items[cbbSecondsRset.SelectedIndex].ToString();//秒 this.Cmd("shutdown -a");//取消之前的關(guān)機(jī)任務(wù) //根據(jù)用戶的選擇組織關(guān)機(jī)命令 string strCmd = "shutdown -r -t " + (((((Convert.ToInt32(strHour) * 60) * 60) + (Convert.ToInt32(strMin) * 60)) + Convert.ToInt32(strSec))).ToString(); this.Cmd(strCmd);//調(diào)用cmd執(zhí)行重啟命令 //彈出消息框告知用戶 MessageBox.Show("計算機(jī)將在" + strHour + "小時" + strMin + "分" + strSec + "秒后重啟"); } catch { } } /// <summary> /// 取消重啟 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnCancelRset_Click(object sender, EventArgs e)//取消重啟 { this.Cmd("shutdown -a");//取消關(guān)機(jī)任務(wù) } /// <summary> /// 初始化模式2 /// </summary> void InitialModel2()//初始化模式2 { try { int num; this.cbbMonths.Items.Clear();//清空月份下拉框 //在月份下拉框添加1~12 for (num = 1; num <= 12; num++) { cbbMonths.Items.Add(num); } //默認(rèn)選擇當(dāng)前月 cbbMonths.SelectedIndex = DateTime.Now.Month - 1; this.cbbHours2.Items.Clear();//清空小時下拉框 //在小時下拉框添加0~23 for (num = 0; num <= 0x17; num++) { this.cbbHours2.Items.Add(num); } //默認(rèn)選擇當(dāng)前小時 cbbHours2.SelectedIndex = DateTime.Now.Hour; this.cbbMins2.Items.Clear();//清空分鐘下拉框、 //在月份下拉框添加0~59 for (num = 0; num <= 0x3b; num++) { this.cbbMins2.Items.Add(num); } //默認(rèn)選擇當(dāng)前秒 cbbMins2.SelectedIndex = DateTime.Now.Minute; SetDay();//根據(jù)用戶選擇的月份選擇天數(shù)(月份的天數(shù)有差異,有潤平年之分) } catch { } } /// <summary> /// 設(shè)置模式2天數(shù) /// </summary> void SetDay()//設(shè)置模式2天數(shù) { try { int num; this.cbbDays.Items.Clear();//清空天數(shù)下拉框 switch ((cbbMonths.SelectedIndex + 1)) { case 1://1 3 5 7 8 10 12 月有31天 case 3: case 5: case 7: case 8: case 10: case 12: for (num = 1; num <= 31; num++) { cbbDays.Items.Add(num); } break; case 4://4 6 9 11月有30天 case 6: case 9: case 11: for (num = 1; num <= 30; num++) { cbbDays.Items.Add(num); } break; case 2: for (num = 1; num <= 28; num++)//2月至少有28天 { cbbDays.Items.Add(num); } //閏年 2月 有29天 if (((Convert.ToInt32(DateTime.Now.Year) % 400) == 0) || (((Convert.ToInt32(DateTime.Now.Year) % 4) == 0) && ((Convert.ToInt32(DateTime.Now.Year) % 100) != 0))) { cbbDays.Items.Add(0x1d);//再加1天 } break; default: break; } if (Convert.ToInt32(DateTime.Now.Day) > cbbDays.Items.Count) {//當(dāng)前天數(shù)大于可選天數(shù),設(shè)置為27 cbbDays.SelectedIndex = 27; } else { //默認(rèn)選為當(dāng)前天數(shù) cbbDays.SelectedIndex = Convert.ToInt32(DateTime.Now.Day) - 1; } } catch { } } /// <summary> /// 當(dāng)月數(shù)改變天數(shù)隨之改變 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cbbMonths_SelectedIndexChanged(object sender, EventArgs e)//當(dāng)月數(shù)改變天數(shù)隨之改變 { SetDay(); } /// <summary> /// 獲取設(shè)置模式2關(guān)機(jī)時間 /// </summary> /// <returns>設(shè)置模式2關(guān)機(jī)時間</returns> private DateTime GetDTime()//獲取設(shè)置模式2關(guān)機(jī)時間 { try { string strYear = Convert.ToString(DateTime.Now.Year); string strMouth = this.cbbMonths.Items[this.cbbMonths.SelectedIndex].ToString(); string strDay = this.cbbDays.Items[this.cbbDays.SelectedIndex].ToString(); string strHour = this.cbbHours2.Items[this.cbbHours2.SelectedIndex].ToString(); string strMin = this.cbbMins2.Items[this.cbbMins2.SelectedIndex].ToString(); //跨年處理 if ((DateTime.Now.Month == 12) && (this.cbbMonths.SelectedIndex == 0)) { strYear = (DateTime.Now.Year + 1).ToString(); } //返回設(shè)置的時間 return Convert.ToDateTime(strYear + "-" + strMouth + "-" + strDay + " " + strHour + ":" + strMin + ":00"); } catch { return DateTime.Now;//返回當(dāng)前時間 } } /// <summary> /// 計算模式2 獲取離關(guān)機(jī)還有多少秒 /// </summary> /// <param name="DateTime1">設(shè)置的關(guān)機(jī)時間</param> /// <param name="DateTime2">當(dāng)前時間</param> /// <returns></returns> private double DateDiff(DateTime DateTime1, DateTime DateTime2)//計算模式2秒數(shù) { try { if (DateTime1 <= DateTime2)//關(guān)機(jī)時間必須是大于當(dāng)前時間 { return 0.0; } //返回記錄關(guān)機(jī)的秒數(shù) return DateTime1.Subtract(DateTime2).Duration().TotalSeconds; } catch { return -1.0; } } /// <summary> /// 模式2確定 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSure2_Click(object sender, EventArgs e)//模式2確定 { try { this.Cmd("shutdown -a");//取消之前的關(guān)機(jī)任務(wù) DateTime dTime = this.GetDTime();//獲取關(guān)機(jī)時間 double sec = this.DateDiff(dTime, DateTime.Now);//獲取離關(guān)機(jī)還有多少秒 //關(guān)機(jī)時間分為2秒~3天 if ((sec > 2.0) && (sec < 259200.0)) { this.Cmd("shutdown -a");//取消之前的關(guān)機(jī)任務(wù) //執(zhí)行關(guān)機(jī)命令 this.Cmd("shutdown -s -t " + Convert.ToInt32(sec.ToString().Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)[0]).ToString()); //彈出消息框提示用戶 MessageBox.Show("計算機(jī)將于" + this.GetDTime().ToString() + "關(guān)機(jī)"); } else { MessageBox.Show("選擇無效?。。?); } } catch { } } /// <summary> /// 模式2取消 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCancel_Click(object sender, EventArgs e)// 模式2取消 { Cmd("shutdown -a");//取消關(guān)機(jī)任務(wù) } /// <summary> /// 當(dāng)選項卡為模式2時,重置時間 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)//當(dāng)選項卡為模式2時,重置時間 { if (tabControl1.SelectedIndex == 1) { InitialModel2(); } } } class Win32API //拖動接口 { [DllImport("User32.dll")] public static extern bool PtInRect(ref Rectangle r, Point p); } }
五、總結(jié)
1、cmd關(guān)機(jī)相關(guān)命令
取消任務(wù)命令:shutdown -a
重啟命令:shutdown -r -t sec (于sec秒后重啟)
關(guān)機(jī)命令:shutdown -s -t sec (于sec秒后關(guān)機(jī))
at 23:00 shutdown -s (在23:00執(zhí)行shutdown -s,即在23:00關(guān)機(jī)。)
at 某個時間 執(zhí)行某個動作/應(yīng)用 (win7測試管理員權(quán)限)
取消at計劃(1是ID)
2、更改應(yīng)用圖標(biāo)
3、窗體設(shè)置為無邊框樣式之后,就拖動不了了??梢酝ㄟ^監(jiān)聽鼠標(biāo)動作,在窗體范圍內(nèi),窗體跟隨鼠標(biāo)的移動。對于貼邊其實就是設(shè)置窗體的位置。
4、調(diào)用第三方程序
Process process = new Process()//創(chuàng)建進(jìn)程 process.StartInfo.FileName = exePath;//exePath:調(diào)用程序的路徑 process.Start();//開始調(diào)用執(zhí)行 process.WaitForExit();//等待退出 process.Close();//關(guān)閉進(jìn)程
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
本文名稱:C#實現(xiàn)定時關(guān)機(jī)小應(yīng)用
分享URL:http://www.rwnh.cn/article26/jcjjjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、外貿(mào)建站、軟件開發(fā)、ChatGPT、網(wǎng)站營銷、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)