如何控制ListView中带CHECKBOXES的item选取?[已解决]
本帖最后由 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&"个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的数量的确能实现。。 顶自己一下...本来打算用p版的ListViewEditEvent.au3函数试试看,结果调试时一直报错...郁闷... #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, $CheckNum = 0
For $i = 1 To 20
_ArrayAdd($LV_Item, GUICtrlCreateListViewItem("Row "&$i&": Col 1", $hListView))
Next
$LV_Item = UBound($LV_Item)-1
While 1
$Msg = GUIGetMsg()
Switch $Msg
Case $GUI_EVENT_CLOSE
Exit
Case $LV_Item To $LV_Item[$LV_Item]
$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 回复 3# yhxhappy
感谢LS回帖,但是经测试,代码无效,点任何一个checkbox,就会选中所有的...-_-||| 回复 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, $CheckNum = 0
For $i = 1 To 20
_ArrayAdd($LV_Item, GUICtrlCreateListViewItem("Row "&$i&": Col 1", $hListView))
Next
$LV_Item = UBound($LV_Item)-1
While 1
$Msg = GUIGetMsg()
Switch $Msg
Case $GUI_EVENT_CLOSE
Exit
Case $LV_Item To $LV_Item[$LV_Item]
$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 回复 5# yhxhappy
郁闷,LS的代码在处理双击跟单击时存在问题...看样子只能用GUICtrlCreateListViewItem跟GUIRegisterMsg结合起来用了...不过挺担心效率的...等会儿自己改着试下看看... 回复 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, $CheckNum = 0
For $i = 1 To 20
_ArrayAdd($LV_Item, GUICtrlCreateListViewItem("Row "&$i&": Col 1", $hListView))
Next
$LV_Item = UBound($LV_Item)-1
AdlibRegister("xxxx")
While 1
$Msg = GUIGetMsg()
Switch $Msg
Case $GUI_EVENT_CLOSE
Exit
Case $LV_Item To $LV_Item[$LV_Item]
$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
If GUICtrlRead($LV_Item[$a],1) = 1 Then
$aNum += 1
EndIf
Next
If $aNum <> $CheckNum Then
GUICtrlSetData($hLabel, "已选定"& $aNum &"个item")
$CheckNum = $aNum
EndIf
EndFunc 多逛逛论坛总会有好处的!各位勿怪! 学习了,但是为什么选择checkbox时不能关联所选行呢?
页:
[1]