找回密码
 加入
搜索
查看: 9043|回复: 20

[AU3基础] 除了用热键以外,有什么办法退出循环呐?(希望点击按钮退出)(已解决)

 火.. [复制链接]
发表于 2011-3-11 16:10:38 | 显示全部楼层 |阅读模式
本帖最后由 dearmb 于 2011-3-14 13:28 编辑

除了用热键以外,有什么办法退出循环呐?(希望点击按钮退出)
查了一些帖子,发现都是设置热键改一个变量跳出循环的,不能通过按钮退出循环吗?
发表于 2011-3-11 16:28:39 | 显示全部楼层
本帖最后由 tryhi 于 2011-3-11 19:24 编辑

热键……按钮……循环……跳出循环……有什么联系?
 楼主| 发表于 2011-3-11 16:37:02 | 显示全部楼层
本帖最后由 dearmb 于 2011-3-11 16:39 编辑

我觉得凡是退出循环的东西,都需要用热键……这好像很不对啊……
比如说,我现在有个开始按钮,开始之后想停止循环,就只能用热键?
按钮开始,热键结束,很别扭
要不就热键开始,热键结束……
感觉按钮这东西一无是处了阿……
发表于 2011-3-11 17:08:01 | 显示全部楼层
gui用事件模式
 楼主| 发表于 2011-3-11 17:22:59 | 显示全部楼层
gui用事件模式
xyold1 发表于 2011-3-11 17:08



    改用了事件模式,但是在循环执行中,依然无法响应按钮操作……
 楼主| 发表于 2011-3-11 19:26:44 | 显示全部楼层
没有办法么……
现在勉强用开始用按钮开始
结束用热键结束,唉,太别扭了……
发表于 2011-3-11 19:31:36 | 显示全部楼层
本帖最后由 tryhi 于 2011-3-11 19:52 编辑

大概明白楼主表达的意思了,求加钱
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Local $Flg = False
Local $t = 0
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 318, 120, 468, 171)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$Button1 = GUICtrlCreateButton("开始循环", 8, 84, 57, 25)
GUICtrlSetOnEvent(-1, "Button1Click")
$Button4 = GUICtrlCreateButton("结束循环", 238, 84, 57, 25)
GUICtrlSetOnEvent(-1, "Button2Click")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
        If $Flg = True Then
                Ab()
        Else
                Sleep(200)
        EndIf
WEnd
Func Ab()
        While 1
                ToolTip("正在循环")
                Sleep(100)
                If $Flg = False Then ExitLoop
        WEnd
EndFunc   ;==>Ab
Func Button1Click()
        $Flg = True
EndFunc   ;==>Button1Click
Func Button2Click()
        $Flg = False
                msgbox(0,0,"你按了结束循环")
EndFunc   ;==>Button2Click
Func Form1Close()
        Exit
EndFunc   ;==>Form1Close

评分

参与人数 1金钱 +10 收起 理由
xyold1 + 10

查看全部评分

 楼主| 发表于 2011-3-11 19:43:52 | 显示全部楼层
大概明白楼主表达的意思了,求加钱
tryhi 发表于 2011-3-11 19:31



   虽然 还没试,但是原理似乎和我现在做的一样
我现在是
If         $flag = 0 Then ExitLoop
然后设个按钮Button2
                        Func Button2()
                        If $locked <> 1 Then
                        MsgBox(48, "错误", "请先“开始”程序!")
                        Else
                        $flag = 0
                        EndIf
                EndFunc
实际上没有任何效果,等到循环结束之后才执行按钮2的操作
发表于 2011-3-11 19:51:39 | 显示全部楼层
本帖最后由 tryhi 于 2011-3-11 19:58 编辑

回复 8# dearmb

你还没试就直接说不行,如果行的话你怎么样???
 楼主| 发表于 2011-3-11 19:57:09 | 显示全部楼层
因为现在没时间试……等周一试……
发表于 2011-3-11 20:00:00 | 显示全部楼层
本帖最后由 tryhi 于 2011-3-11 20:09 编辑

你仔细就会发现跟你那个有一个不一样的地方,我的“开始按钮”是在主循环之中,这使得“结束按钮”优先“开始按钮”运行,当“结束”按下去时“开始”会被挂起
发表于 2011-3-11 20:11:33 | 显示全部楼层
本帖最后由 水木子 于 2011-3-11 20:42 编辑

当然照常理来看,当进入死循环以后是无法响应按钮事件的。
以前是群里就有人问过这个问题,其实不用将问题想得太复杂,直接用按钮开始和退出都是可以的。

不过我倒觉得没必要这样去做,因为 _Timer_SetTimer、AdlibRegister 等函数可以用来替代循环,并且可以很轻松的控制它们的开始和结束。
发表于 2011-3-12 11:47:20 | 显示全部楼层
本帖最后由 3mile 于 2011-3-12 12:05 编辑

第二按钮跳出循环.
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $Flg = 1
Local $t = 0

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 318, 120, 468, 171)
$Button1 = GUICtrlCreateButton("开始循环", 48, 60, 80, 40)
$Button2 = GUICtrlCreateButton("结束循环", 208, 60, 80, 40)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While 1
        $msg=GUIGetMsg()
        Switch $msg
                case -3
                        Exit
                Case $Button1
                        TEST()
        EndSwitch
WEnd
        
Func TEST()
        While $Flg
                ToolTip($t)
                $t+=1
        WEnd
        $Flg=True
        $t=0
EndFunc

Func WM_COMMAND($hWnd, $msg, $wParam, $lParam)
        #forceref $hWnd, $Msg
        Local $nNotifyCode = BitShift($wParam, 16)
        Local $nID = BitAND($wParam, 0x0000FFFF)
        Local $hCtrl = $lParam        
        Switch $hCtrl
                Case GUICtrlGetHandle($Button2)
                                $Flg=False
        EndSwitch
EndFunc   ;==>WM_COMMAND


开关按钮跳出循环:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $Flg = True
Local $t = 0

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 318, 120, 468, 171)
$Button1 = GUICtrlCreateButton("开始循环", 48, 60, 80, 40)
;$Button2 = GUICtrlCreateButton("结束循环", 208, 60, 80, 40)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While 1
        $msg=GUIGetMsg()
        Switch $msg
                case -3
                        Exit
                Case $Button1
                        if GUICtrlRead($Button1)="开始循环" and $Flg=True Then
                                GUICtrlSetData($Button1,"结束循环")
                                TEST()
                        Else
                                GUICtrlSetData($Button1,"开始循环")
                        EndIf
        EndSwitch
WEnd
        
Func TEST()
        While $Flg
                ToolTip($t)
                $t+=1
        WEnd
        $Flg=True
        $t=0
EndFunc

Func WM_COMMAND($hWnd, $msg, $wParam, $lParam)
        #forceref $hWnd, $Msg
        Local $nNotifyCode = BitShift($wParam, 16)
        Local $nID = BitAND($wParam, 0x0000FFFF)
        Local $hCtrl = $lParam        
        Switch $hCtrl
                Case GUICtrlGetHandle($Button1)
                        if GUICtrlRead($Button1)="结束循环" then 
                                $Flg=False
                        Else
                                $Flg=True
                        EndIf
        EndSwitch
EndFunc   ;==>WM_COMMAND
发表于 2011-3-12 19:23:23 | 显示全部楼层
exit不就是退出程序了么?
发表于 2011-3-12 19:40:31 | 显示全部楼层
谢谢分享!!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-6-26 23:51 , Processed in 0.087128 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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