Jump to content

Highlighted item in the ListView?


Recommended Posts

How to detect a change of focus highlighted item in the ListView?

Thanks.

#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>

$Form = GUICreate('', 600, 600)
$ListView = GUICtrlCreateListView(' ', 10, 10, 280, 580, BitOR($LVS_NOCOLUMNHEADER, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS, $LVS_SORTASCENDING))
$hListView = GUICtrlGetHandle($ListView)
_GUICtrlListView_SetColumnWidth($ListView, 0, 258 + 100)
GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
GUISetState()

$FileList = _FileListToArray('C:\WINDOWS\system32', '*.ini', 1)
For $i = 1 To $FileList[0]
    GUICtrlCreateListViewItem($FileList[$i], $ListView)
Next

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case 0
            ContinueLoop
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    Local $tNMHDR = DllStructCreate($tagNMITEMACTIVATE, $lParam)
    Local $IDFrom = DllStructGetData($tNMHDR, 'IDFrom')
    Local $Index = DllStructGetData($tNMHDR, 'Index')
    Local $Code = DllStructGetData($tNMHDR, 'Code')

    Switch $IDFrom
        Case $ListView
            Switch $Code
                Case ???
                    $Item = _GUICtrlListView_GetItem($ListView, $Index)
                    ConsoleWrite($Item[3] & @CR)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Link to comment
Share on other sites

#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>

$Form = GUICreate('', 600, 600)
$ListView = GUICtrlCreateListView(' ', 10, 10, 280, 580, BitOR($LVS_NOCOLUMNHEADER, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS, $LVS_SORTASCENDING))
$hListView = GUICtrlGetHandle($ListView)
_GUICtrlListView_SetColumnWidth($ListView, 0, 258 + 100)
GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
GUISetState()

$FileList = _FileListToArray('C:\WINDOWS\system32', '*.ini', 1)
For $i = 1 To $FileList[0]
    GUICtrlCreateListViewItem($FileList[$i], $ListView)
Next

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case 0
            ContinueLoop
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    Local $tNMHDR = DllStructCreate($tagNMITEMACTIVATE, $lParam)
    Local $IDFrom = DllStructGetData($tNMHDR, 'IDFrom')
    Local $Index = DllStructGetData($tNMHDR, 'Index')
    Local $Code = DllStructGetData($tNMHDR, 'Code')

    ;ConsoleWrite($Code & @crlf)

    Switch $IDFrom
        Case $ListView
            Switch $Code
                Case -2
                    $aSelected = _GUICtrlListView_GetSelectedIndices($ListView, True)
                    ;ConsoleWrite($aSelected[0] & @crlf)
                    if $aSelected[0] > 0 then
                        ;ConsoleWrite($aSelected[1] & @crlf)
                        for $i = 1 to $aSelected[0]
                            ConsoleWrite(_GUICtrlListView_GetItemText($ListView,$aSelected[$i]) & @crlf)
                        next
                    endif
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Link to comment
Share on other sites

Another way:

#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>

$Form = GUICreate('', 600, 600)
$ListView = GUICtrlCreateListView(' ', 10, 10, 280, 580, BitOR($LVS_NOCOLUMNHEADER, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS, $LVS_SORTASCENDING))
$hListView = GUICtrlGetHandle($ListView)
_GUICtrlListView_SetColumnWidth($ListView, 0, 258 + 100)
GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
GUISetState()

$FileList = _FileListToArray('C:\WINDOWS\system32', '*.ini', 1)
For $i = 1 To $FileList[0]
    GUICtrlCreateListViewItem($FileList[$i], $ListView)
Next

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case 0
            ContinueLoop
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    Local $tNMHDR = DllStructCreate($tagNMITEMACTIVATE, $lParam)
    Local $IDFrom = DllStructGetData($tNMHDR, 'IDFrom')
    Local $Index = DllStructGetData($tNMHDR, 'Index')
    Local $Code = DllStructGetData($tNMHDR, 'Code')

    Switch $IDFrom
        Case $ListView
            Switch $Code
            Case $LVN_ITEMCHANGED
                $NMLISTVIEW = DllStructCreate($tagNMLISTVIEW, $lParam)
                ; if state has changed
                If BitAND(DllStructGetData($NMLISTVIEW, "Changed"), $LVIF_STATE) = $LVIF_STATE And _
                    DllStructGetData($NMLISTVIEW, "NewState") <> DllStructGetData($NMLISTVIEW, "OldState") Then 
                    ; take care of only newly selected items (not deselected ones)
                    If BitAND(DllStructGetData($NMLISTVIEW, "NewState"), $LVIS_SELECTED) = $LVIS_SELECTED  Then 
                        $Item = _GUICtrlListView_GetItem($ListView, $Index)
                        ConsoleWrite($Item[3] & @CR)
                    EndIf
                EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Link to comment
Share on other sites

or modification for logging also deselection:

Case $LVN_ITEMCHANGED
                 $NMLISTVIEW = DllStructCreate($tagNMLISTVIEW, $lParam)
                 ; if state has changed
                 If BitAND(DllStructGetData($NMLISTVIEW, "Changed"), $LVIF_STATE) = $LVIF_STATE And _
                     DllStructGetData($NMLISTVIEW, "NewState") <> DllStructGetData($NMLISTVIEW, "OldState") Then 

                        $Item = _GUICtrlListView_GetItem($ListView, $Index)
                        
                        If BitAND(DllStructGetData($NMLISTVIEW, "NewState"), $LVIS_SELECTED) = $LVIS_SELECTED And _
                            BitAND(DllStructGetData($NMLISTVIEW, "OldState"), $LVIS_SELECTED) = 0 Then
                            ConsoleWrite('selected: ' & $Item[3] & @CR)
                        EndIf
                        
                        If BitAND(DllStructGetData($NMLISTVIEW, "NewState"), $LVIS_SELECTED) = 0 And _
                            BitAND(DllStructGetData($NMLISTVIEW, "OldState"), $LVIS_SELECTED) = $LVIS_SELECTED Then
                            ConsoleWrite('deselected: ' & $Item[3] & @CR)
                        EndIf
                 EndIf

EDIT: corrected: deselection was fired twice

Edited by Jon
Link to comment
Share on other sites

Another way:

#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GUIListView.au3>
#include <WindowsConstants.au3>

$Form = GUICreate('', 600, 600)
$ListView = GUICtrlCreateListView(' ', 10, 10, 280, 580, BitOR($LVS_NOCOLUMNHEADER, $LVS_SINGLESEL, $LVS_SHOWSELALWAYS, $LVS_SORTASCENDING))
$hListView = GUICtrlGetHandle($ListView)
_GUICtrlListView_SetColumnWidth($ListView, 0, 258 + 100)
GUIRegisterMsg($WM_NOTIFY, 'WM_NOTIFY')
GUISetState()

$FileList = _FileListToArray('C:\WINDOWS\system32', '*.ini', 1)
For $i = 1 To $FileList[0]
    GUICtrlCreateListViewItem($FileList[$i], $ListView)
Next

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case 0
            ContinueLoop
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)

    Local $tNMHDR = DllStructCreate($tagNMITEMACTIVATE, $lParam)
    Local $IDFrom = DllStructGetData($tNMHDR, 'IDFrom')
    Local $Index = DllStructGetData($tNMHDR, 'Index')
    Local $Code = DllStructGetData($tNMHDR, 'Code')

    Switch $IDFrom
        Case $ListView
            Switch $Code
            Case $LVN_ITEMCHANGED
                $NMLISTVIEW = DllStructCreate($tagNMLISTVIEW, $lParam)
                ; if state has changed
                If BitAND(DllStructGetData($NMLISTVIEW, "Changed"), $LVIF_STATE) = $LVIF_STATE And _
                    DllStructGetData($NMLISTVIEW, "NewState") <> DllStructGetData($NMLISTVIEW, "OldState") Then 
                    ; take care of only newly selected items (not deselected ones)
                    If BitAND(DllStructGetData($NMLISTVIEW, "NewState"), $LVIS_SELECTED) = $LVIS_SELECTED  Then 
                        $Item = _GUICtrlListView_GetItem($ListView, $Index)
                        ConsoleWrite($Item[3] & @CR)
                    EndIf
                EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
x8 thanks to you Zedna. That's exactly what I wanted. I never had not previously worked with the ListView.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...