发一段settimer的实例
Global $t2, $t3 = 1
$Form1 = GUICreate("API定时器实例", 272, 139)
$Label1 = GUICtrlCreateLabel("00:00:00:00", 8, 8, 146, 17);时间
$Label2 = GUICtrlCreateLabel("0", 8, 56, 150, 17);+1
$Label3 = GUICtrlCreateLabel("", 8, 104, 148, 17);2的自乘
$Button1 = GUICtrlCreateButton("关闭定时器1", 168, 8, 89, 25, 0)
$Button2 = GUICtrlCreateButton("关闭定时器2", 168, 48, 89, 25, 0)
$Button3 = GUICtrlCreateButton("关闭定时器3", 168, 96, 89, 25, 0)
GUISetState(@SW_SHOW)
$Timer = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword");创建自定义函数Timer的回调,API定时器函数SetTimer需要
$TimerDLL = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 1000, "ptr", DllCallbackGetPtr($Timer));1000毫秒执行一次
$Timer2DLL = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 200, "ptr", DllCallbackGetPtr($Timer));200毫秒执行一次
$Timer3DLL = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 2000, "ptr", DllCallbackGetPtr($Timer));2000毫秒执行一次
While 1
Switch GUIGetMsg()
Case - 3
ExitLoop
Case $Button1
DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $TimerDLL[0]);关闭定时器
Case $Button2
DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $Timer2DLL[0])
Case $Button3
DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $Timer3DLL[0])
EndSwitch
WEnd
GUIDelete()
DllCallbackFree($Timer);关闭回调
Func Timer($hWnd, $uiMsg, $idEvent, $dwTime)
Switch $idEvent;根据定时器ID来进行操作
Case $TimerDLL[0]
GUICtrlSetData($Label1, @HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC);更新时间
Case $Timer2DLL[0]
$t2 += 1
GUICtrlSetData($Label2, $t2);更新+1
Case $Timer3DLL[0]
$t3 *= 2
GUICtrlSetData($Label3, $t3);更新2的自乘
EndSwitch
EndFunc
|