找回密码
 加入
搜索
查看: 7544|回复: 16

[GUI管理] 按钮数组如何触发按钮点击事件【已解决】

 火.. [复制链接]
发表于 2011-9-19 23:07:18 | 显示全部楼层 |阅读模式
本帖最后由 xms77 于 2011-9-20 16:48 编辑

我在一个GUI窗体上创建了几十个按钮,我给按钮编了一个数组,但是不知道如何来触发按钮的点击事件,难道需要一个一个按钮来写几乎相同的点击事件吗?有没有高效的方法?
发表于 2011-9-20 03:10:11 | 显示全部楼层
既然是数组就可以用循环注册事件
 楼主| 发表于 2011-9-20 09:51:24 | 显示全部楼层
回复 2# 风行者
兄弟你真行,这么晚还不睡觉!能不能给个例子给我研究一下,谢谢了!
发表于 2011-9-20 13:19:39 | 显示全部楼层
以前发过此类帖子,搜不到了,可能忘了加by afan 了,^ *
Opt('GUIOnEventMode', 1) ;----事件模式

GUICreate('批量生成按钮及响应 By Afan', 380, 270)
GUISetOnEvent(-3, '_zx')
Local $_c = 10
Local $aBInfo[$_c + 1][2] = [[$_c]]
For $i = 1 To $_c
        $aBInfo[$i][0] = '按钮' & $i
        $aBInfo[$i][1] = '按钮' & $i & '的任务'
Next
Local $Button[$aBInfo[0][0]]
For $x = 1 To $_c
        $Button[$x - 1] = GUICtrlCreateButton($aBInfo[$x][0], 16 + 175 * ($x - (Int(($x - 1) / 2) * 2 + 1)), 36 + 35 * Int(($x - 1) / 2), 170, 21)
        GUICtrlSetOnEvent(-1, '_zx')
Next

GUISetState()

While 1
        Sleep(100)
WEnd

Func _zx()
        Switch @GUI_CtrlId
                Case -3
                        Exit
                Case $Button[0] To $Button[$_c - 1]
                        MsgBox(0, 0, $aBInfo[@GUI_CtrlId - 2][1])

        EndSwitch
EndFunc   ;==>_zx
 楼主| 发表于 2011-9-20 15:21:36 | 显示全部楼层
回复 4# afan
感谢Afan大大,研究一下你的代码。不知道循环消息模式有没有可能实现?
发表于 2011-9-20 15:37:10 | 显示全部楼层
回复 5# xms77


    一样的思路,我习惯用事件模式而已
GUICreate('批量生成按钮及响应', 380, 270)
Local $_c = 10
Local $aBInfo[$_c + 1][2] = [[$_c]]
For $i = 1 To $_c
        $aBInfo[$i][0] = '按钮' & $i
        $aBInfo[$i][1] = '按钮' & $i & '的任务'
Next
Local $Button[$aBInfo[0][0]]
For $x = 1 To $_c
        $Button[$x - 1] = GUICtrlCreateButton($aBInfo[$x][0], 16 + 175 * ($x - (Int(($x - 1) / 2) * 2 + 1)), 36 + 35 * Int(($x - 1) / 2), 170, 21)
Next

GUISetState()

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case -3
                        Exit
                Case $Button[0] To $Button[$_c - 1]
                        MsgBox(0, 0, $aBInfo[$nMsg - 2][1])
        EndSwitch
WEnd
发表于 2011-9-20 16:25:15 | 显示全部楼层
回复 6# afan

事件模式才是正道呀!死循环的都是浮云!顶你!
 楼主| 发表于 2011-9-20 16:45:31 | 显示全部楼层
本帖最后由 xms77 于 2011-9-20 16:56 编辑

回复 7# gzh888666
呵呵,原来一直用消息模式,顺手了,现在试试时间模式,学习,练习,实践出真知,感谢afan,总算用事件模式解决了
 楼主| 发表于 2011-9-20 16:47:57 | 显示全部楼层
借用afan的代码总算搞定了,感谢afan,论坛由你而精彩
GUISetState()

UDPStartup()
$socket = UDPBind(@IPAddress1, 65532)
If @error <> 0 Then 
        MsgBox(0,"Error", "Server port open error")
        $Server_open = 0
EndIf
While 1
        ;If $Server_open <> 0 Then
                $data = UDPRecv($socket, 128,1)
                If $data <> "" Then
                        $data = BinaryToString($data,4)
                        $Data_array = StringSplit($data,"|")
                        ;_ArrayDisplay($Data_array)
                        $Number = StringRight($Data_array[1],2)
                        $Number = $Number - 20
                        GUICtrlSetData($CellID[$Number],$Data_array[2])
                        GUICtrlSetData($Product[$Number],$Data_array[3])
                        GUICtrlSetImage($pic[$Number], "C:\windows\online.jpg")
                        GUICtrlSetData($IP[$Number],$Data_array[4])
                        ;GUICtrlSetColor($IP[$Number],"0x00ff00")
                EndIf
        ;EndIf
        Sleep(100)
WEnd        

Func _zx()
        Switch @GUI_CtrlId
                Case -3
                        Exit
                Case $Mc_ID[0] To $Mc_ID[62]
                    $GetIp = GUICtrlRead($IP[@GUI_CtrlId-8])
                                        StringRegExp($GetIp,"\d{2}\.\d{2}\.\d{2,3}\.\d{2,3}", 1)
                                        If @error <> 1 Then  _RunVnc($GetIp)                                        
        EndSwitch
EndFunc   ;==>_zx
        
        
;=== Run VNC and auto logon remote PC ====================================================
Func _RunVNC($iIP)
        Run("C:\Program Files\RealVNC\VNC4\vncviewer.exe")
        WinWait("VNC Viewer : Connection Details")
        WinActivate("VNC Viewer : Connection Details")
        WinWaitActive("VNC Viewer : Connection Details")
        ControlSetText("VNC Viewer : Connection Details","","Edit1",$iIP)
        ControlClick("VNC Viewer : Connection Details", "OK", "Button3")
        WinWait("VNC Viewer : Authentication [No Encryption]")
        WinActivate("VNC Viewer : Authentication [No Encryption]")
        WinWaitActive("VNC Viewer : Authentication [No Encryption]")
        ControlSetText("VNC Viewer : Authentication [No Encryption]","","Edit2","123")
        ControlClick("VNC Viewer : Authentication [No Encryption]", "OK", "Button1")
EndFunc    ;==> _RunVnc
发表于 2011-9-20 17:09:34 | 显示全部楼层
谢谢,学习了
 楼主| 发表于 2011-9-21 09:40:22 | 显示全部楼层
回复 10# 紫色风林
不用谢,我也是刚刚开始学习!共勉吧!
 楼主| 发表于 2011-9-21 09:42:13 | 显示全部楼层
回复 6# afan
原来不知道case 里面也可以用xxx[0] to xxx[n]语句的,现在知道了,感谢!
发表于 2011-10-5 01:03:47 | 显示全部楼层
学习了~~谢谢
发表于 2011-10-10 20:52:31 | 显示全部楼层
留个脚印  方便查询   数组按钮
发表于 2011-10-11 14:30:21 | 显示全部楼层
过来学习了~~
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-10-1 15:32 , Processed in 0.103612 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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