二級(jí)VB輔導(dǎo):VB.NET實(shí)現(xiàn)關(guān)機(jī)和重新啟動(dòng)

字號(hào):

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
    ’ 注銷當(dāng)前用戶
    retval = ExitWindowsEx(EWX_FORCE, 0)
    ElseIf Option2.Checked Then
    ’ 關(guān)閉計(jì)算機(jī)
    retval = ExitWindowsEx(EWX_SHUTDOWN, 0)
    ElseIf Option3.Checked Then
    ’ 重新啟動(dòng)
    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鍵時(shí),結(jié)束應(yīng)用程序
    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
    本實(shí)例通過使用ExitWindowEx()API函數(shù)來達(dá)到關(guān)機(jī)和重新啟動(dòng)的目的。在ExitWindowEx()函數(shù)中,參數(shù)uFlags指定要進(jìn)行何種操作。在表86-2中列出了參數(shù)uFlags的值及其說明。
    常量名,值說明 :
    EWX_FORCE
    4
    終止所有進(jìn)程,包括沒有響應(yīng)的進(jìn)程,并注銷Windows
    EWX_REBOOT
    2
    重新啟動(dòng)系統(tǒng)
    EWX_SHUTDOWN
    1
    關(guān)閉系統(tǒng)
    EWX_LOGOFF
    0
    終止所有正在運(yùn)行的進(jìn)程,并注銷Windows
    Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer