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