找回密码
 加入
搜索
查看: 8072|回复: 15

[GUI管理] [已解决]GUICtrlSetOnEvent调用带参数的函数,就会直接运行,如何解决?

  [复制链接]
发表于 2011-7-22 16:33:42 | 显示全部楼层 |阅读模式
本帖最后由 daiyu116 于 2011-8-11 16:08 编辑

如题,使用GUICtrlSetOnEvent调用带参数的函数,就会直接运行该函数,而不是等到点击了按钮之后才运行相应的函数。如何解决?

#include <GUIConstantsEx.au3>
Opt("GUIOnEventMode", 1)
Example()

Func Example()
        Local $Button_1, $Button_2, $msg
        GUICreate("My GUI Button") ; 创建一个对话框,并居中显示

        Opt("GUICoordMode", 2)
        $Button_1 = GUICtrlCreateButton("打开记事本", 10, 30, 100)
                GUICtrlSetOnEvent($Button_1,Button_1(2))
        $Button_2 = GUICtrlCreateButton("测试按钮", 0, -1)
                GUICtrlSetOnEvent($Button_2,"Button_2")
        GUISetState()      ; 显示有两个按钮的对话框
                GUISetOnEvent($GUI_EVENT_CLOSE,"_exit")
        ; 运行界面,直到窗口被关闭
        While 1
                Sleep(1)
        WEnd
EndFunc   ;==>Example

Func _exit()
        Exit
EndFunc

Func Button_1($kind)
        If $kind=1 Then 
                Run('Notepad.exe') 
        Else
                MsgBox(0, '错误','错误!')
        EndIf
EndFunc

Func Button_2()
        MsgBox(0, '测试', '你点击了测试按钮')
EndFunc
 楼主| 发表于 2011-7-22 17:02:30 | 显示全部楼层
本帖最后由 daiyu116 于 2011-7-22 17:14 编辑

回复 1# daiyu116

目前已找到一种非常变通的解决方式:
即利用Assign给变量赋值,代码如下。但是这并不代表着题目就解决了。如有其他方式,请大侠指点,本帖在5天内不改为[已解决],请管理员协助。嘿嘿~

#include <GUIConstantsEx.au3>
Opt("GUIOnEventMode", 1)
Example()
 
Func Example()
        Local $Button_1, $Button_2, $msg
        GUICreate("My GUI Button") ; 创建一个对话框,并居中显示
 
        Opt("GUICoordMode", 2)
        $Button_1 = GUICtrlCreateButton("打开记事本", 10, 30, 100)
                GUICtrlSetOnEvent($Button_1,"Button_1")
                                Global $var=Assign($Button_1,999999)
        $Button_2 = GUICtrlCreateButton("测试按钮", 0, -1)
                GUICtrlSetOnEvent($Button_2,'Button_2')
        GUISetState()      ; 显示有两个按钮的对话框
                GUISetOnEvent($GUI_EVENT_CLOSE,"_exit")
        ; 运行界面,直到窗口被关闭
        While 1
                Sleep(1)
        WEnd
EndFunc   ;==>Example
 
Func _exit()
        Exit
EndFunc
        
Func Button_1()
        If $var=1 Then 
                Run('Notepad.exe') 
        Else
                MsgBox(0, '错误','错误!')
        EndIf
EndFunc
 
Func Button_2()
        MsgBox(0, '测试', '你点击了测试按钮')
EndFunc
发表于 2011-7-22 17:04:52 | 显示全部楼层
#include <GUIConstantsEx.au3>
Opt("GUIOnEventMode", 1)
Example()

Func Example()
        Local $Button_1, $Button_2, $msg
        GUICreate("My GUI Button") ; 创建一个对话框,并居中显示
        
        $Button_1 = GUICtrlCreateButton("打开记事本", 10, 30, 100)
        GUICtrlSetOnEvent($Button_1, "OnClick")
        
        $Button_2 = GUICtrlCreateButton("测试按钮", 110, -1)
        GUICtrlSetOnEvent($Button_2, "Button_2")
        GUISetState() ; 显示有两个按钮的对话框
        GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
        ; 运行界面,直到窗口被关闭
        While 1
                Sleep(1)
        WEnd
EndFunc   ;==>Example

Func _exit()
        Exit
EndFunc   ;==>_exit

Func Button_1($kind)
        If $kind = 1 Then
                Run('Notepad.exe')
        Else
                MsgBox(0, '错误', '错误!')
        EndIf
EndFunc   ;==>Button_1

Func Button_2()
        MsgBox(0, '测试', '你点击了测试按钮')
EndFunc   ;==>Button_2

Func OnClick()
        Button_1(2)
EndFunc   ;==>OnClick

 楼主| 发表于 2011-7-22 17:15:28 | 显示全部楼层
本帖最后由 daiyu116 于 2011-7-22 17:52 编辑

经过思考,发现我自己刚才发的2楼帖子,与最初的意图完全是南辕北辙~
所以告诉大家:2楼的方法是行不通的,因为Assign赋值后,函数虽然起作用了,但是跟事件模式没有一点联系。
请使用3楼3mile的方法~
 楼主| 发表于 2011-7-22 17:18:50 | 显示全部楼层
本帖最后由 daiyu116 于 2011-7-22 17:20 编辑

回复 3# 3mile

好方法!聪明!
现在看来,产生这个问题的原因,应该是GUICtrlSetOnEvent($Button_1,Button_1(2))这一句。
这里把Button_1(2)当成函数直接调用了。

如果其他大侠有其他方法,敬请指点~来者不拒啊
 楼主| 发表于 2011-7-22 18:09:40 | 显示全部楼层
回复 5# daiyu116

还有一种方式,即使用@GUI_CtrlId辨别点击的控件,如下:

#include <GUIConstantsEx.au3>
Opt("GUIOnEventMode", 1)
Example()
 
Func Example()
        GUICreate("My GUI Button") ; 创建一个对话框,并居中显示
        Global $Button_1 = GUICtrlCreateButton("打开记事本", 10, 30, 100)
        GUICtrlSetOnEvent($Button_1, "OnClick")
        Global $Button_2 = GUICtrlCreateButton("测试按钮", 110, -1)
        GUICtrlSetOnEvent($Button_2, "OnClick")
        GUISetState() ; 显示有两个按钮的对话框
        GUISetOnEvent($GUI_EVENT_CLOSE,"_exit")
        ; 运行界面,直到窗口被关闭
        While 1
                Sleep(1)
        WEnd
EndFunc   ;==>Example
 
Func _exit()
        Exit
EndFunc   ;==>_exit
 
Func OnClick()
        If @GUI_CtrlId=$Button_1 Then 
        Button_1(1)
        ElseIf @GUI_CtrlId=$Button_2 Then
                Button_2()
        EndIf
EndFunc   ;==>OnClick 
 
Func Button_1($kind)
        If $kind = 1 Then
                Run('Notepad.exe')
        Else
                MsgBox(0, '错误', '错误!')
        EndIf
EndFunc   ;==>Button_1
 
Func Button_2()
        MsgBox(0, '测试', '你点击了测试按钮')
EndFunc   ;==>Button_2
发表于 2011-7-23 03:10:31 | 显示全部楼层
GUICtrlSetOnEvent ( 控件ID, "函数名" )
第二个参数是 函数名
发表于 2011-7-23 09:10:42 | 显示全部楼层
我是来学习的找不到详细的解释呢郁闷
 楼主| 发表于 2011-7-23 21:40:28 | 显示全部楼层
回复 7# sxd
您所说的我知道,但是带参数的函数,直接用这个函数就不行了,得用变通的方式,例如3楼的方式
 楼主| 发表于 2011-7-23 21:40:46 | 显示全部楼层
回复 8# yjw83523

怎么个详细解释法?
发表于 2011-7-31 10:12:37 | 显示全部楼层
学习了,还蛮有用的,谢谢
发表于 2011-7-31 10:42:00 | 显示全部楼层
回复 9# daiyu116

3楼的,实际根本不是传递了参数了,因为OnClick函数中的:Button_1(2),这里的‘2’其实没有什么意义。因为它根本不是Example函数传递过来的。

要想真的从事件模式的Example函数传递参数,只能是定义全局变量
 楼主| 发表于 2011-7-31 19:56:06 | 显示全部楼层
本帖最后由 daiyu116 于 2011-7-31 20:03 编辑

回复 12# happytc

请给个例子?有劳了~
 楼主| 发表于 2011-8-10 14:25:23 | 显示全部楼层
经过思考,12楼所说的全局变量,应该就是控件标识 @GUI_CtrlId(只是使用 event 函数时有效. 请参考 GUICtrlSetOnEvent 函数.)
       而9楼说法我也有不认同的地方,即”因为OnClick函数中的:Button_1(2),这里的‘2’其实没有什么意义“这一句。其中的2是有意义的,就是为了区别Button_1中的参数是1的情况。
发表于 2011-8-10 19:03:55 | 显示全部楼层
学习了。3楼的方法很简单,怎么当初就木有想到捏。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-6-11 16:54 , Processed in 0.085575 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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