#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <GuiListView.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
Opt('GUIOnEventMode', 1)
Global $idListView, $hListView, $pListViewFunc, $idAddSingleSel, $idDelSingleSel
Example()
While 1
Sleep(1000)
WEnd
Func Example()
; Create GUI
GUICreate("ctrl+single-click --> single-click", 420, 200)
GUISetOnEvent(-3, '_Exit')
; Create ListView
$idListView = GUICtrlCreateListView("", 10, 10, 400, 180, $GUI_SS_DEFAULT_LISTVIEW - $LVS_SINGLESEL, $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($idListView, $LVS_EX_DOUBLEBUFFER + $LVS_EX_FULLROWSELECT)
$hListView = GUICtrlGetHandle($idListView)
; Add columns to ListView
_GUICtrlListView_InsertColumn($idListView, 0, "Column 1", 94)
_GUICtrlListView_InsertColumn($idListView, 1, "Column 2", 94)
_GUICtrlListView_InsertColumn($idListView, 2, "Column 3", 94)
_GUICtrlListView_InsertColumn($idListView, 3, "Column 4", 94)
; Fill ListView
Local $iItems = 100
For $i = 0 To $iItems - 1
GUICtrlCreateListViewItem($i & "/Column 1|" & $i & "/Column 2|" & $i & "/Column 3|" & $i & "/Column 4", $idListView)
Next
; Subclass ListView to receive keyboard/mouse messages
$pListViewFunc = DllCallbackGetPtr(DllCallbackRegister("ListViewFunc", "lresult", "hwnd;uint;wparam;lparam;uint_ptr;dword_ptr"))
_WinAPI_SetWindowSubclass($hListView, $pListViewFunc, 0, 0) ; $iSubclassId = 0, $pData = 0
; Add/del single selection
$idAddSingleSel = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_AddSingleSel')
$idDelSingleSel = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, '_DelSingleSel')
;GUIRegisterMsg
GUIRegisterMsg($WM_NOTIFY,"wm_notify")
; Show GUI
GUISetState(@SW_SHOW)
EndFunc ;==>Example
Func ListViewFunc($hWnd, $iMsg, $wParam, $lParam, $iSubclassId, $pData)
If $iMsg <> $WM_LBUTTONDOWN Then
Local $result = DllCall("comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam)
If Not @error Then
Return $result[0]
Else
Return
EndIf
EndIf
Switch $wParam
Case 0x0001
If _GUICtrlListView_GetSelectedCount($idListView) Then
Local $aHit = _GUICtrlListView_HitTest(GUICtrlGetHandle($idListView), BitAND($lParam, 0xFFFF), BitShift($lParam, 16))
If Not (@error Or $aHit[0] = -1) Then
If _GUICtrlListView_GetItemSelected($idListView, $aHit[0]) Then
GUICtrlSendToDummy($idDelSingleSel, $aHit[0])
Else
GUICtrlSendToDummy($idAddSingleSel, $aHit[0])
EndIf
EndIf
Return 0
EndIf
Case 0x0009
Return 0
EndSwitch
Local $result = DllCall("comctl32.dll", "lresult", "DefSubclassProc", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam)
If Not @error Then
Return $result[0]
Else
Return
EndIf
#forceref $iSubclassId, $pData
EndFunc ;==>ListViewFunc
Func _AddSingleSel()
Local $iItem = GUICtrlRead($idAddSingleSel)
_GUICtrlListView_SetItemSelected($idListView, $iItem, True)
_GUICtrlListView_SetItemFocused($idListView, $iItem)
EndFunc ;==>_AddSingleSel
Func _DelSingleSel()
Local $iItem = GUICtrlRead($idDelSingleSel)
_GUICtrlListView_SetItemSelected($idListView, $iItem, False)
_GUICtrlListView_SetItemFocused($idListView, $iItem)
EndFunc ;==>_DelSingleSel
Func _Exit()
_WinAPI_RemoveWindowSubclass($hListView, $pListViewFunc, 0)
GUIDelete()
Exit
EndFunc ;==>_Exit
Func _WinAPI_SetWindowSubclass($hWnd, $pSubclassProc, $idSubClass, $pData = 0)
Local $aRet = DllCall('comctl32.dll', 'bool', 'SetWindowSubclass', 'hwnd', $hWnd, 'ptr', $pSubclassProc, 'uint_ptr', $idSubClass, _
'dword_ptr', $pData)
If @error Then Return SetError(@error, @extended, 0)
; If Not $aRet[0] Then Return SetError(1000, 0, 0)
Return $aRet[0]
EndFunc ;==>_WinAPI_SetWindowSubclass
Func _WinAPI_RemoveWindowSubclass($hWnd, $pSubclassProc, $idSubClass)
Local $aRet = DllCall('comctl32.dll', 'bool', 'RemoveWindowSubclass', 'hwnd', $hWnd, 'ptr', $pSubclassProc, 'uint_ptr', $idSubClass)
If @error Then Return SetError(@error, @extended, False)
Return $aRet[0]
EndFunc ;==>_WinAPI_RemoveWindowSubclass
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $wParam
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
$hWndListView = $idListView
If Not IsHWnd($idListView) Then $hWndListView = GUICtrlGetHandle($idListView)
$tNMHDR = DllStructCreate($tagNMHDR, $lParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndListView
Switch $iCode
Case $LVN_COLUMNCLICK ; A column was clicked
$tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
_DebugPrint("$LVN_COLUMNCLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
"-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
"-->Code:" & @TAB & $iCode & @CRLF & _
"-->Item:" & @TAB & DllStructGetData($tInfo, "Item") & @CRLF & _
"-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
"-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
"-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
"-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
"-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
"-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
"-->Param:" & @TAB & DllStructGetData($tInfo, "Param"))
; No return value
Case $LVN_DELETEITEM ; An item is about to be deleted
$tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
_DebugPrint("$LVN_DELETEITEM" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
"-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
"-->Code:" & @TAB & $iCode & @CRLF & _
"-->Item:" & @TAB & DllStructGetData($tInfo, "Item") & @CRLF & _
"-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
"-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
"-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
"-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
"-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
"-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
"-->Param:" & @TAB & DllStructGetData($tInfo, "Param"))
; No return value
Case $LVN_KEYDOWN ; A key has been pressed
$tInfo = DllStructCreate($tagNMLVKEYDOWN, $lParam)
_DebugPrint("$LVN_KEYDOWN" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
"-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
"-->Code:" & @TAB & $iCode & @CRLF & _
"-->VKey:" & @TAB & DllStructGetData($tInfo, "VKey") & @CRLF & _
"-->Flags:" & @TAB & DllStructGetData($tInfo, "Flags"))
; No return value
Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
_DebugPrint("$NM_CLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
"-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
"-->Code:" & @TAB & $iCode & @CRLF & _
"-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
"-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
"-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
"-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
"-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
"-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
"-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
"-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
"-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
; No return value
Case $NM_DBLCLK ; Sent by a list-view control when the user double-clicks an item with the left mouse button
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
_DebugPrint("$NM_DBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
"-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
"-->Code:" & @TAB & $iCode & @CRLF & _
"-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
"-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
"-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
"-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
"-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
"-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
"-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
"-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
"-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
; No return value
Case $NM_KILLFOCUS ; The control has lost the input focus
_DebugPrint("$NM_KILLFOCUS" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
"-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
"-->Code:" & @TAB & $iCode)
; No return value
Case $NM_RCLICK ; Sent by a list-view control when the user clicks an item with the right mouse button
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
_DebugPrint("$NM_RCLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
"-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
"-->Code:" & @TAB & $iCode & @CRLF & _
"-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
"-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
"-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
"-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
"-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
"-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
"-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
"-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
"-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
;Return 1 ; not to allow the default processing
Return 0 ; allow the default processing
Case $NM_RDBLCLK ; Sent by a list-view control when the user double-clicks an item with the right mouse button
$tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
_DebugPrint("$NM_RDBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
"-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
"-->Code:" & @TAB & $iCode & @CRLF & _
"-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @CRLF & _
"-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @CRLF & _
"-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @CRLF & _
"-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @CRLF & _
"-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @CRLF & _
"-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @CRLF & _
"-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @CRLF & _
"-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @CRLF & _
"-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))
; No return value
Case $NM_RETURN ; The control has the input focus and that the user has pressed the ENTER key
_DebugPrint("$NM_RETURN" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
"-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
"-->Code:" & @TAB & $iCode)
; No return value
Case $NM_SETFOCUS ; The control has received the input focus
_DebugPrint("$NM_SETFOCUS" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
"-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
"-->Code:" & @TAB & $iCode)
; No return value
Case $NM_CUSTOMDRAW
_DebugPrint("$NM_CUSTOMDRAW")
$tDraw = DllStructCreate($tagNMLVCUSTOMDRAW, $lParam)
$iDrawStage = DllStructGetData($tDraw, "dwDrawStage")
$iItemSpec = DllStructGetData($tDraw, "dwItemSpec")
Switch $iDrawStage
Case $CDDS_PREPAINT
_DebugPrint("$CDDS_PREPAINT")
Return $CDRF_NOTIFYITEMDRAW
Case $CDDS_ITEMPREPAINT
_DebugPrint("$CDDS_ITEMPREPAINT")
If BitAND($iItemSpec, 1) = 1 Then
DllStructSetData($tDraw, "clrTextBk", 0xff00cc)
Else
DllStructSetData($tDraw, "clrTextBk", 0xccff00)
EndIf
Return $CDRF_NEWFONT
EndSwitch
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY
Func _DebugPrint($s_Text, $sLine = @ScriptLineNumber)
ConsoleWrite( _
"!===========================================================" & @CRLF & _
"+======================================================" & @CRLF & _
"-->Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $s_Text & @CRLF & _
"+======================================================" & @CRLF)
EndFunc ;==>_DebugPrint