提供 一個 更容易理解的 範例 官方網站的範例
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
; Set a HotKey
HotKeySet("x", "_Interrupt")
; Declare a flag
$fInterrupt = 0
$hGUI = GUICreate("Test", 500, 500)
$hButton_1 = GUICtrlCreateButton("Func One", 10, 10, 80, 30)
$hButton_2 = GUICtrlCreateButton("Func Two", 10, 50, 80, 30)
; Create a dummy control for the Accelerator to action when pressed
$hAccelInterupt = GUICtrlCreateDummy()
; Set an Accelerator key to action the dummy control
Dim $AccelKeys[1][2]=[ ["z", $hAccelInterupt] ]
GUISetAccelerators($AccelKeys)
GUISetState()
; Intercept Windows command messages with out own handler
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit
Case $hButton_1
_Func_1()
Case $hButton_2
_Func_2()
EndSwitch
WEnd
Func _Func_1()
; Make sure the flag is cleared
$fInterrupt = 0
For $i = 1 To 20
ConsoleWrite("-Func 1 Running" & @CRLF)
; Look for the flag
If $fInterrupt <> 0 Then
; The flag was set
Switch $fInterrupt
Case 1
ConsoleWrite("!Func 1 interrrupted by Func 2" & @CRLF)
Case 2
ConsoleWrite("!Func 1 interrrupted by HotKey" & @CRLF)
Case 3
ConsoleWrite("!Func 1 interrrupted by Accelerator" & @CRLF)
EndSwitch
Return
EndIf
Sleep(100)
Next
ConsoleWrite(">Func 1 Ended" & @CRLF)
EndFunc
Func _Func_2()
For $i = 1 To 3
ConsoleWrite("+Func 2 Running" & @CRLF)
Sleep(100)
Next
ConsoleWrite(">Func 2 Ended" & @CRLF)
EndFunc
Func _Interrupt()
; The HotKey was pressed so set the flag
$fInterrupt = 2
EndFunc
Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
; The Func 2 button was pressed so set the flag
If BitAND($wParam, 0x0000FFFF) = $hButton_2 Then $fInterrupt = 1
; The dummy control was actioned by the Accelerator key so set the flag
If BitAND($wParam, 0x0000FFFF) = $hAccelInterupt Then $fInterrupt = 3
Return $GUI_RUNDEFMSG
EndFunc ;==>_WM_COMMAND
|