找回密码
 加入
搜索
查看: 5702|回复: 10

[GUI管理] OnEvent模式在子程序里面的表现[已解决]

  [复制链接]
发表于 2012-1-3 10:06:11 | 显示全部楼层 |阅读模式
本帖最后由 eaglelin 于 2012-1-3 13:34 编辑

我昨天试了一下OnEvent模式
不是以前没用过这个模式。而是我在主程序里面用了宏@GuiCtrlID是成功的,但是在子程序里面的GUI用@GuiCtrlID  出现了控件没有反应的情况。我反复调试,没有结果。所以来问问
#include <GuiConstants.au3>

GUICreate("MyGUI", 372, 88, -1, -1)

$Progress_1 = GUICtrlCreateProgress(10, 10, 370, 20)
$Button_2 = GUICtrlCreateButton("start", 10, 40, 60, 30)
$Button_3 = GUICtrlCreateButton("pause", 80, 40, 60, 30)
$Button_4 = GUICtrlCreateButton("exit", 150, 40, 70, 30)

Opt("GUIOnEventMode", 1)

Dim $start = 0, $ostart

GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
GUICtrlSetOnEvent($Button_2, "gui")
GUICtrlSetOnEvent($Button_3, "gui")
GUICtrlSetOnEvent($Button_4, "gui")

While 1
    If $start >= 0 Then
        Sleep(50)
        $start += 1
        If $start > 100 Then $start = 0
        If $start >= 0 Then
            $ostart = $start
            GUICtrlSetData($Progress_1, $start)
        Else
            ContinueLoop
        EndIf
    EndIf
WEnd

Exit

Func gui()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE, $Button_4
            Exit
        Case $Button_2
            subfunc()
        Case $Button_3
            If $start = -100 Then
                $start = $ostart
            Else
                $start = -100
            EndIf
    EndSwitch
EndFunc   ;==>gui

Func  subfunc()

GUICreate("SubGUI", 372, 88, -1, -1)
$Button_2 = GUICtrlCreateButton("start1", 10, 40, 60, 30)
$Button_4 = GUICtrlCreateButton("exit", 150, 40, 70, 30)
GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "subgui")
GUICtrlSetOnEvent($Button_2, "subgui")
GUICtrlSetOnEvent($Button_4, "subgui")
While 1
WEnd
 Sleep(100)
EndFunc

Func subgui()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE, $Button_4
            Exit
        Case $Button_2
            MsgBox(1,"","Sub OK")
    EndSwitch
EndFunc   ;==>gui
 楼主| 发表于 2012-1-3 11:40:45 | 显示全部楼层
贴上了源码
发表于 2012-1-3 12:01:07 | 显示全部楼层
本帖最后由 xiehuahere 于 2012-1-3 12:44 编辑

回复 1# eaglelin


    subfunc里面,

1)控件ID建议跟主GUI的区分开来,并且要声明为全局变量,否则subgui函数中识别不到这些控件ID。

2)  GUISetState() 应该在所有GUICtrlSetOnEvent动作都做完之后。
   即,要先为各个控件注册事件响应函数,然后再show出GUI来。
  
3)那个 Sleep(100) 应该是在 While 1循环里面吧?这个While循环不需要,因为主GUI已经有一个主循环了,依靠这个即可。

还有,你再看看吧。
发表于 2012-1-3 12:05:34 | 显示全部楼层
另外,你有没有发现,点击Pause暂停后,要再点击Pause按钮两次才能恢复进度显示。

建议 gui 函数中,
Case $Button_3
            If $start = -100 Then
                $start = $ostart
            Else
                $start = -100
            EndIf
改一下判断条件:
Case $Button_3
            If $start < 0 Then
                $start = $ostart
            Else
                $start = -100
            EndIf
参见:
http://www.autoitx.com/forum.php ... mp;extra=#pid382849
发表于 2012-1-3 12:07:48 | 显示全部楼层
楼上的说法,我没弄过,不知道
但是为什么非要GUICtrlSetOnEvent到一个函数呢
一个按钮一个函数不是更简单清楚吗?
发表于 2012-1-3 12:12:50 | 显示全部楼层
回复 5# seniors


    看一下 OnEvent模式进阶教程:
http://www.autoitx.com/forum.php ... p;extra=&page=1

估计是参考了这个。
发表于 2012-1-3 12:23:39 | 显示全部楼层
subfunc里面的
While 1
WEnd
去掉
发表于 2012-1-3 12:42:03 | 显示全部楼层
本帖最后由 xiehuahere 于 2012-1-3 12:43 编辑

试试这个吧:
#include <GuiConstants.au3>

Global $subGUI, $Button_5, $Button_6
GUICreate("MyGUI", 372, 88, -1, -1)

$Progress_1 = GUICtrlCreateProgress(10, 10, 370, 20)
$Button_2 = GUICtrlCreateButton("start", 10, 40, 60, 30)
$Button_3 = GUICtrlCreateButton("pause", 80, 40, 60, 30)
$Button_4 = GUICtrlCreateButton("exit", 150, 40, 70, 30)

Opt("GUIOnEventMode", 1)

Dim $start = 0, $ostart

GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "gui")
GUICtrlSetOnEvent($Button_2, "gui")
GUICtrlSetOnEvent($Button_3, "gui")
GUICtrlSetOnEvent($Button_4, "gui")

While 1
    If $start >= 0 Then
        Sleep(50)
        $start += 1
        If $start > 100 Then $start = 0
        If $start >= 0 Then
            $ostart = $start
            GUICtrlSetData($Progress_1, $start)
        Else
            ContinueLoop
        EndIf
    EndIf
WEnd

Exit

Func gui()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE, $Button_4
            Exit
        Case $Button_2
            subfunc()
        Case $Button_3
            If $start < 0 Then
                $start = $ostart
            Else
                $start = -100
            EndIf
    EndSwitch
EndFunc   ;==>gui

Func  subfunc()
        $subGUI = GUICreate("SubGUI", 372, 88, -1, -1)
        $Button_5 = GUICtrlCreateButton("start1", 10, 40, 60, 30)
        $Button_6 = GUICtrlCreateButton("exit", 150, 40, 70, 30)
        GUISetOnEvent($GUI_EVENT_CLOSE, "subgui")
        GUICtrlSetOnEvent($Button_5, "subgui")
        GUICtrlSetOnEvent($Button_6, "subgui")
        GUISetState()
EndFunc
Func subgui()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE, $Button_6
            GUIDelete($subGUI)
        Case $Button_5
            MsgBox(1,"","Sub OK")
    EndSwitch
EndFunc   ;==>gui
 楼主| 发表于 2012-1-3 13:25:08 | 显示全部楼层
本帖最后由 eaglelin 于 2012-1-3 13:35 编辑

哦!就是说控件ID不能重复是吧! 谢谢了 !
 楼主| 发表于 2012-1-3 13:25:31 | 显示全部楼层
哦!就是说控件ID不能重复是吧!
发表于 2015-4-28 14:02:40 | 显示全部楼层
8楼好事例,谢谢。
我在进阶教程那里没看到子窗口里用按钮退出子窗口的说法。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-18 12:43 , Processed in 0.081322 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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