关于统计_GUICtrlListView_GetSelectedCount问题[已解决]
本帖最后由 blue_dvd 于 2012-11-18 22:50 编辑想要效果
主要是统计用户点击选择了多少个项目?
代码如下#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.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()
For $i = 1 To 20
GUICtrlCreateListViewItem("Row "&$i&": Col 1", $hListView)
Next
GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY') ;注册侦测用户是否选择
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
GUIDelete()
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $tNMHDR, $hWndFrom, $iCode
$hWndListView = $hListView
If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($HListView)
$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
Local $aHit = _GUICtrlListView_HitTest($hListView)
If $aHit<>-1 And _GUICtrlListView_GetItemChecked($hListView,$aHit)=TrueThen
_GUICtrlListView_SetItemChecked($hListView,$aHit)
_GUICtrlListView_SetItemSelected($hListView, $aHit,True)
ElseIf $aHit<>-1 And _GUICtrlListView_GetItemChecked($hListView,$aHit)=FalseThen
_GUICtrlListView_SetItemChecked($hListView,$aHit,False)
_GUICtrlListView_SetItemSelected($hListView, $aHit,False)
EndIf
GUICtrlSetData($hLabel, "共选定项目为"&_GUICtrlListView_GetSelectedCount($hListView))
EndSwitch
EndSwitch
EndFunc总会出错,不知道什原因?代码比较幼稚,请各位不要笑的太厉害!
解决方法在8楼! 看不懂什么意思 出错的地方原因是你48行后用的Id“$hListView”,而非句柄“$hWndListView”
处理这类问题,建议使用P版的“ListView UDF - ListView消息扩展”
http://www.autoitx.com/forum.php?mod=viewthread&tid=13175 lz这个问题有点毛病,selectcount是获取选定项的计数,不是复选项的计数 回复 1# blue_dvd
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <ListviewConstants.au3>
GUICreate("ListView Click Item", 400, 300)
$hListView = GUICtrlCreateListView("", 2, 2, 394, 268, BitAND($GUI_SS_DEFAULT_LISTVIEW ,BitNOT($LVS_SINGLESEL)))
$hListView = GUICtrlGetHandle($hListView)
_GUICtrlListView_SetExtendedListViewStyle($hListView, $LVS_EX_FULLROWSELECT + $LVS_EX_CHECKBOXES )
_GUICtrlListView_InsertColumn($hListView, 0, "Column 2", 200)
_GUICtrlListView_InsertColumn($hListView, 0, "Column 1", 150)
$hLabel = GUICtrlCreateLabel("已选定0个item", 2, 275, 394, 15)
GUISetState()
For $i = 1 To 20
_GUICtrlListView_addItem($hListView, "Row " & $i & ": Col 1")
Next
GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY') ;注册侦测用户是否选择
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
GUIDelete()
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $tNMHDR, $hWndFrom, $iCode, $countitem = ''
$hWndListView = $hListView
If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)
$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
Local $aHit = _GUICtrlListView_HitTest($hListView)
If $aHit Then
If _GUICtrlListView_GetItemChecked($hListView, $aHit) Then
_GUICtrlListView_SetItemSelected($hListView, $aHit, False)
Else
_GUICtrlListView_SetItemSelected($hListView, $aHit)
EndIf
EndIf
GUICtrlSetData($hLabel, "共选定项目为" & _GUICtrlListView_GetSelectedCount($hListView))
EndSwitch
EndSwitch
EndFunc ;==>WM_NOTIFY 回复 4# netegg
有没有统计复选项计数的函数? 本帖最后由 netegg 于 2012-11-18 18:17 编辑
回复 6# blue_dvd
如果还是单选定(选定和复选不是一个意思)的话,不用函数,判断里设置个累加(累减)的变量就行了 本帖最后由 netegg 于 2012-11-18 18:13 编辑
这是单选的例子
#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <ListviewConstants.au3>
Global$count=0
GUICreate("ListView Click Item", 400, 300)
$hListView = GUICtrlCreateListView("", 2, 2, 394, 268)
$hListView = GUICtrlGetHandle($hListView)
_GUICtrlListView_SetExtendedListViewStyle($hListView, $LVS_EX_FULLROWSELECT + $LVS_EX_CHECKBOXES )
_GUICtrlListView_InsertColumn($hListView, 0, "Column 2", 200)
_GUICtrlListView_InsertColumn($hListView, 0, "Column 1", 150)
$hLabel = GUICtrlCreateLabel("已选定0个item", 2, 275, 394, 15)
GUISetState()
For $i = 1 To 20
_GUICtrlListView_addItem($hListView, "Row " & $i & ": Col 1")
Next
For $i = 0 To _GUICtrlListView_GetItemCount($hListView)
If _GUICtrlListView_GetItemChecked($hListView, $i) Then
$count += 1
EndIf
Next
GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY') ;注册侦测用户是否选择
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd
GUIDelete()
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $tNMHDR, $hWndFrom, $iCode
$hWndListView = $hListView
If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)
$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
Local $aHit = _GUICtrlListView_HitTest($hListView)
If $aHit Then
If _GUICtrlListView_GetItemChecked($hListView, $aHit) Then
;_GUICtrlListView_SetItemSelected($hListView, $aHit, False)
$count -= 1
Else
;_GUICtrlListView_SetItemSelected($hListView, $aHit)
$count += 1
EndIf
EndIf
GUICtrlSetData($hLabel, "共选定项目为" & $count)
EndSwitch
EndSwitch
EndFunc ;==>WM_NOTIFY 回复 8# netegg
多谢了,此为正解也!不过我如果双击项目发现没有选中,但$count却累加了! 本帖最后由 netegg 于 2012-11-18 22:20 编辑
回复 9# blue_dvd
双击的话,消息是不一样的,需要另加一个双击处理才能得到正确的结果
.......
case $nm_dblclk
return 1
.......
回复 5# netegg
还有一个问题:变量$aHit是放什么数据的?查看函数HitTest发现了一堆英文! - Zero based index of the item at the specified position, or -1
- If True, position is in control's client window but not on an item
- If True, position is over item icon
- If True, position is over item text
- If True, position is over item state image
- If True, position is somewhere on the item
- If True, position is above the control's client area
- If True, position is below the control's client area
- If True, position is to the left of the client area
- If True, position is to the right of the client area
求解释! 本帖最后由 blue_dvd 于 2012-11-18 22:33 编辑
回复 10# netegg .......
case $nm_dblclk
return 1
.......
这段代码加到case $nm_Click后面不行
是不是要在声明一个函数才行?
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $iwParam
Local $tNMHDR, $hWndFrom, $iCode
$hWndListView = $hListView
If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)
$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_dblclk
return 1
EndSwitch
EndSwitch
EndFunc ;==>WM_NOTIFY
是状态图像
case $nm_dbclk放到setdata下面 回复 11# blue_dvd
http://www.autoitx.com/thread-7404-1-1.html英文的问题 一切都解决了,再次感谢netegg大神!
页:
[1]
2