找回密码
 加入
搜索
查看: 5628|回复: 8

[AU3基础] 如何控制ListView中带CHECKBOXES的item选取?[已解决]

  [复制链接]
发表于 2012-8-6 12:08:35 | 显示全部楼层 |阅读模式
本帖最后由 palkiller 于 2012-8-14 10:30 编辑

代码如下:
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
Global $hListView,$count_select,$hListView,$hLabel
$count_select=0
GUICreate("ListView Click Item", 400, 300)
$hListView = GUICtrlCreateListView("", 2, 2, 394, 268)
_GUICtrlListView_SetExtendedListViewStyle(-1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_CHECKBOXES))
$hLabel = GUICtrlCreateLabel("", 2, 275, 394, 15)
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
_GUICtrlListView_InsertColumn($hListView, 0, "Column 1", 200)
_GUICtrlListView_InsertColumn($hListView, 0, "Column 2", 150)
For $i=1 To 20
        _GUICtrlListView_AddItem($hListView, "Row "&$i&": Col 1", $i)
Next
GUICtrlSetData($hLabel,"已选定"&$count_select[0]&"个item")
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
GUIDelete()

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
        Local $hWndFrom, $iCode, $tNMHDR, $hWndListView
        $hWndListView = $hListView
        If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)
        $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
        $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
        $iCode = DllStructGetData($tNMHDR, "Code")
        Switch $hWndFrom
                Case $hWndListView
                        Switch $iCode
                                Case $NM_CLICK 
                                        $Index = _GUICtrlListView_GetSelectionMark($hListView)
                                        If _GUICtrlListView_GetItemFocused($hListView,$Index)=True Then
                                                $check=_GUICtrlListView_GetItemChecked($hListView,$Index)
                                                If $check=True Then
                                                        _GUICtrlListView_SetItemChecked($hListView,$Index,False)
                                                        $count_select-=1
                                                Else
                                                        _GUICtrlListView_SetItemChecked($hListView,$Index)
                                                        $count_select+=1
                                                EndIf
                                        EndIf
                                        GUICtrlSetData($hLabel,"已选定"&$count_select&"个item")
                        EndSwitch
        EndSwitch
        Return $GUI_RUNDEFMSG
EndFunc
 
这段代码需要实现的是当鼠标单击item时,如果复选框状态为UnChecked状态,则复选框状态更改为Checked,计数器+1,如果复选框状态为Checked,则更改为UnChecked,计数器-1,但是在实际运行过程中发现,鼠标位置在复选框处时点击,计数器的值会更改,但是复选框状态有时不能正常转换...
上面讲的有点乱,刚找到一个帖子,是TreeView的复选框问题,俺要实现的效果跟他是一样的。。。http://www.autoitx.com/forum.php?mod=viewthread&tid=33640&highlight=%B8%B4%D1%A1%BF%F2

感谢yhxhappy给的思路,用AdlibRegister注册函数去计算item被Checked的数量的确能实现。。
 楼主| 发表于 2012-8-10 13:20:44 | 显示全部楼层
顶自己一下...本来打算用p版的ListViewEditEvent.au3函数试试看,结果调试时一直报错...郁闷...
发表于 2012-8-10 20:26:24 | 显示全部楼层
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

GUICreate("ListView Click Item", 400, 300)
$hListView = GUICtrlCreateListView("", 2, 2, 394, 268, "", $LVS_EX_FULLROWSELECT+$LVS_EX_CHECKBOXES)
        _GUICtrlListView_InsertColumn($hListView, 0, "Column 1", 200)
        _GUICtrlListView_InsertColumn($hListView, 0, "Column 2", 150)

$hLabel = GUICtrlCreateLabel("", 2, 275, 394, 15)

GUISetState()

Local $LV_Item[1], $CheckNum = 0
For $i = 1 To 20
        _ArrayAdd($LV_Item, GUICtrlCreateListViewItem("Row "&$i&": Col 1", $hListView))
Next
$LV_Item[0] = UBound($LV_Item)-1

While 1
        $Msg = GUIGetMsg()
        Switch $Msg
        Case $GUI_EVENT_CLOSE
                Exit
        Case $LV_Item[1] To $LV_Item[$LV_Item[0]]
                $Index = _GUICtrlListView_GetHotItem($hListView)
                If _GUICtrlListView_GetItemChecked($hListView, $Index) Then
                        _GUICtrlListView_SetItemChecked($hListView, $Index, False)
                        $CheckNum -= 1
                Else
                        _GUICtrlListView_SetItemChecked($hListView, $Index, True)
                        $CheckNum += 1
                EndIf
                GUICtrlSetData($hLabel, "已选定"&$CheckNum&"个item")
        EndSwitch
WEnd
 楼主| 发表于 2012-8-10 21:07:27 | 显示全部楼层
回复 3# yhxhappy


感谢LS回帖,但是经测试,代码无效,点任何一个checkbox,就会选中所有的...-_-|||
发表于 2012-8-11 00:01:49 | 显示全部楼层
回复 4# palkiller

不好意思,之前没测试清楚,光顾着点项目了,现修改了下
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

GUICreate("ListView Click Item", 400, 300)
$hListView = GUICtrlCreateListView("", 2, 2, 394, 268, "", $LVS_EX_FULLROWSELECT+$LVS_EX_CHECKBOXES)
        _GUICtrlListView_InsertColumn($hListView, 0, "Column 1", 200)
        _GUICtrlListView_InsertColumn($hListView, 0, "Column 2", 150)

$hLabel = GUICtrlCreateLabel("", 2, 275, 394, 15)

GUISetState()

Local $LV_Item[1], $CheckNum = 0
For $i = 1 To 20
        _ArrayAdd($LV_Item, GUICtrlCreateListViewItem("Row "&$i&": Col 1", $hListView))
Next
$LV_Item[0] = UBound($LV_Item)-1

While 1
        $Msg = GUIGetMsg()
        Switch $Msg
        Case $GUI_EVENT_CLOSE
                Exit
        Case $LV_Item[1] To $LV_Item[$LV_Item[0]]
                $Index = _GUICtrlListView_GetHotItem($hListView)                
                If $Index = -1 Then
                        If GUICtrlRead($Msg,1) = 4 Then
                                $CheckNum -= 1
                        Else
                                $CheckNum += 1
                        EndIf
                Else
                        If GUICtrlRead($Msg,1) = 4 Then
                                GUICtrlSetState($Msg, $GUI_CHECKED)
                                $CheckNum += 1
                        Else
                                GUICtrlSetState($Msg, $GUI_UNCHECKED)
                                $CheckNum -= 1
                        EndIf                
                EndIf
                GUICtrlSetData($hLabel, "已选定"& $CheckNum &"个item")
        EndSwitch
WEnd
 楼主| 发表于 2012-8-12 14:28:26 | 显示全部楼层
回复 5# yhxhappy


郁闷,LS的代码在处理双击跟单击时存在问题...看样子只能用GUICtrlCreateListViewItem跟GUIRegisterMsg结合起来用了...不过挺担心效率的...等会儿自己改着试下看看...

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2012-8-12 23:10:10 | 显示全部楼层
回复 6# palkiller
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

GUICreate("ListView Click Item", 400, 300)
$hListView = GUICtrlCreateListView("", 2, 2, 394, 268, "", $LVS_EX_FULLROWSELECT+$LVS_EX_CHECKBOXES)
        _GUICtrlListView_InsertColumn($hListView, 0, "Column 1", 200)
        _GUICtrlListView_InsertColumn($hListView, 0, "Column 2", 150)

$hLabel = GUICtrlCreateLabel("已选定0个item", 2, 275, 394, 15)

GUISetState()

Local $LV_Item[1], $CheckNum = 0
For $i = 1 To 20
        _ArrayAdd($LV_Item, GUICtrlCreateListViewItem("Row "&$i&": Col 1", $hListView))
Next
$LV_Item[0] = UBound($LV_Item)-1


AdlibRegister("xxxx")
While 1
        $Msg = GUIGetMsg()
        Switch $Msg
        Case $GUI_EVENT_CLOSE
                Exit
        Case $LV_Item[1] To $LV_Item[$LV_Item[0]]
                $Index = _GUICtrlListView_GetHotItem($hListView)                
                If $Index <> -1 Then
                        If GUICtrlRead($Msg,1) = 4 Then
                                GUICtrlSetState($Msg, $GUI_CHECKED)
                        Else
                                GUICtrlSetState($Msg, $GUI_UNCHECKED)
                        EndIf                
                EndIf
        EndSwitch
WEnd

Func xxxx()
        $aNum = 0
        For $a = 1 To $LV_Item[0]
                If GUICtrlRead($LV_Item[$a],1) = 1 Then
                        $aNum += 1
                EndIf
        Next
        If $aNum <> $CheckNum Then
                GUICtrlSetData($hLabel, "已选定"& $aNum &"个item")
                $CheckNum = $aNum
        EndIf
EndFunc
发表于 2012-8-12 23:49:18 | 显示全部楼层
多逛逛论坛总会有好处的!各位勿怪!
发表于 2015-12-4 20:44:29 | 显示全部楼层
学习了,但是为什么选择checkbox时不能关联所选行呢?
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-28 22:19 , Processed in 0.084260 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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