如何检测系统何时关闭或使用AutoIt注销?
如果计算机在我的脚本仍在运行时关闭或注销,我需要运行一个函数。
答案 0 :(得分:3)
您需要处理消息 WM_QUERYENDSESSION
示例代码:
; Define Windows Message Codes
$WM_QUERYENDSESSION = 0x11
; Define Callback Parameter Codes
$ENDSESSION_CLOSEAPP = 0x00000001
$ENDSESSION_CRITICAL = 0x40000000
$ENDSESSION_LOGOFF = 0x80000000
; Register a Callback on the Message to the Function "onShutDown"
GUIRegisterMsg($WM_QUERYENDSESSION, "onShutDown")
; #FUNCTION# ========================================================
; Name...........: onShutDown Example Function
; Description ...: Application receives the WM_QUERYENDSESSION message
;
; Parameters ....:
; $hWndGUI - A handle to the window
; $MsgID - The WM_QUERYENDSESSION identifier
; $wParam - This parameter is reserved for future use
; $lParam - This parameter can be one or more of the following values.
; |0 - The system is shutting down or restarting
; (it is not possible to determine which event is occurring)
; |$ENDSESSION_CLOSEAPP - Application is using a ressource or file that needs to be freed
; |$ENDSESSION_CRITICAL - Application is forced to shut down
; |$ENDSESSION_LOGOFF - The user is logging off
;
; Return values .:
; |True - Allow to Shutdown
; |False - Prevent Shutdown
; Remarks .......: Applications should respect the user's intentions and return TRUE. By default any application returns **TRUE** for this message.
; If shutting down would corrupt the system or media that is being burned, the application can return **FALSE**.
; However, it is good practice to respect the user's actions.
;
Func onShutDown($hWndGUI, $MsgID, $wParam, $lParam)
Return True ; allow Shutdown ( or return "False" to prevent Shutdown )
EndFunc
答案 1 :(得分:2)
您可以通过假设PC正在关闭的方式实施OnAutoItExitRegister()
。
OnAutoItExitRegister("DetectShutdown")
GUICreate("My GUI") ; will create a dialog box that when displayed is centered
GUISetState(@SW_SHOW) ; will display an empty dialog box
; Run the GUI until the dialog is closed
While True
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then
OnAutoItExitUnRegister("DetectShutdown")
Exit
EndIf
WEnd
Func DetectShutdown()
MsgBox(0, "", "Shutdown Detected!")
EndFunc
答案 2 :(得分:0)
如何检测系统何时关闭或使用AutoIt注销?
根据Documentation - Function Reference - OnAutoItExitRegister()
:
可以使用
@exitMethod
检索退出模式。
还检测用户注销和Windows关闭。
如果计算机在我的脚本仍在运行时关闭或注销,我需要运行一个函数。
示例(对退出方法的确定和反应):
#include <AutoItConstants.au3>; @exitMethod constants.
Global Enum $EXIT_OK, _
$EXIT_ONAUTOITEXITREGISTER, _
$EXIT_HOTKEYSET
Global Const $g_iDelayMain = 250
Global Const $g_iDelayExit = 1000 * 5
Global Const $g_sKeyExit = '{ESC}'
Global Const $g_sConsole = '%s-%s-%s %s:%s:%s.%s - @exitCode = %i (%s) @exitMethod = %i (%s)\n'
Global Const $g_aExit = [ _
'OK', _
'OnAutoItExitRegister() failed', _
'HotKeySet() failed' _
]
Global Const $g_aMethod = [ _
'end of script reached', _
'Exit function executed', _
'systray exit function activated', _
'Windows logoff', _
'Windows shutdown' _
]
Global $g_bStateExit = False
Main()
Func Main()
If Not OnAutoItExitRegister('_ReportExit') Then Exit $EXIT_ONAUTOITEXITREGISTER
If Not HotKeySet($g_sKeyExit, 'SetStateExit') Then Exit $EXIT_HOTKEYSET
While Not $g_bStateExit
Sleep($g_iDelayMain)
WEnd
Exit $EXIT_OK
EndFunc
Func _ReportExit()
Local Const $sLine = StringFormat($g_sConsole, _
@YEAR, @MON, @MDAY, @HOUR, @MIN, @SEC, @MSEC, _
@exitCode, $g_aExit[@exitCode], _
@exitMethod, $g_aMethod[@exitMethod] _
)
ConsoleWrite($sLine)
Sleep($g_iDelayExit)
Switch @exitMethod
; Case $EXITCLOSE_NORMAL
; Your code here.
; Case $EXITCLOSE_BYEXIT
; Your code here.
; Case $EXITCLOSE_BYCLICK
; Your code here.
Case $EXITCLOSE_BYLOGOFF
; Your code here.
Case $EXITCLOSE_BYSHUTDOWN
; Your code here.
EndSwitch
EndFunc
Func SetStateExit()
$g_bStateExit = True
EndFunc