#include <GUIConstants.au3>
#include <GuiListView.au3>
Global $LVCLICKEDITEM[4]=[-1,-1]
Global $LVITEMCK[2]=[-1,0]
Opt("GUIOnEventMode", 1)
$GUI = GUICreate("lvCheckTest", 633, 447, 196, 117)
GuiSetOnEvent(-3,"ExitFunc")
$ListView = GUICtrlCreateListView("Some kind of list", 5, 10, 620, 390)
GUICtrlSetOnEvent(-1, "ListViewClick")
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_CHECKBOXES, $LVS_EX_CHECKBOXES)
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($ListView, 0x101E, 0, 610)
;Create a Thousand Items
For $x = 0 to 1000
GUICtrlCreateListViewItem("Checked LvItem #"&$x,$ListView)
GUISetOnEvent(-1,"ListViewClick")
Next
$Status1 = GUICtrlCreateLabel("", 0, 428, 315, 17, $SS_SUNKEN)
$Status2 = GUICtrlCreateLabel("", 318, 428, 313, 17, $SS_SUNKEN)
GUISetState(@SW_SHOW)
;Start WndProc handler
GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")
While 1
Sleep(100)
WEnd
Func ListViewClick()
ConsoleWrite("ListViewClick"&@LF)
EndFunc
Func _ListViewCheckedEvent()
ConsoleWrite("ListViewCheckedEvent"&@LF)
Local $state = _GUICtrlListView_GetItemChecked($ListView,$LVCLICKEDITEM[0])
ConsoleWrite(StringFormat("Item:%d Checked State is %d"&@LF,$LVCLICKEDITEM[0],$state))
GUICtrlSetData($Status1,StringFormat("Item:%d Checked State is %d"&@LF,$LVCLICKEDITEM[0],$state))
GuiCtrlSetData($Status2,"Checked Event = True")
EndFunc
Func _ListViewItemClick()
ConsoleWrite("ListViewItemClick"&@LF)
Local $state = _GUICtrlListView_GetItemChecked($ListView,$LVCLICKEDITEM[0])
GUICtrlSetData($Status1,StringFormat("Item:%d Checked State is %d"&@LF,$LVCLICKEDITEM[0],$state))
GuiCtrlSetData($Status2,"Checked Event = False")
EndFunc
Func ExitFunc()
Exit
EndFunc
Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
#forceref $hWndGUI, $MsgID, $wParam
Local $tagNMHDR, $from, $pressed,$event, $retval = $GUI_RUNDEFMSG ;, $event
$tagNMHDR = DllStructCreate("int;int;int", $lParam);NMHDR (hwndFrom, idFrom, code)
If @error Then
$tagNMHDR =0
Return
EndIf
;$from = DllStructGetData($tagNMHDR, 1);not used
$event = DllStructGetData($tagNMHDR, 3)
Select
Case $wParam = $ListView
Select
Case $event = $NM_CLICK ;<<<<<<this happens before the item changes state
$LVCLICKEDITEM = _LVGetInfo($lParam) ; get the Item clicked
$LVITEMCK[0]= $LVCLICKEDITEM[0] ; retain the item
$LVITEMCK[1]= _GUICtrlListView_GetItemChecked($ListView,$LVCLICKEDITEM[0]); retain the state
Case $event = $NM_DBLCLK ;<<<<<<this happens before the item changes state
$LVCLICKEDITEM = _LVGetInfo($lParam) ; get the Item clicked
$LVITEMCK[0]= $LVCLICKEDITEM[0] ; retain the item
$LVITEMCK[1]= _GUICtrlListView_GetItemChecked($ListView,$LVCLICKEDITEM[0]); retain the state
Case $event = $LVN_ITEMCHANGED ;<<<<<<this happens after the item changes state
;make sure the item is still the same
$LVCLICKEDITEM = _LVGetInfo($lParam)
If $LVCLICKEDITEM[0] = $LVITEMCK[0] Then
;yes
_ListViewCheckedEvent() ; event handler for item checked
$LVITEMCK[0]=-1 ; reset
$LVITEMCK[1]=-1 ; reset
Else
_ListViewItemClick() ; default action.
EndIf
EndSelect
EndSelect
$tagNMHDR = 0
$event = 0
$lParam = 0
Return $retval
EndFunc ;==>WM_Notify_Events
Func _LVGetInfo($lParam)
Local $aClicked[2]
Local $tagNMITEMACTIVATE = DllStructCreate("int;int;int;int;int;int;int;int;int", $lParam)
$aClicked[0] = DllStructGetData($tagNMITEMACTIVATE, 4)
$aClicked[1] = DllStructGetData($tagNMITEMACTIVATE, 5)
if $aClicked[1] < -1 then $aClicked[1] = -1
if $aClicked[0] < -1 then $aClicked[0] = -1
if $aClicked[1] > _GUICtrlListView_GetItemCount($ListView) then $aClicked[1] = -1
if $aClicked[0] > _GUICtrlListView_GetItemCount($ListView) then $aClicked[0] = -1
$tagNMITEMACTIVATE = 0
Return $aClicked
EndFunc