Private Declare Function EXITWINDOWSEX LIB "User32" (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
'Defining the ESC button
Const vk_escape as short = & h1bs
Private submmand1_click (Byval Eventsender As System.Object, Byval Eventargs As System.EventArgs) Handles Command1.click
If Option1.checked Then
'Logout current user
Retval = exitwindowsex (EWX_FORCE, 0)
Elseif Option2.checked THEN
'Turn off the computer
Retval = exitwindowsex (EWX_SHUTDOWN, 0)
Elseif Option3.checked THEN
' Restart
Retval = EXITWINDOWSEX (EWX_REBOOT, 0)
END IF
End Sub
Private submmand2_click (byval eventsender as system.object, byval eventargs as system.eventargs) handles command2.click
Me.close ()
End Sub
End the application when you press the ESC button
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 kil
Me.close ()
END IF
IF keyascii = 0 THEN
Eventargs.handled = TRUE
END IF
End Sub This instance uses the EXITWINDOWEX () API function to achieve the purpose of shutdown and restart. In the exitwindowex () function, the parameter uflags specifies what operations to do. The value of the parameter uflags and its description are listed in Table 86-2. Table 86-2 Parameter UFLAGS value and description
Constant name
value
Description
EWX_FORCE 4 Terminate all processes, including the process without a response, and log out of Windows EWX_REBOOT 2 Restart System EWX_SHUTDOWN 1 Turn off the system EWX_LOGOFF 0 Terminate all the running processes and log out of Windows