VB.NET完成關機和重新啟動。本站提示廣大學習愛好者:(VB.NET完成關機和重新啟動)文章只能為提供參考,不一定能成為您想要的結果。以下是VB.NET完成關機和重新啟動正文
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer
Const EWX_FORCE As Short = 4
Const EWX_LOGOFF As Short = 0
Const EWX_REBOOT As Short = 2
Const EWX_SHUTDOWN As Short = 1
Dim retval As Integer
'' 定義Esc按鍵
Const VK_ESCAPE As Short = &H1Bs
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
If Option1.Checked Then
'' 登記以後用戶
retval = ExitWindowsEx(EWX_FORCE, 0)
ElseIf Option2.Checked Then
'' 封閉計算機
retval = ExitWindowsEx(EWX_SHUTDOWN, 0)
ElseIf Option3.Checked Then
'' 重新啟動
retval = ExitWindowsEx(EWX_REBOOT, 0)
End If
End Sub
Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click
Me.Close()
End Sub
'' 按Esc鍵時,完畢使用順序
Private Sub Form1_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
If KeyAscii = VK_ESCAPE Then
Me.Close()
End If
If KeyAscii = 0 Then
eventArgs.Handled = True
End If
End Sub
本實例經過運用ExitWindowEx()API函數來到達關機和重新啟動的目的。在ExitWindowEx()函數中,參數uFlags指定要停止何種操作。在表86-2中列出了參數uFlags的值及其闡明。
表86-2 參數uFlags的值及闡明
常量名
值
闡明
EWX_FORCE
4
終止一切進程,包括沒有呼應的進程,並登記Windows
EWX_REBOOT
2
重新啟動零碎
EWX_SHUTDOWN
1
封閉零碎
EWX_LOGOFF
0
終止一切正在運轉的進程,並登記Windows