找回密码
 加入
搜索
楼主: 邪恶海盗

[AU3基础] [已解决]如何中断运行中的命令?

[复制链接]
发表于 2023-9-2 16:46:35 | 显示全部楼层
邪恶海盗 发表于 2023-9-2 16:42
你这是在打击我学习的积极性么???

因为 WM_COMMAND 例子实在是太多了,我首先也是给你说的这个~
 楼主| 发表于 2023-9-2 17:07:38 | 显示全部楼层
afan 发表于 2023-9-2 16:46
因为 WM_COMMAND 例子实在是太多了,我首先也是给你说的这个~

送佛要送到西的道理都不懂么?
发表于 2023-9-2 17:17:38 | 显示全部楼层
邪恶海盗 发表于 2023-9-2 17:07
送佛要送到西的道理都不懂么?
                        For $n = 1 To 10
                                If $StopCode = 1 Then ExitLoop
                                $ts = TimerInit()
                                Do
                                        If GUIGetMsg() = -3 Then Exit
                                        Sleep(10)
                                Until TimerDiff($ts) > 500
                                GUICtrlSetData($Input1, $n)
                        Next
 楼主| 发表于 2023-9-2 17:53:58 | 显示全部楼层
本帖最后由 邪恶海盗 于 2023-9-2 17:55 编辑
X,原来只加一条判断消息就OK了,捣鼓许久觉得还是GUIOnEventMode好用一些,就是不知道CheckBox这个写得对不对...
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#region ### START Koda GUI section ### Form=c:\documents and settings\administrator\桌面\form11.kxf
Opt('GUIOnEventMode', 1)
$Form1 = GUICreate("Form1", 500, 200)
GUISetOnEvent(-3, '_Close')
$Input1 = GUICtrlCreateInput("Input1", 56, 32, 225, 21)
$Input2 = GUICtrlCreateInput("Input2", 56, 132, 225, 21)
$Button1 = GUICtrlCreateButton("开始", 312, 24, 65, 33)
GUICtrlSetOnEvent(-1, '_Start')
$Button2 = GUICtrlCreateButton("停止", 400, 24, 73, 33)
$Checkbox = GUICtrlCreateCheckbox("Checkbox1", 300, 132)
GUICtrlSetOnEvent(-1, '_Chkbox')
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
        Sleep(1000)
WEnd

Func _Start()
        Opt('GUIOnEventMode', 0)
        GUICtrlSetData($Button1, '正在运行')
        GUICtrlSetState($Button1, $GUI_DISABLE)
        GUICtrlSetState($Input1, $GUI_DISABLE)

        For $n = 1 To 10
                $ts = TimerInit()
                Do
                        Switch GUIGetMsg()
                                Case -3
                                        _Close()
                                Case $Button2
                                        ExitLoop 2
                                Case $Checkbox
                                        _Chkbox()
                        EndSwitch
                        GUICtrlSetData($Input1, $n)
                Until TimerDiff($ts) > 500
        Next
        GUICtrlSetData($Button1, '开始')
        GUICtrlSetState($Button1, $GUI_ENABLE)
        GUICtrlSetState($Input1, $GUI_ENABLE)
        Opt('GUIOnEventMode', 1)
EndFunc   ;==>_Start
Func _Chkbox()

        If GUICtrlRead($Checkbox) = $GUI_CHECKED Then
                GUICtrlSetData($Input2, 1)
        Else
                GUICtrlSetData($Input2, 0)
        EndIf

EndFunc   ;==>_Chkbox

Func _Close()
        Exit
EndFunc   ;==>_Close

 楼主| 发表于 2023-9-2 18:04:10 | 显示全部楼层
不是小灵通 发表于 2023-9-2 15:40
关闭按钮无效,应该是程序暂时执念在for循环内不能自拔吧
是不是可以考虑个办法在循环体内弄个检测点击关 ...

就do后面加 If GUIGetMsg() = -3 Then Exit检测就行了,比如这样:
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#region ### START Koda GUI section ### Form=c:\documents and settings\administrator\桌面\form11.kxf
Global $StopCode
$Form1 = GUICreate("Form1", 500, 200)
$Input1 = GUICtrlCreateInput("Input1", 56, 32, 225, 21)
$Button1 = GUICtrlCreateButton("Button1", 312, 24, 65, 33)
$Button2 = GUICtrlCreateButton("Button2", 400, 24, 73, 33)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        $StopCode = 0
                        GUICtrlSetData($Button1, '开始')
                        GUICtrlSetData($Button1, '正在运行')
                        GUICtrlSetState($Button1, $GUI_DISABLE)
                        GUICtrlSetState($Input1, $GUI_DISABLE)
                        For $n = 1 To 10
                                If $StopCode = 1 Then ExitLoop
                                $ts = TimerInit()
                                Do
                                        If GUIGetMsg() = -3 Then Exit
                                        GUICtrlSetData($Input1, $n)
                                Until TimerDiff($ts) > 500
                        Next
                        GUICtrlSetData($Button1, '开始')
                        GUICtrlSetState($Button1, $GUI_ENABLE)
                        GUICtrlSetState($Input1, $GUI_ENABLE)
        EndSwitch
WEnd

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
        Local $hWndFrom, $iIDFrom, $iCode, $iIDFrom1
        Switch BitAND(0xffff, $wParam)
                Case $Button2
                        $StopCode = 1
        EndSwitch
EndFunc   ;==>WM_COMMAND
Func _exit()
        Exit
EndFunc   ;==>_exit
 楼主| 发表于 2023-9-3 10:48:43 | 显示全部楼层



看起来使用WM_COMMAND也不麻烦,但好像点Button2后要等延时走完了才会传递函数,而使用GUIOnEventMode是即时响应的...
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#region ### START Koda GUI section ### Form=c:\documents and settings\administrator\桌面\form11.kxf
Global $StopCode
$Form1 = GUICreate("Form1", 500, 200)
$Input1 = GUICtrlCreateInput("Input1", 56, 32, 225, 21)
$Input2 = GUICtrlCreateInput("Input2", 56, 132, 225, 21)
$Button1 = GUICtrlCreateButton("Button1", 312, 24, 65, 33)
$Button2 = GUICtrlCreateButton("Button2", 400, 24, 73, 33)
$Checkbox = GUICtrlCreateCheckbox("Checkbox", 300, 132)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        $StopCode = 0
                        GUICtrlSetData($Button1, '正在运行')
                        GUICtrlSetState($Button1, $GUI_DISABLE)
                        GUICtrlSetState($Input1, $GUI_DISABLE)
                        For $n = 1 To 10
If $StopCode = 1 Then ExitLoop
$ts = TimerInit()
Do
                                        If GUIGetMsg() = -3 Then Exit
                                        GUICtrlSetData($Input1, $n)
                                Until TimerDiff($ts) > 500
                        Next
                        GUICtrlSetData($Button1, '开始')
                        GUICtrlSetState($Button1, $GUI_ENABLE)
                        GUICtrlSetState($Input1, $GUI_ENABLE)
        EndSwitch
WEnd

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
        Local $hWndFrom, $iIDFrom, $iCode, $iIDFrom1
        Switch BitAND(0xffff, $wParam)
                Case $Button2
                        $StopCode = 1
                Case $Checkbox
                        If GUICtrlRead($Checkbox) = $GUI_CHECKED Then
                                GUICtrlSetData($Input2, 1)
                        Else
                                GUICtrlSetData($Input2, 0)
                        EndIf
        EndSwitch
EndFunc   ;==>WM_COMMAND
Func _exit()
        Exit
EndFunc   ;==>_exit

 楼主| 发表于 2023-9-3 10:54:46 | 显示全部楼层
搞定了,原来很简单,把28行的If $StopCode = 1 Then ExitLoop删除,然后把29行改成Until TimerDiff($ts) > 500 Or $StopCode=1即可,看来大佬有时候也会带我们入坑的...
发表于 2023-9-3 11:03:44 | 显示全部楼层
WM_COMMAND 与 GUIOnEventMode 没有相干性~ 不管用什么方式都能及时响应,关键是代码逻辑。
发表于 2023-9-3 17:59:27 | 显示全部楼层
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=c:\documents and settings\administrator\桌面\form11.kxf
$Form1 = GUICreate("Form1", 500, 200)
$Input1 = GUICtrlCreateInput("Input1", 56, 32, 225, 21)
$Button1 = GUICtrlCreateButton("Button1", 312, 24, 65, 33)
$Button2 = GUICtrlCreateButton("Button2", 400, 24, 73, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $stop = False
GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit

                Case $Button1
                        For $n = 1 To 100
                                If $stop Then ExitLoop
                                Sleep(500)
                                GUICtrlSetData($Input1, $n)
                        Next
                        $stop = False
                Case $Button2
        EndSwitch
WEnd
Func WM_COMMAND($hwnd, $msg, $wparam, $lparam)
        Local $Code = BitShift($wparam, 16)
        Local $CtrlId = BitAND($wparam, 0xffff)
        Local $hCtrl = $lparam
        Switch $CtrlId
                Case $Button1
                Case $Button2
                        $stop = True
        EndSwitch
        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND
 楼主| 发表于 2023-9-3 18:06:20 | 显示全部楼层

这个好像也要等延时过了之后才响应exitloop的?
 楼主| 发表于 2023-9-3 21:44:30 | 显示全部楼层
afan 发表于 2023-9-3 11:03
WM_COMMAND 与 GUIOnEventMode 没有相干性~ 不管用什么方式都能及时响应,关键是代码逻辑。

又发现一个问题,假如说把StopCode放到Until后面,实际上触发之后for to还是在运行的,对于实际使用来说是否会有影响?
发表于 2023-9-3 22:02:52 | 显示全部楼层
邪恶海盗 发表于 2023-9-3 21:44
又发现一个问题,假如说把StopCode放到Until后面,实际上触发之后for to还是在运行的,对于实际使用来说是否 ...

当然是这样,因此跳出的时机及检测点的数量很重要。
 楼主| 发表于 2023-9-3 22:27:36 | 显示全部楼层
afan 发表于 2023-9-3 22:02
当然是这样,因此跳出的时机及检测点的数量很重要。

在这个例子里怎么设计比较好???
发表于 2023-9-3 22:44:31 | 显示全部楼层
本帖最后由 afan 于 2023-9-3 22:47 编辑
邪恶海盗 发表于 2023-9-3 22:27
在这个例子里怎么设计比较好???

以你21#的代码为例,简单修改 For...Next中的内容,检测点换个地方即可,顺便再次纠正一处影响效率的地方(这是第三次纠正,每次都被你改回错误的方式,这种情况我一般都懒得说了),另外Sleep可按需添加。
                        For $n = 1 To 10
                                $ts = TimerInit()
                                Do
                                        If GUIGetMsg() = -3 Then Exit
                                        If $StopCode = 1 Then ExitLoop 2
                                Until TimerDiff($ts) > 500
                                GUICtrlSetData($Input1, $n)
                        Next
 楼主| 发表于 2023-9-3 23:00:22 | 显示全部楼层
afan 发表于 2023-9-3 22:44
以你21#的代码为例,简单修改 For...Next中的内容,检测点换个地方即可,顺便再次纠正一处影响效率的地方 ...

+1,果然条条大路通罗马啊,对于从来没学过编程的来说,能东拼西凑出这些玩意算是不错了,传说中的"屎山"代码可能就是如此???
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-2 07:21 , Processed in 0.080626 second(s), 14 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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