函数参考


_Timer_SetTimer

创建具有指定超时值的计时器

#Include <Timers.au3>
_Timer_SetTimer($hWnd [, $iElapse = 250 [, $sTimerFunc = "" [, $iTimerID = -1]]])

参数

$hWnd 与计时器相关的窗口句柄.
这个窗口必须为调用线程所拥有
$iElapse [可选参数] 指定超时值,以毫秒为单位
$sTimerFunc [可选参数] 通知超时值流逝的函数名称
$iTimerID [可选参数] 指定计时器标识符.
如果 $iTimerID = -1 创建新的计时器
如果 $iTimerID 匹配现有的计时器, 则替换计时器
如果 $iTimerID = -1 and $sTimerFunc = "" 那么将使用计时器 WM_TIMER 事件

返回值

成功: 返回新计时器的整数标识
失败: 返回 0

注意/说明

The callback function is called with the following parameters:
$hWnd, $Msg, $iIDTimer, $dwTime

相关

_Timer_KillTimer, _Timer_KillAllTimers, _Timer_GetTimerID

详情参考

在MSDN中搜索


示例/演示


#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <Timers.au3>
#include <GuiStatusBar.au3>
#include <ProgressConstants.au3>

Global $iMemo, $hStatusBar, $progress, $percent = 0, $direction = 1

_Example_CallBack()

Func _Example_CallBack()
    Local $hGUI, $iTimerProgress, $btn_change, $iWait = 10, $btn_state
    Local $aParts[3] = [75, 330, -1]

    $hGUI = GUICreate("Timers Using CallBack Function(s)", 400, 320)
    $iMemo = GUICtrlCreateEdit("", 2, 32, 396, 226, BitOR($WS_HSCROLL, $WS_VSCROLL))
    GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
    $btn_state = GUICtrlCreateButton("Start Progress Bar", 70, 270, 100, 25)
    $btn_change = GUICtrlCreateButton("Change", 215, 270, 90, 25)
    GUICtrlSetState($btn_change, $GUI_DISABLE)
    $hStatusBar = _GUICtrlStatusBar_Create($hGUI, $aParts)
    _GUICtrlStatusBar_SetText($hStatusBar, "Timers")
    _GUICtrlStatusBar_SetText($hStatusBar, @TAB & @TAB & StringFormat("%02d:%02d:%02d", @HOUR, @MIN, @SEC), 2)
    $progress = GUICtrlCreateProgress(0, 0, -1, -1, $PBS_SMOOTH)
    GUICtrlSetColor($progress, 0xff0000)
    _GUICtrlStatusBar_EmbedControl($hStatusBar, 1, GUICtrlGetHandle($progress))
    GUISetState()

    _Timer_SetTimer($hGUI, 1000, "_UpdateStatusBarClock") ; 创建计时器

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $btn_state
                If GUICtrlRead($btn_state) = "Start Progress Bar" Then
                    $iTimerProgress = _Timer_SetTimer($hGUI, $iWait, "_UpdateProgressBar") ; 创建计时器
                    If @error Or $iTimerProgress = 0 Then ContinueLoop
                    GUICtrlSetData($btn_state, "Stop Progress Bar")
                    GUICtrlSetState($btn_change, $GUI_ENABLE)
                Else
                    GUICtrlSetState($btn_change, $GUI_DISABLE)
                    _Timer_KillTimer($hGUI, $iTimerProgress)
                    GUICtrlSetData($btn_state, "Start Progress Bar")
                EndIf

            Case $btn_change
                If $iWait = 10 Then
                    $iWait = 250
                Else
                    $iWait = 10
                EndIf
                MemoWrite("Timer for _UpdateProgressBar set at: " & $iWait & " milliseconds")
                $iTimerProgress = _Timer_SetTimer($hGUI, $iWait, "", $iTimerProgress) ; 用不同的间隔重用计时器
        EndSwitch
    WEnd
    ConsoleWrite("Killed All Timers? " & _Timer_KillAllTimers($hGUI) & @CRLF)
    GUIDelete()
EndFunc   ;==>_Example_CallBack

; 回调函数
Func _UpdateStatusBarClock($hWnd, $Msg, $iIDTimer, $dwTime)
    #forceref $hWnd, $Msg, $iIDTimer, $dwTime
    _GUICtrlStatusBar_SetText($hStatusBar, @TAB & @TAB & StringFormat("%02d:%02d:%02d", @HOUR, @MIN, @SEC), 2)
EndFunc   ;==>_UpdateStatusBarClock

; 回调函数
Func _UpdateProgressBar($hWnd, $Msg, $iIDTimer, $dwTime)
    #forceref $hWnd, $Msg, $iIDTimer, $dwTime
    $percent += 5 * $direction
    GUICtrlSetData($progress, $percent)
    If $percent = 100 Or $percent = 0 Then $direction *= -1
    If $percent = 100 Then
        GUICtrlSetColor($progress, 0xff0000)
    ElseIf $percent = 0 Then
        GUICtrlSetColor($progress, 0x0000ff)
    EndIf
EndFunc   ;==>_UpdateProgressBar

; 写入一行到 memo 控件
Func MemoWrite($sMessage)
    GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
EndFunc   ;==>MemoWrite