求助关于做一个“秒表”的小工具
我昨天想了一天也没想出来,秒表这个问题其实很典型。你点击了开始之后就进入了一个死循环,怎么样按暂停的时候还能响应?求高手解答,万分感谢。;不好意思,这里放多了,因为中间尝试了很多方法,所以调用了很多东西
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <file.au3>
#Include <GuiButton.au3>
#include <GUIListView.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <IE.au3>
#include <Timers.au3>
#include <ListboxConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <TabConstants.au3>
$Form1 = GUICreate("Form1", 555, 309, 198, 123)
$StartButton = GUICtrlCreateButton("开始", 328, 176, 185, 105)
GUICtrlSetFont(-1, 42, 800, 0, "微软雅黑")
$Time = GUICtrlCreateLabel("00:00:00", 32, 25, 386, 128)
GUICtrlSetFont(-1, 72, 400, 0, "微软雅黑")
GUICtrlSetBkColor(-1, 0xFF0000)
$ResetButton = GUICtrlCreateButton("重置", 29, 176, 185, 105)
GUICtrlSetFont(-1, 42, 800, 0, "微软雅黑")
$SmallTime = GUICtrlCreateLabel("00", 418, 45, 98, 108)
GUICtrlSetFont(-1, 60, 400, 0, "微软雅黑")
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlCreateLabel("", 416, 25, 100, 20)
GUICtrlSetFont(-1, 72, 400, 0, "微软雅黑")
GUICtrlSetBkColor(-1, 0xFF0000)
GUISetState(@SW_SHOW)
$If_First_Run = 1
$If_Start_Or_Pause = -1
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $StartButton
$If_Start_Or_Pause = -$If_Start_Or_Pause
If $If_Start_Or_Pause == 1 Then
_GUICtrlButton_SetText($StartButton, "暂停")
$Button_State = _GUICtrlButton_GetText($StartButton)
MsgBox(0,"",$Button_State)
Else
_GUICtrlButton_SetText($StartButton, "继续")
$Button_State = _GUICtrlButton_GetText($StartButton)
MsgBox(0,"",$Button_State)
EndIf
If $If_First_Run == 1 Then
;这里想要做一个暂停和继续,尤其是获取毫秒数,不知道怎么搞了
$Start_Time = _Timer_Init()
$If_First_Run = 0
EndIf
While $Button_State == "暂停"
;这里就进入一个死循环了,点击了一次开始就再也出不来了
Sleep(1000)
GUICtrlSetData ($Time,@HOUR&":"&@MIN&":"&@SEC)
;想要通过获取按钮状态来退出循环,可惜这里已经不再执行上面的 GUIGetMsg()了
$Button_State = _GUICtrlButton_GetText($StartButton)
;MsgBox(0,"",$Button_State)
WEnd
Case $ResetButton
EndSwitch
WEnd 补充一下,获取系统当前时间只是简单化问题,还是想从00:00:00 00开始的,就和手表一样,盼高手 多进程代替多线程
独立一个进程计时,给主进程暂停命令了之后再发消息暂停? 请使用
【code】你的代码【/code】
或
【au3】你的代码【/au3】
标签。
LZ从来没学会吗? 本帖最后由 netegg 于 2011-12-1 12:04 编辑
我记得我发过一个呀,示例板块吧,找找看
无外乎多加个按钮进行开启/关闭罢了 本帖最后由 xiehuahere 于 2011-12-1 13:31 编辑
回复 1# acetaohai123
关于如何退出循环:#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <file.au3>
#Include <GuiButton.au3>
#include <GUIListView.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <IE.au3>
#include <Timers.au3>
#include <ListboxConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <TabConstants.au3>
$Form1 = GUICreate("Form1", 555, 309, 198, 123)
$StartButton = GUICtrlCreateButton("开始", 328, 176, 185, 105)
GUICtrlSetFont(-1, 42, 800, 0, "微软雅黑")
$Time = GUICtrlCreateLabel("00:00:00", 32, 25, 386, 128)
GUICtrlSetFont(-1, 72, 400, 0, "微软雅黑")
GUICtrlSetBkColor(-1, 0xFF0000)
$ResetButton = GUICtrlCreateButton("重置", 29, 176, 185, 105)
GUICtrlSetFont(-1, 42, 800, 0, "微软雅黑")
$SmallTime = GUICtrlCreateLabel("00", 418, 45, 98, 108)
GUICtrlSetFont(-1, 60, 400, 0, "微软雅黑")
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlCreateLabel("", 416, 25, 100, 20)
GUICtrlSetFont(-1, 72, 400, 0, "微软雅黑")
GUICtrlSetBkColor(-1, 0xFF0000)
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
$If_First_Run = 1
$If_Start_Or_Pause = -1
$initFlag = 0 ; 加个中断标志位
$Button_State = "开始" ; 这个要设为全局变量
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $StartButton
$If_Start_Or_Pause = -$If_Start_Or_Pause
If $If_Start_Or_Pause = 1 Then
_GUICtrlButton_SetText($StartButton, "暂停")
$Button_State = _GUICtrlButton_GetText($StartButton)
MsgBox(0,"",$Button_State)
Else
_GUICtrlButton_SetText($StartButton, "继续")
$Button_State = _GUICtrlButton_GetText($StartButton)
MsgBox(0,"",$Button_State)
EndIf
If $If_First_Run = 1 Then
;这里想要做一个暂停和继续,尤其是获取毫秒数,不知道怎么搞了
$Start_Time = _Timer_Init()
$If_First_Run = 0
EndIf
While $Button_State = "暂停"
If $initFlag Then ExitLoop;通过中断标志来退出循环
Sleep(1000)
GUICtrlSetData ($Time,@HOUR&":"&@MIN&":"&@SEC)
WEnd
Case $ResetButton
EndSwitch
WEnd
Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
If BitAND($wParam, 0x0000FFFF) =$StartButton And $Button_State = "暂停" Then $initFlag = 1
Return $GUI_RUNDEFMSG
EndFunc 本帖最后由 骗子 于 2011-12-1 15:49 编辑
用GUIOnEventMode
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <file.au3>
#include <GuiButton.au3>
#include <GUIListView.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <IE.au3>
#include <Timers.au3>
#include <ListboxConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <TabConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#region ### START Koda GUI section ### Form=
$Form1_1 = GUICreate("伪秒表", 556, 310, 198, 123)
$StartButton = GUICtrlCreateButton("开始", 328, 176, 185, 105)
GUICtrlSetFont(-1, 42, 800, 0, "微软雅黑")
$Time = GUICtrlCreateLabel("00:00:00:00", 12, 25, 520, 128)
GUICtrlSetFont(-1, 72, 400, 0, "微软雅黑")
GUICtrlSetBkColor(-1, 0xFF0000)
$ResetButton = GUICtrlCreateButton("重置", 29, 176, 185, 105)
GUICtrlSetFont(-1, 42, 800, 0, "微软雅黑")
;~ $SmallTime = GUICtrlCreateLabel("00", 418, 45, 98, 108)
;~ GUICtrlSetFont(-1, 60, 400, 0, "微软雅黑")
;~ GUICtrlSetBkColor(-1, 0xFF0000)
;~ GUICtrlCreateLabel("", 416, 25, 100, 20)
;~ GUICtrlSetFont(-1, 72, 400, 0, "微软雅黑")
;~ GUICtrlSetBkColor(-1, 0xFF0000)
GUISetState(@SW_SHOW)
GUICtrlSetOnEvent($StartButton, "_kaiguan")
GUICtrlSetOnEvent($ResetButton, "_chushuhua")
#endregion ### END Koda GUI section ###
Global $shi = 0, $fen = 0, $miao = 0, $haomiao = 0, $kaiguan = 1, $begin, $dif
While 1
Sleep(100)
_jishu()
WEnd
Func _kaiguan()
If $kaiguan = 1 Then
$kaiguan = 0
GUICtrlSetData($StartButton, "开始")
$begin = TimerInit()
Else
$kaiguan = 1
GUICtrlSetData($StartButton, "暂停")
$dif = TimerDiff($begin)
MsgBox(0, "实际用时:", $dif)
EndIf
EndFunc ;==>_kaiguan
Func _jishu()
If $kaiguan = 0 Then
$Time1 = StringFormat("%02i:%02i:%02i:%2i", $shi, $fen, $miao, $haomiao)
GUICtrlSetData($Time, $Time1)
$haomiao += 1
If $haomiao > 9 Then
$miao += 1
$haomiao = 0
EndIf
If $miao > 59 Then
$miao = 0
$fen += 1
EndIf
If $fen > 59 Then
$fen = 0
$shi += 1
EndIf
EndIf
EndFunc ;==>_jishu
Func _chushuhua()
Global $shi = 0, $fen = 0, $miao = 0, $haomiao = 0
$Time1 = StringFormat("%02i:%02i:%02i:%02i", $shi, $fen, $miao, $haomiao)
GUICtrlSetData($Time, $Time1)
EndFunc ;==>_chushuhua
http://www.autoitx.com/thread-28517-1-1.html这是一个倒计时的例子 今天看了一个人写的案例,发现七楼很对啊。就是用GUIOnEventMode,这样才可以让监听的按钮脱离死循环 我又改了改,看这个帖子,又有新问题啦:http://www.autoitx.com/forum.php?mod=viewthread&tid=29044&page=1&extra=#pid370825,四楼的教导铭记在心,我还贴了图呢。 不是GUIOnEventMode模式的也可以的:#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Form1", 555, 309, 198, 123)
$Button1 = GUICtrlCreateButton("开始",328, 176, 185, 105)
GUICtrlSetFont(-1, 42, 800, 0, "微软雅黑")
$Time = GUICtrlCreateLabel("00:00:00", 32, 25, 386, 128)
GUICtrlSetFont(-1, 72, 400, 0, "微软雅黑")
GUICtrlSetBkColor(-1, 0xFF0000)
$ResetButton = GUICtrlCreateButton("重置", 29, 176, 185, 105)
GUICtrlSetFont(-1, 42, 800, 0, "微软雅黑")
$SmallTime = GUICtrlCreateLabel(" :秒表", 418, 45, 98, 108)
GUICtrlSetFont(-1, 20, 400, 0, "微软雅黑")
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlCreateLabel("", 416, 25, 100, 20)
GUICtrlSetFont(-1, 72, 400, 0, "微软雅黑")
GUICtrlSetBkColor(-1, 0xFF0000)
GUISetState(@SW_SHOW)
$stop = 0
While 1
Sleep(100)
If $stop = 1 Then
GUICtrlSetData($Time,@HOUR&":"&@MIN&":"&@SEC)
EndIf
If $stop = 0 Then
EndIf
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button1
If GUICtrlRead ($Button1)= "开始" Then
GUICtrlSetData($Button1,"暂停")
$stop = 1
Else
If GUICtrlRead ($Button1)= "继续" Then
GUICtrlSetData($Button1,"暂停")
$stop = 1
Else
GUICtrlSetData($Button1,"继续")
$stop = 0
EndIf
EndIf
EndSwitch
WEnd
回复 9# acetaohai123
事实证明OnEventMode比CycleMode好 那么多,写的真好了 本帖最后由 netegg 于 2011-12-3 12:00 编辑
回复 12# xms77
未必,两者的优劣完全看书写的逻辑,没好坏之分
如果逻辑能力比较好的话,完全可以在一个脚本里切来切去
还有,试想下,很多操作需要同时响应(不是多进程或多线程,只是响应),未必用哪个更好些 回复 14# netegg
蛋哥,受教了!thank U
页:
[1]