找回密码
 加入
搜索
查看: 5119|回复: 7

[GUI管理] 如何给ListView添加鼠标的点击事件?急

  [复制链接]
发表于 2012-10-14 17:37:37 | 显示全部楼层 |阅读模式
是指点击某行后,直接运行函数的,而不是通过获取焦点间接实现的,拜谢了
发表于 2012-10-14 21:23:01 | 显示全部楼层
那就用onevent模式了

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

GUICreate("", 300, 200, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
$listview = GUICtrlCreateListView("         1        |2", 5, 5, 290, 190)

For $i = 1 To 10
        GUICtrlCreateListViewItem("第" & $i & "行|第二列", $listview)
        GUICtrlSetOnEvent(-1, "_test")
Next

GUISetState()

While 1
        Sleep(10)
WEnd

Func _test()
        MsgBox(0, "", GUICtrlRead(@GUI_CtrlId))
EndFunc   ;==>_test

Func _exit()
        Exit
EndFunc   ;==>_exit


不过我觉得通过焦点来获取挺好的啊

#include <GuiListView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')

GUICreate("", 300, 200, -1, -1)
$listview = GUICtrlCreateListView("                 |", 5, 5, 290, 190)
GUICtrlCreateListViewItem("www.baidu.com", $listview)
GUICtrlCreateListViewItem("www.google.com", $listview)
GUISetState()

Do
        Sleep(10)
        
Until GUIGetMsg() = -3

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
        #forceref $hWnd, $iMsg, $iwParam
        Local $tNMHDR, $hWndFrom, $iCode

        $hWndListView = GUICtrlGetHandle($listview)

        $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) ; $tagNMHDR - Contains information about a notification message
        $hWndFrom = DllStructGetData($tNMHDR, 'hWndFrom')
        $iCode = DllStructGetData($tNMHDR, 'Code')

        Switch $hWndFrom
                Case $hWndListView
                        Switch $iCode
                                Case $NM_CLICK ; Sent by a list-view control when user clicks an item with left mouse button
                                        $aHIT = _GUICtrlListView_SubItemHitTest($hWndListView)
                                        If _GUICtrlListView_GetItemText($listview, $aHIT[0]) <> "" Then MsgBox(0, "", _GUICtrlListView_GetItemText($listview, $aHIT[0]))
                        EndSwitch
        EndSwitch
EndFunc   ;==>WM_NOTIFY

发表于 2012-10-14 21:34:42 | 显示全部楼层
回复 2# zch11230


    膜拜高淫,看了好久的ListView鼠标点击代码,还是看不懂...
 楼主| 发表于 2012-10-15 00:24:12 | 显示全部楼层
回复 2# zch11230

先谢谢了,明天测试下
 楼主| 发表于 2012-10-21 16:22:48 | 显示全部楼层
回复 2# zch11230


    双击呢,有办法不?
发表于 2012-10-21 16:40:28 | 显示全部楼层
回复 5# zuiyi

难道都不看帮助的吗?
发表于 2012-10-21 16:52:13 | 显示全部楼层
本帖最后由 xms77 于 2012-10-21 16:56 编辑

]回复 5# zuiyi
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

Func WM_NOTIFY($hWndGUI, $MsgID, $WParam, $LParam)
        Local $tagNMHDR, $Event, $hWndFrom, $IDFrom
        Local $tagNMHDR = DllStructCreate("int;int;int", $LParam)
                Local $Index,$itemText,$iMachineId,$answer
        If @error Then Return $GUI_RUNDEFMSG
        $IDFrom = DllStructGetData($tagNMHDR, 2)
        $Event = DllStructGetData($tagNMHDR, 3)
        $tagNMHDR = 0
        Switch $IDFrom;选择产生事件的控件
                Case $hListView
                    Switch $Event; 选择产生的事件
                                                Case $NM_CLICK ; 左击
                                                        ;...
                         Case $NM_DBLCLK ; 双击
                                                        ;...
                                                Case $NM_RCLICK ; 右击
                                                        ;...
                    EndSwitch
        EndSwitch
        Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
 楼主| 发表于 2012-10-21 19:28:37 | 显示全部楼层
回复 7# xms77


  谢谢了
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-17 17:30 , Processed in 0.079787 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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