找回密码
 加入
搜索
查看: 700|回复: 7

[AU3基础] 多计时器如何数组方式加载 - 已解决

[复制链接]
发表于 2022-11-9 09:34:07 | 显示全部楼层 |阅读模式
本帖最后由 rally 于 2022-11-11 18:35 编辑

想做个多计时器多任务的监控画面,查找学习了论坛各位大佬的一些思路代码。奈何还没理清个思路来做。用Timer建多个计时,用数组的方式也没处理好,还是对Timer没有深层的理解。这里贴出两个思路方式,请大佬帮提携指点一下新手菜鸟,不胜感激

;这个是取自论坛的计时器多线程,改了下数组方式处理,就没有计时效果,不知道错在哪里.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $Button[4],$Timer[4],$TimerDLL[4],$Label[4]
$Form1 = GUICreate("多线程实例", 272, 139, -1, -1)
For $i=1 to 3
   $Button[$i] = GUICtrlCreateButton("开始", 168, 8+40*($i-1), 89, 25, 0)
   $Label[$i]= GUICtrlCreateLabel("00:00:00", 8, 8+40*($i-1), 146, 17)
Next

GUISetState(@SW_SHOW)

While 1
   $msg = GUIGetMsg()
   If $msg= -3 Then
          Exit
   Else
                 For $i=1 to 3
                        If $msg = $Button[$i] Then
                        If GUICtrlRead($Button[$i]) =="开始" Then
                           $Timer1 = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")
                           $TimerDLL1 = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 1000, "ptr", DllCallbackGetPtr($Timer))
                           GUICtrlSetData($Button[$i],"关闭")
                           ExitLoop
                        Else
                           DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $TimerDLL[1])
                           DllCallbackFree($Timer1)
                           GUICtrlSetData($Button[$i],"开始")
                           ExitLoop
                        EndIf
                                  EndIf
             Next

   EndIf
WEnd

Func Timer($hWnd, $uiMsg, $idEvent, $dwTime)
      For $i=1 to 3
        If $idEvent = $TimerDLL11[$i][0] Then
                GUICtrlSetData($Label[$i], @HOUR & ":" & @MIN & ":" & @SEC)
                                ExitLoop
                 EndIf
          Next
EndFunc


;下面这个是论坛的思路方式,对建立多个不确定数量的计时任务,不太适用,或者我没找到处理思路

$Form1 = GUICreate("多线程实例", 272, 139, -1, -1)
$Label1 = GUICtrlCreateLabel("00:00:00", 8, 8, 146, 17)
$Label2 = GUICtrlCreateLabel("00:00:00", 8, 56, 150, 17)
$Label3 = GUICtrlCreateLabel("00:00:00", 8, 104, 148, 17)
$Button1 = GUICtrlCreateButton("开始", 168, 8, 89, 25, 0)
$Button2 = GUICtrlCreateButton("开始", 168, 48, 89, 25, 0)
$Button3 = GUICtrlCreateButton("开始", 168, 96, 89, 25, 0)
GUISetState(@SW_SHOW)
Global $t2, $t3 = 1

While 1
        Switch GUIGetMsg()
                       Case - 3
                        Exit
                                         Case $Button1
                                                If GUICtrlRead($Button1) =="开始" Then
                            $Timer = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")
                            $TimerDLL = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 1000, "ptr", DllCallbackGetPtr($Timer))
                                                        GUICtrlSetData($Button1,"关闭")
                                                 Else
                                                        GUICtrlSetData($Button1,"开始")
                            DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $TimerDLL)
                                                    DllCallbackFree($Timer)
                                                 EndIf

                Case $Button2
                                                If GUICtrlRead($Button2) =="开始" Then
                                               $Timer2 = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")
                               $Timer2DLL = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 200, "ptr", DllCallbackGetPtr($Timer2))
                                                           GUICtrlSetData($Button2,"关闭")
                                                        Else
                                                                   GUICtrlSetData($Button2,"开始")
                               DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $Timer2DLL)
                               DllCallbackFree($Timer2)
                                                 EndIf
                Case $Button3
                                         If GUICtrlRead($Button3) =="开始" Then
                              $Timer3 = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")
                              $Timer3DLL = DllCall("user32.dll", "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 500, "ptr", DllCallbackGetPtr($Timer3))
                                                          GUICtrlSetData($Button3,"关闭")
                                                Else
                                                          GUICtrlSetData($Button3,"开始")
                              DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $Timer3DLL)
                              DllCallbackFree($Timer3)
                          EndIf
        EndSwitch
WEnd
Func Timer($hWnd, $uiMsg, $idEvent, $dwTime)
        If $idEvent = $TimerDLL[0] Then
                GUICtrlSetData($Label1, @HOUR & ":" & @MIN & ":" & @SEC)
        ElseIf $idEvent = $Timer2DLL[0] Then
                           GUICtrlSetData($Label2, @HOUR & ":" & @MIN & ":" & @SEC)
        ElseIf $idEvent = $Timer3DLL[0] Then
;~                 $t3 *= 2
;~                 GUICtrlSetData($Label3, $t3)
                GUICtrlSetData($Label3, @HOUR & ":" & @MIN & ":" & @SEC)
        EndIf
EndFunc


;这个是我想做的计时任务界面(不一定是10个任务,可以改数组数量增减) ,  每个任务可以独立执行计时及停止 。下面只是实行单个的,多个就不行,没找打解决思路

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $label[20],$Button[20],$NO[50],$Customer[50],$SPEC[50],$Time[50],$Stime[50],$Dtime[50],$Status[50],$ACT[50]
Global $str =[7,"NO","客户","规格型号","烘烤时长(Hours)","进箱时间","倒计时","状态"]

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 1050, 500, 223, 108)

Local $a=24,$b=24
$Group1 = GUICtrlCreateGroup("", $a, $b, 1000, 737)
GUICtrlSetFont(-1, 14, 800, 0, "思源黑体 CN Medium")

$Label[1] = GUICtrlCreateLabel("",$a +20+ (1-1)*120,$b+50,80,30)
$Label[2] = GUICtrlCreateLabel("",$a -15+ (2-1)*100,$b+50,80,30)
$Label[3] = GUICtrlCreateLabel("",$a +10+ (3-1)*120,$b+50,80,30)
$Label[4] = GUICtrlCreateLabel("",$a +40+ (4-1)*120,$b+50,80,30,0x00001)
$Label[5] = GUICtrlCreateLabel("",$a +65+ (5-1)*120,$b+50,80,30)
$Label[6] = GUICtrlCreateLabel("",$a +140+ (6-1)*120,$b+50,80,30)
$Label[7] = GUICtrlCreateLabel("",$a +170+ (7-1)*120,$b+50,80,30)

For $i=1 to 7 step 1
   GUICtrlSetData($Label[$i] ,$str[$i])
   GUICtrlSetFont($Label[$i], 12, 800, 0, "宋体")
Next

$d=30
For $j=1 to 10 step 1
   $NO[$j]  = GUICtrlCreateLabel($j,$a +20,$b+50+$j*35,20,30)
   $Customer[$j] = GUICtrlCreateInput("",$a + 10 +$d,$b+50+$j*35,130,30,0x00001)
   $SPEC[$j] = GUICtrlCreateInput("",$a + 140  +$d,$b+50+$j*35,220,30,0x00001)
   $Time[$j] = GUICtrlCreateInput("",$a + 360  +$d,$b+50+$j*35,100,30,0x00001)
   $Stime[$j] = GUICtrlCreateInput("2022-11-02 17:32:56",$a + 460  +$d,$b+50+$j*35,200,30,0x00001)
   $Dtime[$j] = GUICtrlCreateInput("00:00:00",$a + 660  +$d,$b+50+$j*35,160,30,0x00001)
   $ACT[$j] = GUICtrlCreateButton("",$a + 840  +$d,$b+50+$j*35,80,30,0x00001)
   GUICtrlSetFont($NO[$j], 16, 800, 0, "宋体")
   GUICtrlSetFont($Customer[$j], 16, 800, 0, "宋体")
   GUICtrlSetFont($SPEC[$j], 16, 800, 0, "宋体")
   GUICtrlSetFont($Time[$j], 16, 800, 0, "宋体")
   GUICtrlSetFont($Stime[$j], 14, 800, 0, "宋体")
   GUICtrlSetFont($Dtime[$j], 16, 800, 0, "宋体")
   GUICtrlSetBkColor($Dtime[$j], 0x000000)              ;控件的背景色
   GUICtrlSetColor($Dtime[$j], 65280)
   GUICtrlSetFont($ACT[$j], 16, 800, 0, "宋体")
Next

GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###


While 1
        $nMsg = GUIGetMsg()
    If $nMsg = $GUI_EVENT_CLOSE Then Exit
        For $i=1 to 10 step 1
           If $nMsg = $ACT[$i] Then
                   If GUICtrlRead($ACT[$i]) = "已完成" Then
                        GUICtrlSetData($ACT[$i],"开始")
                    Else
                            GUICtrlSetData($ACT[$i],"进行中")
                         AdlibRegister(_TimerUp($i))
                        EndIf
                        ExitLoop
            EndIf
           If $nMsg = $GUI_EVENT_CLOSE Then
          ExitLoop
                  For $i=1 to 10
                      AdlibUnRegister(_TimerUp($i))
                  Next
                  Exit
           EndIf
    Next

;~         Switch $nMsg
;~                 Case $GUI_EVENT_CLOSE
;~                         Exit
;~         Case $ACT[1]
;~               GUICtrlSetBkColor($ACT[1],0x00ff00)
;~               GUICtrlSetData($ACT[1],"已完成")
;~         EndSwitch
WEnd

Func _TimerUp($j)
        $OnTime1 = DllCall("kernel32.dll", "int", "GetTickCount")
        while 1
                $OnTime2 = DllCall("kernel32.dll", "int", "GetTickCount")
                $Hours = Floor(Mod($OnTime2[0] - $OnTime1[0], 86400000) / 3600000)
                $Minutes = Floor(Mod(Mod($OnTime2[0] - $OnTime1[0], 86400000), 3600000) / 60000)
                $Seconds = Floor(Mod(Mod(Mod($OnTime2[0] - $OnTime1[0], 86400000), 3600000), 60000) / 1000)
         If StringLen($Hours)<2 Then $Hours="0"&$Hours
         If StringLen($Minutes)<2 Then $Minutes="0"&$Minutes
                 If StringLen($Seconds)<2 Then $Seconds="0"&$Seconds
                $time = $Hours & ":" & $Minutes & ":" & $Seconds
                $time1 = GUICtrlRead($Dtime[$j])
                If $time1 <> $time Then GUICtrlSetData($Dtime[$j], $time)

                $nMsg = GUIGetMsg()
           If $nMsg = $GUI_EVENT_CLOSE Then Exit
           For $i=1 to 10 step 1
             If $nMsg = $ACT[$i] Then
                    If GUICtrlRead($ACT[$i]) = "进行中" Then
                           GUICtrlSetData($ACT[$i],"暂停")
               ExitLoop
                    Else
                          GUICtrlSetData($ACT[$i],"进行中")
                       _TimerUp($i)
                           ExitLoop
                        EndIf
                 EndIf
          Next

   WEnd

EndFunc   ;==>_TimerUp




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2022-11-10 18:58:49 | 显示全部楼层
本帖最后由 afan 于 2022-11-10 19:18 编辑
rally 发表于 2022-11-10 18:45
感谢大大的支援 !   我想请教下 : $iIX = ($iMsg - $Button[1]) / 2 + 1   这个表达方式是怎么来的 ? ...

$iIX = ($iMsg - $Button[1]) / 2 + 1 这个根据你创建控件的方式得来的
因为你在一次循环中创建了两个控件,因此,按钮的ID是间隔的
如果在循环中仅创建按钮,它们的ID就是连续的,就可以写成 $iIX = ($iMsg - $Button[1]) + 1
包括消息循环里面的 Case $Button[1], $Button[2], $Button[3] 也是这个原因,修改后就可以直接
Case $Button[1] To $Button[3]
至于回调函数 _Timer 里面的写法,如果需要简写,可以使用以下方式
au3版本支持map的使用map,不支持的使用字典 或Assign+Eval方式,或者直接用个大点的数组代替map
在_Timer_SetTimer创建定时器时将其ID与序号一一对应声明
之后在 _Timer 里就可以直接查询ID的序号而直接处理

评分

参与人数 1金钱 +30 收起 理由
rally + 30 很给力!

查看全部评分

发表于 2022-11-10 12:28:53 | 显示全部楼层
#include <Timers.au3>

Global $hGUI, $Button[4], $Label[4], $aiTimerID[4]
$hGUI = GUICreate('定时器数组', 272, 139, -1, -1)
For $i = 1 To 3
        $Button[$i] = GUICtrlCreateButton('开始', 168, 8 + 40 * ($i - 1), 89, 25, 0)
        $Label[$i] = GUICtrlCreateLabel('00:00:00', 8, 8 + 40 * ($i - 1), 146, 17)
Next

GUISetState()

Local $iMsg, $iIX
While 1
        $iMsg = GUIGetMsg()
        Switch $iMsg
                Case -3
                        _Timer_KillAllTimers($hGUI)
                        GUIDelete()
                        Exit
                Case $Button[1], $Button[2], $Button[3]
                        $iIX = ($iMsg - $Button[1]) / 2 + 1
                        If GUICtrlRead($iMsg) == '开始' Then
                                GUICtrlSetData($iMsg, '停止')
                                $aiTimerID[$iIX] = _Timer_SetTimer($hGUI, 1000, '_Timer')
                        Else
                                GUICtrlSetData($iMsg, '开始')
                                _Timer_KillTimer($hGUI, $aiTimerID[$iIX])
                                $aiTimerID[$iIX] = 0
                        EndIf

        EndSwitch
WEnd

Func _Timer($hWnd, $iMsg, $iIDTimer, $iTime)
        Switch $iIDTimer
                Case $aiTimerID[1]
                        GUICtrlSetData($Label[1], @HOUR & ':' & @MIN & ':' & @SEC)
                Case $aiTimerID[2]
                        GUICtrlSetData($Label[2], @HOUR & ':' & @MIN & ':' & @SEC)
                Case $aiTimerID[3]
                        GUICtrlSetData($Label[3], @HOUR & ':' & @MIN & ':' & @SEC)
        EndSwitch
EndFunc   ;==>_Timer
 楼主| 发表于 2022-11-10 18:45:00 | 显示全部楼层

感谢大大的支援 !   我想请教下 : $iIX = ($iMsg - $Button[1]) / 2 + 1   这个表达方式是怎么来的 ?我不明白意思,还请大大指点一下。
另外,如果我用阵列建n 个Button 。 Case $Button[1], $Button[2], $Button[3]....... 这个有没有好的表达方法?
Func _Timer($hWnd, $iMsg, $iIDTimer, $iTime)
        Switch $iIDTimer
                Case $aiTimerID[1]
                Case $aiTimerID[2]
                Case $aiTimerID[3]
                ........如果有n个(不确定的个数) 有没有变量表示方法 ?
 楼主| 发表于 2022-11-11 15:05:13 | 显示全部楼层
afan 发表于 2022-11-10 18:58
$iIX = ($iMsg - $Button[1]) / 2 + 1 这个根据你创建控件的方式得来的
因为你在一次循环中创建了两个控 ...

非常谢谢大大解惑 ,这块我再根据大大的指导学习一下 。每次遇到难题都是大大解围 &#128077;
 楼主| 发表于 2022-11-11 17:12:36 | 显示全部楼层

大大 ,再帮看下我这样写倒计时,为什么每次点一个按钮开始,就会初始化其他正在计时的?

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Timers.au3>

Global $hGUI, $Button[4], $Label[4], $aiTimerID[4]
$hGUI = GUICreate('定时器数组', 272, 139, -1, -1)
For $i = 1 To 3
        $Button[$i] = GUICtrlCreateButton('开始', 168, 8 + 40 * ($i - 1), 89, 25, 0)
        $Label[$i] = GUICtrlCreateLabel('00:00:00', 8, 8 + 40 * ($i - 1), 146, 17)
Next

GUISetState()

Local $iMsg, $iIX
While 1
        $iMsg = GUIGetMsg()
        Switch $iMsg
                Case -3
                        _Timer_KillAllTimers($hGUI)
                        GUIDelete()
                        Exit
                                         Case $Button[1], $Button[2], $Button[3]
                                                $OnTime1 = DllCall("kernel32.dll", "int", "GetTickCount")
                        $iIX = ($iMsg - $Button[1]) / 2 + 1
                        If GUICtrlRead($iMsg) == '开始' Then
                                GUICtrlSetData($iMsg, '停止')
                                $aiTimerID[$iIX] = _Timer_SetTimer($hGUI, 1000, '_Timer')
                        Else
                                GUICtrlSetData($iMsg, '开始')
                                _Timer_KillTimer($hGUI, $aiTimerID[$iIX])
                                $aiTimerID[$iIX] = 0
                        EndIf

        EndSwitch
WEnd


Func _Timer($hWnd, $iMsg, $iIDTimer, $iTime)
        Switch $iIDTimer
                Case $aiTimerID[1]
                   $OnTime2 = DllCall("kernel32.dll", "int", "GetTickCount")
                   $Hours = Floor(Mod($OnTime2[0] - $OnTime1[0], 86400000) / 3600000)
                   $Minutes = Floor(Mod(Mod($OnTime2[0] - $OnTime1[0], 86400000), 3600000) / 60000)
                   $Seconds = Floor(Mod(Mod(Mod($OnTime2[0] - $OnTime1[0], 86400000), 3600000), 60000) / 1000)
           If StringLen($Hours)<2 Then $Hours="0"&$Hours
           If StringLen($Minutes)<2 Then $Minutes="0"&$Minutes
                   If StringLen($Seconds)<2 Then $Seconds="0"&$Seconds
                   $time0 = $Hours & ":" & $Minutes & ":" & $Seconds
                   $time1 = GUICtrlRead($Label[1])
                   ConsoleWrite($time1 & @CRLF)
                   If $time0<>$time1 Then GUICtrlSetData($Label[1], $time0)
;~            GUICtrlSetData($Label[1], @HOUR & ':' & @MIN & ':' & @SEC)


                 Case $aiTimerID[2]
                   $OnTime3 = DllCall("kernel32.dll", "int", "GetTickCount")
                   $Hours = Floor(Mod($OnTime3[0] - $OnTime1[0], 86400000) / 3600000)
                   $Minutes = Floor(Mod(Mod($OnTime3[0] - $OnTime1[0], 86400000), 3600000) / 60000)
                   $Seconds = Floor(Mod(Mod(Mod($OnTime3[0] - $OnTime1[0], 86400000), 3600000), 60000) / 1000)
           If StringLen($Hours)<2 Then $Hours="0"&$Hours
           If StringLen($Minutes)<2 Then $Minutes="0"&$Minutes
                   If StringLen($Seconds)<2 Then $Seconds="0"&$Seconds
                   $time2 = $Hours & ":" & $Minutes & ":" & $Seconds
                   $time3 = GUICtrlRead($Label[2])
                   If $time2<>$time3 Then GUICtrlSetData($Label[2], $time2)

                 Case $aiTimerID[3]
                   $OnTime4 = DllCall("kernel32.dll", "int", "GetTickCount")
                   $Hours = Floor(Mod($OnTime4[0] - $OnTime1[0], 86400000) / 3600000)
                   $Minutes = Floor(Mod(Mod($OnTime4[0] - $OnTime1[0], 86400000), 3600000) / 60000)
                   $Seconds = Floor(Mod(Mod(Mod($OnTime4[0] - $OnTime1[0], 86400000), 3600000), 60000) / 1000)
           If StringLen($Hours)<2 Then $Hours="0"&$Hours
           If StringLen($Minutes)<2 Then $Minutes="0"&$Minutes
                   If StringLen($Seconds)<2 Then $Seconds="0"&$Seconds
                   $time4 = $Hours & ":" & $Minutes & ":" & $Seconds
                   $time5 = GUICtrlRead($Label[3])
                   If $time4<>$time5 Then GUICtrlSetData($Label[3], $time4)
                 EndSwitch

EndFunc   ;==>_Timer
发表于 2022-11-11 17:30:22 | 显示全部楼层
本帖最后由 afan 于 2022-11-11 17:54 编辑
rally 发表于 2022-11-11 17:12
大大 ,再帮看下我这样写倒计时,为什么每次点一个按钮开始,就会初始化其他正在计时的?

#include

因为你每次点击 开始 计时,就会重置 GetTickCount 的值,而你的计时都是基于该值的,不同步才怪~
另外 GetTickCount 不需要这样DllCall,_Timer 函数里面的 $iTime 就是该数据。
建议:直接使用时间戳就好了,TimerInit()、 TimerDiff()

#include <Timers.au3>
#include <Date.au3>

Global $hGUI, $Button[4], $Label[4], $aiTimerID[4], $aTimer[4][2]
$hGUI = GUICreate('定时器数组', 272, 139, -1, -1)
For $i = 1 To 3
        $Button[$i] = GUICtrlCreateButton('开始', 168, 8 + 40 * ($i - 1), 89, 25, 0)
        $Label[$i] = GUICtrlCreateLabel('00:00:00', 8, 8 + 40 * ($i - 1), 146, 17)
Next

GUISetState()

Local $iMsg, $iIX
While 1
        $iMsg = GUIGetMsg()
        Switch $iMsg
                Case -3
                        _Timer_KillAllTimers($hGUI)
                        GUIDelete()
                        Exit
                Case $Button[1], $Button[2], $Button[3]
                        $iIX = ($iMsg - $Button[1]) / 2 + 1
                        $aTimer[$iIX][0] = TimerInit()
                        If GUICtrlRead($iMsg) == '开始' Then
                                GUICtrlSetData($iMsg, '停止')
                                $aiTimerID[$iIX] = _Timer_SetTimer($hGUI, 1000, '_Timer')
                        Else
                                GUICtrlSetData($iMsg, '开始')
                                _Timer_KillTimer($hGUI, $aiTimerID[$iIX])
                                $aiTimerID[$iIX] = 0
                        EndIf
        EndSwitch
WEnd

Func _Timer($hWnd, $iMsg, $iIDTimer, $iTime)
        Switch $iIDTimer
                Case $aiTimerID[1]
                        __Timing(1)
                Case $aiTimerID[2]
                        __Timing(2)
                Case $aiTimerID[3]
                        __Timing(3)
        EndSwitch
EndFunc   ;==>_Timer

Func __Timing($iIX)
        Local $timeDiff = TimerDiff($aTimer[$iIX][0])
        Local $Hours, $Minutes, $Seconds
        _TicksToTime($timeDiff, $Hours, $Minutes, $Seconds)
        Local $time0 = StringFormat('%02d:%02d:%02d', $Hours, $Minutes, $Seconds)
        If $time0 <> $aTimer[$iIX][1] Then GUICtrlSetData($Label[$iIX], $time0)
        $aTimer[$iIX][1] = $time0
EndFunc   ;==>__Timing
 楼主| 发表于 2022-11-11 18:34:02 | 显示全部楼层
afan 发表于 2022-11-11 17:30
因为你每次点击 开始 计时,就会重置 GetTickCount 的值,而你的计时都是基于该值的,不同步才怪~
另外  ...

感觉大大您这样写高级多了,我这样的写法太冗余了

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <array.au3>
#include <Timers.au3>

Global $hGUI, $Button[4], $Label[4], $aiTimerID[4]
$hGUI = GUICreate('定时器数组', 272, 139, -1, -1)
For $i = 1 To 3
        $Button[$i] = GUICtrlCreateButton('开始', 168, 8 + 40 * ($i - 1), 89, 25, 0)
        $Label[$i] = GUICtrlCreateLabel('00:00:00', 8, 8 + 40 * ($i - 1), 146, 17)
Next

GUISetState()

Local $iMsg, $iIX
Local $a=0 ,$b=0 ,$c=0,$d=0 , $e=0 ,$f=0 ,$i=0, $j=0 ,$k=0
Global $sec[20] = [$a,$b,$c,$d,$e,$f,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

While 1
        $iMsg = GUIGetMsg()
        Switch $iMsg
                Case -3
                        _Timer_KillAllTimers($hGUI)
                        GUIDelete()
                        Exit
                                         Case $Button[1], $Button[2], $Button[3]
                                                $OnTime1 = DllCall("kernel32.dll", "int", "GetTickCount")
                        $iIX = ($iMsg - $Button[1]) / 2 + 1
                        If GUICtrlRead($iMsg) == '开始' Then
                                GUICtrlSetData($iMsg, '停止')
                                $aiTimerID[$iIX] = _Timer_SetTimer($hGUI, 1000, '_Timer')
                                                                ConsoleWrite($iIX)
                        Else
                                GUICtrlSetData($iMsg, '开始')
                                _Timer_KillTimer($hGUI, $aiTimerID[$iIX])
                                $aiTimerID[$iIX] = 0
                        EndIf

        EndSwitch
WEnd



Func _Timer($hWnd, $iMsg, $iIDTimer, $iTime)
        Switch $iIDTimer
                Case $aiTimerID[1]
                        $i+=1
            Local $s,$m,$h
                        $s = Mod($i,60)
            $m = Mod($i/60,60)
                        $h = Mod($i/60/60,60)
                        $time = StringFormat("%02d:%02d:%02d", $h, $m, $s)
                        GUICtrlSetData($Label[1], $time)

                 Case $aiTimerID[2]
            $j+=1
            Local $s,$m,$h
                        $s = Mod($j,60)
            $m = Mod($j/60,60)
                        $h = Mod($j/60/60,60)
                        $time = StringFormat("%02d:%02d:%02d", $h, $m, $s)
                        GUICtrlSetData($Label[2], $time)


                 Case $aiTimerID[3]
            $k+=1
            Local $s,$m,$h
                        $s = Mod($k,60)
            $m = Mod($k/60,60)
                        $h = Mod($k/60/60,60)
                        $time = StringFormat("%02d:%02d:%02d", $h, $m, $s)
                        GUICtrlSetData($Label[3], $time)
                 EndSwitch

EndFunc   ;==>_Timer
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-20 03:00 , Processed in 0.079909 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表