C#程序設計關閉Windows窗體淺析
C#程序設計關閉Windows窗體使許多軟件在有自動關機功能的同時有一個友好的用戶界面,現在用visual C#來編寫設計關閉Windows窗體的程序。并且讓你很快掌握Visual C#中對API的操作程序。
C#程序設計關閉Windows窗體具體的步驟
C#程序設計關閉Windows窗體1、界面的設計
新建一個標準工程,向工程中增加一個Windows窗體并向窗體中添加如下控件,并分別設置其屬性:
Windows窗體界面:
將窗體屬性中的caption設置為"關閉windows",名稱設置為"frmmain"。
C#程序設計關閉Windows窗體2. 在窗體類中引用API函數
API函數是構筑Windows應用程序的基石,是Windows編程的必備利器。每一種Windows應用程序開發工具都提供了間接或直接調用了Windows API函數的方法,或者是調用Windows API函數的接口,也就是說具備調用動態連接庫的能力。Visual C#和其它開發工具一樣也能夠調用動態鏈接庫的API函數。
在Visual C#中調用API的基本過程:
首先,在調用API之前,你必須先導入System.Runtime.InteropServices這個名稱空間。該名稱空間包含了在Visual C#中調用API的一些必要集合,具體的方法如下:
- using System.Runtime.InteropServices ;
- using System.Text ;
在導入了名稱空間后,我們要聲明在程序中所要用到的API函數。我們的程序主要是獲取系統的相關信息,所以用到的API函數都是返回系統信息的。先給出在Visual C#中聲明API的方法:
- [ DllImport("user32") ]
- public static extern long SetWindowPos(
- long hwnd , long hWndInsertAfter, long X ,
- long y , long cx, long cy, long wFlagslong) ;
其中,"DllImport"屬性用來從不可控代碼中調用一個方法,它指定了DLL的位置,該DLL中包含調用的外部方法;"kernel32"設定了類庫名;"public"指明函數的訪問類型為公有的;"static"修飾符聲明一個靜態元素,而該元素屬于類型本身而不是指定的對象;"extern"表示該方法將在工程外部執行,同時使用DllImport導入的方法必須使用"extern"修飾符;最后GetWindowsDirectory函數包含了兩個參數,一個為StringBuilder類型的,另一個為int類型的,該方法返回的內容存在于StringBuilder類型的參數中。同時,因為我們在這里使用到了StringBuilder類,所以在程序的開始處,我們還得添加System.Text這個名稱空間,方法同上。
聲明其它的在程序中所要用到的API函數:
- [ DllImport("user32") ]
- public static extern long ExitWindowsEx(long uFlags, long dwReserved ) ;
- [ DllImport("shell32") ]
- public static extern long ShellAbout(long uFlags, long dwReserved ) ;
C#程序設計關閉Windows窗體3. 增加窗體類的變量
- long dwReserved ;
- const int SHUTDOWN = 1 ;
- const int REBOOT = 2 ;
- const int LOGOFF = 0 ;
- long sh ;
- int counter , n ;
C#程序設計關閉Windows窗體4. 編寫窗體類的方法
在窗體的Load(事件過程中編寫如下代碼:
- private void frmmain1_Load(object sender, System.EventArgs e )
- {
- file://用系統時間初始化組件
- Time.Text = System.DateTime.Today.ToShortDateString( ) +
- " "+ System.DateTime.Today.ToLongTimeString( ) ;
- }
在組件Timer1的OnTimer事件過程中編寫如下代碼:
- / / 在組件Timer1的OnTimer事件過程中編寫如下代碼:
- private void Timer1_Timer(object sender, System.EventArgs e )
- {
- file://接收當前日期和時間,用于即時顯示
- string CurrDate=System.DateTime.Today.ToShortDateString( ) ;
- string CurrTime=System.DateTime.Today.ToShortTimeString( ) ;
- file://隨時檢測設定的關機日期和時間是否有效
- if( this.CheckBox1.Checked == true )
- {
- if(CurrDate== SetupDate.ToString( ) && CurrTime==SetupTime.ToString( ) )
- ColseComputer( ) ;
- }
- }
- private void ColseComputer( )
- { sh = ExitWindowsEx(SHUTDOWN, dwReserved) ; }
- private void button1_Click(object sender, System.EventArgs e )
- {
- Form2 frm=new Form2( ) ;
- frm.Show( ) ;
- }
- private void ButReOpen_Click(object sender, System.EventArgs e )
- { sh = ExitWindowsEx(REBOOT, dwReserved) ; }
- private void ButReLogin_Click(object sender, System.EventArgs e )
- { sh = ExitWindowsEx(LOGOFF, dwReserved) ; }
- private void ButCancle_Click(object sender, System.EventArgs e )
- { this.Close( ) ; }
- private void ButClose_Click_1(object sender, System.EventArgs e )
- { sh = ExitWindowsEx(REBOOT, dwReserved) ; }
C#程序設計關閉Windows窗體的基本內容就向你介紹到這里,希望對你了解和學習C#程序設計關閉Windows窗體有所幫助。
【編輯推薦】