本帖最后由 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
|