找回密码
 加入
搜索
楼主: haorui658

[GUI管理] 怎么不用热键而用一个按钮终止一个循环?[已解决]

 火.. [复制链接]
发表于 2010-3-4 01:02:09 | 显示全部楼层
使用事件模式可以解决:
#include <GUIConstants.au3>
Opt("GUIOnEventMode", 1)
$var = 1
$pause = True
$Mail = GUICreate("邮件发送", 450, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "mygui")
$Group1 = GUICtrlCreateGroup("基本配置", 8, 8, 330, 300)
$SmtpServer_Label = GUICtrlCreateLabel("SMTP服务器", 20, 40, 80, 20)
$UP_Label = GUICtrlCreateLabel("用户名/密码", 20, 70, 80, 20)
$FromAddress_Label = GUICtrlCreateLabel("From", 20, 100, 80, 20)
$To_Label = GUICtrlCreateLabel("To", 20, 130, 80, 20)
$Subject_Label = GUICtrlCreateLabel("标题", 20, 160, 80, 20)
$Body_Label = GUICtrlCreateLabel("正文", 20, 190, 80, 20)
$Start_Label = GUICtrlCreateLabel("起始", 20, 360, 80, 20)
$Start_Label = GUICtrlCreateLabel("数量", 80, 360, 80, 20)

$SMTP = GUICtrlCreateInput("", 100, 40, 220, 20)
$Username = GUICtrlCreateInput("", 100, 70, 100, 20)
$Password = GUICtrlCreateInput("", 220, 70, 100, 20, 0x0020)
$From = GUICtrlCreateInput("", 100, 100, 220, 20)
$To = GUICtrlCreateInput("", 100, 130, 220, 20)
$Subject = GUICtrlCreateInput("subject", 100, 160, 220, 20)
$Body = GUICtrlCreateEdit("Mail Body", 100, 190, 220, 100)


$Start = GUICtrlCreateButton("Start", 340, 200, 70)
GUICtrlSetOnEvent(-1, "mygui")
$Stop = GUICtrlCreateButton("Stop", 340, 280, 70)
GUICtrlSetOnEvent(-1, "mygui")
GUISetState(@SW_SHOW)

While 1
        If $pause == False Then
                $var=1
                While ($var <= 2000) And $pause == False
                        MsgBox(0, "", "Send")
                        ;sendmail ()
                        $var = $var + 1
                        Sleep(500)
                WEnd
        EndIf
        Sleep(100)
WEnd
Func mygui()
        Switch @GUI_CtrlId
                Case $Start
                        $pause = False
                Case $Stop
                        $pause = True
                Case $GUI_EVENT_CLOSE
                        Exit
        EndSwitch
EndFunc   ;==>mygui


Func sendmail()
        Sleep(300)
EndFunc   ;==>sendmail

评分

参与人数 1金钱 +20 收起 理由
afan + 20

查看全部评分

 楼主| 发表于 2010-3-4 08:57:13 | 显示全部楼层
回复 16# foboy


    事件机制果然可以行的通,十分感谢楼上,消息模式中估计不能sleep太长时间,否则无法截获消息
发表于 2010-3-4 13:12:15 | 显示全部楼层
回复 17# haorui658


    需要注意的是,这里虽然能即时响应Stop按钮,但Start开始的动作仍未实时停止,而是必须完成当次动作才会停止循环。你将LS的第39行代码改成sleep(5000)就能知道。Start 后按 stop,然后立即再 start,会发现并没有立即执行,而是必须等到 sleep 完才会继续。
 楼主| 发表于 2010-3-4 17:57:50 | 显示全部楼层
恩 多谢afan提醒,这已经足够了
发表于 2010-3-4 18:13:35 | 显示全部楼层
本帖最后由 afan 于 2010-3-5 10:37 编辑

回复 19# haorui658


    给你个直观点的消息模式演示,间隔2秒循环
GUICreate('邮件发送', 450, 500)
$Group1 = GUICtrlCreateGroup('基本配置', 8, 8, 330, 300)
$SmtpServer_Label = GUICtrlCreateLabel('SMTP服务器', 20, 40, 80, 20)
$UP_Label = GUICtrlCreateLabel('用户名/密码', 20, 70, 80, 20)
$FromAddress_Label = GUICtrlCreateLabel('From', 20, 100, 80, 20)
$To_Label = GUICtrlCreateLabel('To', 20, 130, 80, 20)
$Subject_Label = GUICtrlCreateLabel('标题', 20, 160, 80, 20)
$Body_Label = GUICtrlCreateLabel('正文', 20, 190, 80, 20)
$Start_Label = GUICtrlCreateLabel('起始', 20, 360, 80, 20)
$Start_Label = GUICtrlCreateLabel('数量', 80, 360, 80, 20)
$SMTP = GUICtrlCreateInput('', 100, 40, 220, 20)
$Username = GUICtrlCreateInput('', 100, 70, 100, 20)
$Password = GUICtrlCreateInput('', 220, 70, 100, 20, 0x0020)
$From = GUICtrlCreateInput('', 100, 100, 220, 20)
$To = GUICtrlCreateInput('', 100, 130, 220, 20)
$Subject = GUICtrlCreateInput('subject', 100, 160, 220, 20)
$Body = GUICtrlCreateEdit('Mail Body', 100, 190, 220, 100)
$Start = GUICtrlCreateButton('Start', 340, 200, 70)
$Stop = GUICtrlCreateButton('Stop', 340, 280, 70)
$Label = GUICtrlCreateLabel('准备Send', 80, 400, 200, 20)
GUISetState()
GUIRegisterMsg(0x0111, 'WM_COMMAND')
$pause = True
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case -3
                        Exit
                Case $Start
                        GUICtrlSetState($Start, 128)
                        $var = 1
                        $pause = False
                        While $var <= 2000
                                If $pause Then
                                        GUICtrlSetData($Label, 'Send ' & $var - 1 & ' 已停止')
                                        MsgBox(0, 0, '已停止')
                                        GUICtrlSetState($Start, 64)
                                        ExitLoop
                                EndIf
                                GUICtrlSetData($Label, 'Send ' & $var)
                                sendmail()
                                $var += 1
                        WEnd

        EndSwitch
WEnd

Func sendmail()
        Sleep(2000)
        ;.....发送邮件
EndFunc   ;==>sendmail

Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
        If $pause = False And $lParam = GUICtrlGetHandle($Stop) Then Dim $tmp = GUICtrlSetData($Label, 'Send ' & $var & ' 停止中...'), $pause = True
EndFunc   ;==>WM_COMMAND

评分

参与人数 1贡献 +5 收起 理由
lynfr8 + 5

查看全部评分

发表于 2010-3-4 20:49:27 | 显示全部楼层
谢谢了,事件模式基本掌握。
发表于 2010-3-5 03:20:03 | 显示全部楼层
回复 15# haorui658


    学习了
发表于 2010-3-5 04:09:16 | 显示全部楼层
本帖最后由 foboy 于 2010-3-5 04:17 编辑

20楼的代码同样需要一个循环完成以后才能判断是否暂停的标志。运行的过程是一样的。只是实现的方法不同。不过也学习了。也许以后有用。
 楼主| 发表于 2010-3-5 10:15:08 | 显示全部楼层
本帖最后由 haorui658 于 2010-3-7 13:59 编辑

回复 20# afan


    very good,本来看了蛋蛋的还是不会用,在琢磨中,这个例子好
发表于 2012-4-1 10:31:50 | 显示全部楼层
不错 了解一下
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-17 05:29 , Processed in 0.071422 second(s), 14 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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