找回密码
 加入
搜索
查看: 2438|回复: 4

[AU3基础] (已解决)按钮消息不能实时获取

[复制链接]
发表于 2010-12-19 13:09:03 | 显示全部楼层 |阅读模式
本帖最后由 330259789 于 2010-12-19 21:04 编辑

用While 循环运行主程序,当按下按钮时弹出对话框,可是按了以后得循环十几次才跳出来,咋反应这么慢的啊?代码如下
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 623, 442, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 40, 32, 75, 25)
$Button2 = GUICtrlCreateButton("Button2", 136, 32, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        MsgBox(0, 0, "button1被按下")
                Case $Button2
                        MsgBox(0, 0, "button2被按下")
        EndSwitch
        g()
WEnd
Func g()
        Sleep(1000)
        MsgBox(0, 0,"主程序" )
EndFunc   ;==>g
发表于 2010-12-19 13:58:29 | 显示全部楼层
这样写肯定反应慢了。
你把主程序放在WHILE里面,不断的循环运行,那两个按钮是干啥用的?把主程序放在按钮事件里面不行吗?

下面代码改成了事件模式
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)                                                                        ;切换至 事件模式

$Form1 = GUICreate("Form1", 623, 442)
GUISetOnEvent($GUI_EVENT_CLOSE, "g")
$Button1 = GUICtrlCreateButton("Button1", 40, 32, 75, 25)
GUICtrlSetOnEvent(-1, "g")
$Button2 = GUICtrlCreateButton("Button2", 136, 32, 75, 25)
GUICtrlSetOnEvent(-1, "g")
GUISetState(@SW_SHOW)


While 1
        Sleep(1000)
        ToolTip(@HOUR &":"& @MIN &":"& @SEC, @DesktopWidth/2, @DesktopHeight/2, "主程序运行中")
WEnd

Func g()
        Switch @GUI_CtrlId
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button1
                        MsgBox(0, 0, "button1被按下")
                Case $Button2
                        MsgBox(0, 0, "button2被按下")
        EndSwitch
EndFunc   ;==>g
发表于 2010-12-19 17:05:54 | 显示全部楼层
消息循环中的g()
当中不能有SLEEP()
不管sleep(多少时间)
都会造成消息循环函数(有自动挂起并闲置时间的功能)反应迟钝
!
 楼主| 发表于 2010-12-19 21:03:22 | 显示全部楼层
回复 3# _ddqs.
原来这样啊~!又学会一招,谢谢
 楼主| 发表于 2010-12-19 21:04:05 | 显示全部楼层
回复 2# yhxhappy
恩,改成了事件模式果然OK了。。
谢谢~!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-4-28 21:05 , Processed in 0.081020 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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