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

[GUI管理] 关于GUI按钮产生的循环如何有效的退出

  [复制链接]
发表于 2011-4-5 12:29:20 | 显示全部楼层 |阅读模式
三个按钮,前两个都指向一个无限循环的子程序(我是这么理解的),第三个做为停止键。
但是实现过程中发现如果子程序的循环时间很短的话停止键比较有效,循环时间如果比较长的话想用停止按钮停止循环就又点困难了,得不停的点好多次才行
有没有办法解决这个问题


#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
;~ #NoTrayIcon
Local $Flg=False
HotKeySet("{ESC}", "Terminate")
HotKeySet('{F9}', '_HIShow')
Dim $H = True
Local $GUI_Button_1, $GUI_Button_2, $GUI_Button_stop, $GUIActiveX,$oIE
_IEErrorHandlerRegister()
$oIE = _IECreateEmbedded()
GUICreate("test。 F9键隐藏/显示窗口,ESC键退出", 300, 300, @DesktopWidth /2, @DesktopHeight /2)
$GUIActiveX = GUICtrlCreateObj($oIE, -1, 100, 300, 200)
        
        
        
$GUI_Button_1 = GUICtrlCreateButton("循环1,延时1秒,不停的按停止键才能推出循环", 5, 5, 290, 25)
$GUI_Button_2 = GUICtrlCreateButton("循环2,循环一圈很快,按一次停止键就能退出", 5, 30, 290, 25)
$GUI_Button_stop = GUICtrlCreateButton("停止键,狂点才能停", 5, 60, 290, 25)
        

$oIE.navigate("www.baidu.com")
GUISetState()

While 1
        $msg = GUIGetMsg()
        Select
                Case $msg = $GUI_EVENT_CLOSE
                        Exit
                Case $msg = $GUI_Button_1
                        _xunhuan1()
                Case $msg = $GUI_Button_2
                        _xunhuan2()
                Case $msg = $GUI_Button_stop
                        Exit
        EndSelect
WEnd


Func _xunhuan1()
        While 1
                $msg = GUIGetMsg()
                                If $msg = $GUI_Button_stop Then ExitLoop
                $oIE.navigate("www.baidu.com")
                Sleep (1000)  ;加了延时
        WEnd
        MsgBox(0,"","循环1被停止了")
EndFunc   

Func _xunhuan2()
        While 1
        $msg = GUIGetMsg()
        If $msg = $GUI_Button_stop Then ExitLoop
        $oIE.navigate("www.163.com") ;循环时间很短,按停止键后可以直接停止
        WEnd
        MsgBox(0,"","循环2被停止了")
EndFunc   




Func _HIShow()
        If $H = True Then
                GUISetState(@SW_HIDE)
                $H = False
        Else
                GUISetState(@SW_SHOW)
                $H = True
        EndIf
EndFunc   ;==>_HIShow
Func Terminate()
        Exit 0
EndFunc   ;==>Terminate
发表于 2011-4-5 13:51:26 | 显示全部楼层
做成多进程就可以了...
Autoit是一个单线程语言,不能够在循环中随意中断的.
发表于 2011-4-5 14:36:31 | 显示全部楼层
OnEvent模式能解决你的问题吧

评分

参与人数 1金钱 +10 贡献 +1 收起 理由
骗子 + 10 + 1 如何将上面的代码转换成OnEvent模式

查看全部评分

发表于 2011-4-5 14:40:22 | 显示全部楼层
用注册windows消息模式应该可以解决这个问题
发表于 2011-4-5 15:34:36 | 显示全部楼层
如果是操作对象,不好办,需要对象响应并返回值
发表于 2011-4-6 21:07:51 | 显示全部楼层
在onevent 模式下可以注册系统级的消息,程序会优先响应,例如,加入下面代码,程序进入死循环或正在运算时,也能响应最大小化和关闭,我对系统消息还不太了解,不知如何为按钮注册一个系统消息。
GUIRegisterMsg($WM_SYSCOMMAND, "On_WM_SYSCOMMAND")
Func On_WM_SYSCOMMAND($hWnd, $Msg, $wParam, $lParam)
        Local Const $SC_MOVE = 0xF010
        Local Const $SC_SIZE = 0xF000
        Local Const $SC_CLOSE = 0xF060

        If $hWnd = $Form1 Then
                ; ConsoleWrite(BitAND($wParam, 0xFFF0) & @crlf)
                ; 61472 Minimize
                ; 61488 Maximize
                ; 61728 Revoke Maximize or Minize (Restore)

                Switch BitAND($wParam, 0xFFF0)
                        Case 61472

                        Case 61488
;~                                 ConsoleWrite("Maximize detected" & @CRLF)
                        Case 61728

                        Case $SC_MOVE

                        Case $SC_SIZE
;~                                 ConsoleWrite("WM_Size detected" & @CRLF)
                        Case $SC_CLOSE

                                Exit
                                ; Return -1 ; to intercept close
                EndSwitch
        EndIf

        Return $GUI_RUNDEFMSG
EndFunc   ;==>On_WM_SYSCOMMAND
发表于 2011-4-6 23:12:28 | 显示全部楼层
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
;~ #NoTrayIcon
Global $Flg=True
;~ HotKeySet("{ESC}", "Terminate")
;~ HotKeySet('{F9}', '_HIShow')
Dim $H = True
Local $GUI_Button_1, $GUI_Button_2, $GUI_Button_stop, $GUIActiveX,$oIE
_IEErrorHandlerRegister()
$oIE = _IECreateEmbedded()
GUICreate("test。 F9键隐藏/显示窗口,ESC键退出", 300, 300, @DesktopWidth /2, @DesktopHeight /2)
$GUIActiveX = GUICtrlCreateObj($oIE, -1, 100, 300, 200)
        
        
        
$GUI_Button_1 = GUICtrlCreateButton("循环1,延时1秒,不停的按停止键才能推出循环", 5, 5, 290, 25)
$GUI_Button_2 = GUICtrlCreateButton("循环2,循环一圈很快,按一次停止键就能退出", 5, 30, 290, 25)
$GUI_Button_stop = GUICtrlCreateButton("停止键,狂点才能停", 5, 60, 290, 25)
        
 
$oIE.navigate("www.baidu.com")
GUISetState()
 GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
While 1
        $msg = GUIGetMsg()
        Select
                Case $msg = $GUI_EVENT_CLOSE
                        Exit
                Case $msg = $GUI_Button_1
                        _xunhuan1()
                Case $msg = $GUI_Button_2
                        _xunhuan2()
;~                 Case $msg = $GUI_Button_stop
;~                         Exit
        EndSelect
WEnd
 
 
Func _xunhuan1()
        if $Flg=False then $Flg=True
        GUICtrlSetState($GUI_Button_1,$gui_disable)
        GUICtrlSetState($GUI_Button_2,$gui_disable)
        While $Flg
                $oIE.navigate("www.baidu.com")
                Sleep (1000)  ;加了延时
        WEnd
        MsgBox(0,"","循环1被停止了")
                GUICtrlSetState($GUI_Button_1,$gui_enable)
                GUICtrlSetState($GUI_Button_2,$gui_enable)
EndFunc   
 
Func _xunhuan2()
        if $Flg=False then $Flg=True
        GUICtrlSetState($GUI_Button_1,$gui_disable)
        GUICtrlSetState($GUI_Button_2,$gui_disable)
        While $Flg
        $oIE.navigate("www.163.com") ;循环时间很短,按停止键后可以直接停止
        WEnd
        MsgBox(0,"","循环2被停止了")
                
                GUICtrlSetState($GUI_Button_1,$gui_enable)
                GUICtrlSetState($GUI_Button_2,$gui_enable)
EndFunc   
 
Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0xFFFF) = $GUI_Button_stop Then $Flg = False
    Return $GUI_RUNDEFMSG
EndFunc
发表于 2011-4-17 07:28:50 | 显示全部楼层
还是没有看明白
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-6-17 23:53 , Processed in 0.080995 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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