恭喜了............ 建议用事件模式+adlib吧,这个嵌套了GUIGetMsg(),不容易看清程序结构
没注意到4楼循环锁死了,4楼建议真是不行的
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
$Form1 = GUICreate("Form1", 487, 98)
GUISetOnEvent($GUI_EVENT_CLOSE, "quit")
$Progress1 = GUICtrlCreateProgress(8, 72, 465, 17)
GUICtrlSetData(-1, 0)
$Button1 = GUICtrlCreateButton("开始", 48, 16, 105, 33)
GUICtrlSetOnEvent(-1, "start")
$Button2 = GUICtrlCreateButton("中止", 168, 16, 105, 33)
GUICtrlSetState(-1, $gui_disable)
GUICtrlSetOnEvent(-1, "stop")
$Button3 = GUICtrlCreateButton("退出", 288, 16, 97, 33)
GUICtrlSetOnEvent(-1, "quit")
Global $starti = 0, $stopi = 30
GUISetState(@SW_SHOW)
While 1
Sleep(100)
WEnd
Func quit()
Exit
EndFunc ;==>quit
Func stop()
AdlibUnRegister("Progress")
setstate()
MsgBox(0, "操作被中止", "操作被中止, 最后的 $i 是:" & $starti)
EndFunc ;==>stop
Func start()
GUICtrlSetState($Button1, $gui_disable)
GUICtrlSetState($Button2, $gui_enable)
GUICtrlSetState($Button3, $gui_disable)
GUICtrlSetData($Progress1, 0)
$starti = 0
AdlibRegister("Progress")
EndFunc ;==>start
Func setstate()
GUICtrlSetState($Button1, $gui_enable)
GUICtrlSetState($Button2, $gui_disable)
GUICtrlSetState($Button3, $gui_enable)
EndFunc ;==>setstate
Func Progress()
$starti += 1
GUICtrlSetData($Progress1, $starti * 100 / $stopi)
If $starti = $stopi Then
AdlibUnRegister("Progress")
setstate()
EndIf
EndFunc ;==>Progress 回复 17# seniors
谢谢高手,这个应该是正解。不过目前我还不怎么熟悉事件模式,需要学习吸收。
我是想修改以前编的一个小工具,看了看改成事件模式太麻烦,工作量太大了。 非事件模式的参看地一下吧
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Form1", 487, 98)
$Progress1 = GUICtrlCreateProgress(8, 72, 465, 17)
GUICtrlSetData(-1, 0)
$Button1 = GUICtrlCreateButton("开始", 48, 16, 105, 33)
$Button2 = GUICtrlCreateButton("中止", 168, 16, 105, 33)
GUICtrlSetState(-1, $gui_disable)
$Button3 = GUICtrlCreateButton("退出", 288, 16, 97, 33)
Global $starti = 0, $stopi = 30
GUISetState(@SW_SHOW)
While 1
$sMsg = GUIGetMsg()
Switch $sMsg
Case $Button1
GUICtrlSetState($Button1, $gui_disable)
GUICtrlSetState($Button2, $gui_enable)
GUICtrlSetState($Button3, $gui_disable)
GUICtrlSetData($Progress1, 0)
$starti = 0
AdlibRegister("Progress")
Case $Button2
AdlibUnRegister("Progress")
setstate()
MsgBox(0, "操作被中止", "操作被中止, 最后的 $i 是:" & $starti)
Case $GUI_EVENT_CLOSE, $Button3
Exit
EndSwitch
WEnd
Func setstate()
GUICtrlSetState($Button1, $gui_enable)
GUICtrlSetState($Button2, $gui_disable)
GUICtrlSetState($Button3, $gui_enable)
EndFunc ;==>setstate
Func Progress()
$starti += 1
GUICtrlSetData($Progress1, $starti * 100 / $stopi)
If $starti = $stopi Then
AdlibUnRegister("Progress")
setstate()
EndIf
EndFunc ;==>Progress
回复 19# seniors
厉害!感觉这个最适合。
多谢多谢! 我的解决之道如下: 楼主应该避免使用sleep,因为此函数会导致程序工作效率下降,我认为可以使用其他方法实现你的功能和效果,而且不会中断程序。 可以看一下我的 更新器示例 带中止按钮:传送门 热键不知道行不行? 回复 19# seniors
这个代码正解!! 回复 21# tvzml
试验不成功。我看代码里面也没有与按钮2有关的命令,是不是你少了什么代码? 修改一下19楼的代码,实现中止点击开始继续原来的进度#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("Form1", 487, 98)
$Progress1 = GUICtrlCreateProgress(8, 72, 465, 17)
GUICtrlSetData(-1, 0)
$Button1 = GUICtrlCreateButton("开始", 48, 16, 105, 33)
$Button2 = GUICtrlCreateButton("中止", 168, 16, 105, 33)
GUICtrlSetState(-1, $gui_disable)
$Button3 = GUICtrlCreateButton("退出", 288, 16, 97, 33)
Global $starti = 0, $stopi = 30
GUISetState(@SW_SHOW)
While 1
$sMsg = GUIGetMsg()
Switch $sMsg
Case $Button1
GUICtrlSetState($Button1, $gui_disable)
GUICtrlSetState($Button2, $gui_enable)
GUICtrlSetState($Button3, $gui_disable)
GUICtrlSetData($Progress1, $starti * 100 / $stopi)
AdlibRegister("Progress")
Case $Button2
AdlibUnRegister("Progress")
setstate()
MsgBox(0, "操作被中止", "操作被中止, 最后的 $i 是:" & $starti)
Case $GUI_EVENT_CLOSE, $Button3
Exit
EndSwitch
WEnd
Func setstate()
GUICtrlSetState($Button1, $gui_enable)
GUICtrlSetState($Button2, $gui_disable)
GUICtrlSetState($Button3, $gui_enable)
EndFunc ;==>setstate
Func Progress()
$starti += 1
GUICtrlSetData($Progress1, $starti * 100 / $stopi)
If $starti = $stopi Then
AdlibUnRegister("Progress")
setstate()
Return $starti
EndIf
EndFunc ;==>Progress 学习了,谢谢大家,前期一被这个问题纠结 好帖子,我也困惑了好久,mark一下
页:
1
[2]