Jump to content

Listview subitem persistent focus? [SOLVED]


rootx
 Share

Recommended Posts

Holà, I would like to understand why when I select a subitem remains the focus on it. THX

;Rootx
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <GuiImageList.au3>
#include <MsgBoxConstants.au3>
#include <GuiListView.au3>

Global $idListview

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 450, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 16, 16, 75, 25)
$idListview = GUICtrlCreateListView("", 16, 56, 586, 358, BitOR($GUI_SS_DEFAULT_LISTVIEW,$LVS_SORTASCENDING,$LVS_AUTOARRANGE), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES))


    _GUICtrlListView_InsertColumn($idListview, 0, "Column 1", 100)
    _GUICtrlListView_InsertColumn($idListview, 1, "Column 2", 100)
    _GUICtrlListView_InsertColumn($idListview, 2, "Column 3", 100)


    _GUICtrlListView_AddItem($idListview, "Row 1: Col 1", 0)
    _GUICtrlListView_AddSubItem($idListview, 0, "Row 1: Col 2", 1, 1)
    _GUICtrlListView_AddSubItem($idListview, 0, "Row 1: Col 3", 2, 1)

    _GUICtrlListView_AddItem($idListview, "Row 2: Col 1", 1)
    _GUICtrlListView_AddSubItem($idListview, 1, "Row 2: Col 2", 1, 1)
    _GUICtrlListView_AddSubItem($idListview, 1, "Row 2: Col 3", 2,1)

    _GUICtrlListView_AddItem($idListview, "Row 3: Col 1", 2)
    _GUICtrlListView_AddSubItem($idListview, 2, "Row 3: Col 2",1, 1)
    _GUICtrlListView_AddSubItem($idListview, 2, "Row 3: Col 3",2, 1)



$StatusBar1 = _GUICtrlStatusBar_Create($Form1)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1

    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $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
                    Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    Local $iCol = DllStructGetData($tInfo, "SubItem")
                    ConsoleWrite("Column clicked: " & $iCol & @CRLF)

                Case $NM_CLICK

                    $aHit = _GUICtrlListView_SubItemHitTest($hWndFrom)
                    $LVEIP_Item = $aHit[0]
                    $LVEIP_SubItem = $aHit[1]

                    Local $tItem
                    $tItem = DllStructCreate($tagLVITEM)
                    DllStructSetData($tItem, "Mask", $LVIF_STATE)
                    DllStructSetData($tItem, "Item", $LVEIP_Item)
                    DllStructSetData($tItem, "SubItem", $LVEIP_SubItem)
                    DllStructSetData($tItem, "State", $LVIS_SELECTED)
                    DllStructSetData($tItem, "StateMask", $LVIS_SELECTED)
                    _GUICtrlListView_SetItemEx($hWndFrom, $tItem)

                    ConsoleWrite("Selected Cell " & _GUICtrlListView_GetItemText($idListview,$LVEIP_Item,$LVEIP_SubItem)&@CRLF)


            EndSwitch
    EndSwitch
EndFunc

 

Edited by rootx
Link to comment
Share on other sites

Patience please, someone will eventually respond to your topic.

$LVS_SHOWSELALWAYS
$idListview = GUICtrlCreateListView("", 16, 56, 586, 358, BitOR($GUI_SS_DEFAULT_LISTVIEW,$LVS_SORTASCENDING,$LVS_AUTOARRANGE, $LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES))

Also, you can use this on your $NM_CLICK to get the row and column of the item clicked (the sub item) without using SubItemhitTest

$tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
$iRow = DllStructGetData($tInfo, "Index")
$iCol = DllStructGetData($tInfo, "SubItem")

 

Edited by InunoTaishou
Link to comment
Share on other sites

1 hour ago, InunoTaishou said:

Also, you can use this on your $NM_CLICK to get the row and column of the item clicked (the sub item) without using SubItemhitTest

$tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
$iRow = DllStructGetData($tInfo, "Index")
$iCol = DllStructGetData($tInfo, "SubItem")

The index isn't returning in my trials on subitems > 0, which seems to be by design per the structure description.

Quote

; Name...........: $tagNMITEMACTIVATE
; Description ...: Sent by a list-view control when the user activates an item
; Fields ........: $tagNMHDR - Contains information about a notification message
;                  Index      - Index of the list-view item. If the item index is not used for the notification,
;                  +this member will contain -1

;Rootx
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <GuiImageList.au3>
#include <MsgBoxConstants.au3>
#include <GuiListView.au3>

Global $idListview

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 450, 192, 124)
$Button1 = GUICtrlCreateButton("Button1", 16, 16, 75, 25)
;$idListview = GUICtrlCreateListView("", 16, 56, 586, 358, BitOR($GUI_SS_DEFAULT_LISTVIEW,$LVS_SORTASCENDING,$LVS_AUTOARRANGE,$LVS_SINGLESEL), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES))
$idListview = GUICtrlCreateListView("", 16, 56, 586, 358, BitOR($GUI_SS_DEFAULT_LISTVIEW,$LVS_SORTASCENDING,$LVS_AUTOARRANGE, $LVS_SHOWSELALWAYS), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES))

    _GUICtrlListView_InsertColumn($idListview, 0, "Column 1", 100)
    _GUICtrlListView_InsertColumn($idListview, 1, "Column 2", 100)
    _GUICtrlListView_InsertColumn($idListview, 2, "Column 3", 100)


    _GUICtrlListView_AddItem($idListview, "Row 1: Col 1", 0)
    _GUICtrlListView_AddSubItem($idListview, 0, "Row 1: Col 2", 1, 1)
    _GUICtrlListView_AddSubItem($idListview, 0, "Row 1: Col 3", 2, 1)

    _GUICtrlListView_AddItem($idListview, "Row 2: Col 1", 1)
    _GUICtrlListView_AddSubItem($idListview, 1, "Row 2: Col 2", 1, 1)
    _GUICtrlListView_AddSubItem($idListview, 1, "Row 2: Col 3", 2,1)

    _GUICtrlListView_AddItem($idListview, "Row 3: Col 1", 2)
    _GUICtrlListView_AddSubItem($idListview, 2, "Row 3: Col 2",1, 1)
    _GUICtrlListView_AddSubItem($idListview, 2, "Row 3: Col 3",2, 1)



$StatusBar1 = _GUICtrlStatusBar_Create($Form1)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1

    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $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
                    Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    Local $iCol = DllStructGetData($tInfo, "SubItem")
    ;Debug
    ConsoleWrite("Column clicked: " & $iCol & @CRLF)

                Case $NM_CLICK
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                    $iRow = DllStructGetData($tInfo, "Index")
                    $iCol = DllStructGetData($tInfo, "SubItem")
                    ConsoleWrite($iRow & @TAB & $iCol & @CRLF)
;~                     $aHit = _GUICtrlListView_SubItemHitTest($hWndFrom)
;~                     $LVEIP_Item = $aHit[0]
;~                     $LVEIP_SubItem = $aHit[1]

                    Local $tItem
                    $tItem = DllStructCreate($tagLVITEM)
                    DllStructSetData($tItem, "Mask", $LVIF_STATE)
                    DllStructSetData($tItem, "Item", $iRow)
                    DllStructSetData($tItem, "SubItem", $iCol)
                    DllStructSetData($tItem, "State", $LVIS_SELECTED)
                    DllStructSetData($tItem, "StateMask", $LVIS_SELECTED)
                    _GUICtrlListView_SetItemEx($hWndFrom, $tItem)

                    ;ConsoleWrite("Selected Cell " & _GUICtrlListView_GetItemText($idListview,$iRow,$iCol)&@CRLF)


            EndSwitch
    EndSwitch
EndFunc

 

edit: found this example - be sure to read the next couple posts on that thread for a cosmetic change you may want to incorporate.

 

Edited by spudw2k
Link to comment
Share on other sites

53 minutes ago, spudw2k said:

 

Wow.... I'm blind... THX 4the other just study this example...

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>

Opt( "MustDeclareVars", 1 )

Global $hGui, $hLV, $iLVx, $iLVy, $aHit[2] = [ -1, -1 ]  ; $aHit contains row & col of marked cell
Global $aLV = [ [ 0,  25,  45, -1, -1 ], _ ; $hLV, $iLVx, $iLVy, $aHit[0], $aHit[1]
                [ 0, 125, 145, -1, -1 ], _
                [ 0, 225, 245, -1, -1 ] ]

Example()


Func Example()

  $hGui = GUICreate( "Mark Cell in Listview", 500, 520 )

  Local $idTab = GUICtrlCreateTab( 10, 10, 480, 500 )

  GUICtrlCreateTabItem( "Tab 0, LV 0" )
  Local $idLV0 = GUICtrlCreateListView( "Column 0|Column 1|Column 2", 25, 45, 250, 250 )
  For $i = 0 To 99
    GUICtrlCreateListViewItem( "Cell " & $i & ".0" & "|Cell " & $i & ".1" & "|Cell " & $i & ".2", $idLV0 )
  Next
  $hLV = GUICtrlGetHandle( $idLV0 )
  $aLV[0][0] = $hLV
  $iLVx = 25
  $iLVy = 45

  GUICtrlCreateTabItem( "Tab 1, LV 1" )
  Local $idLV1 = GUICtrlCreateListView( "Column 0|Column 1|Column 2", 125, 145, 250, 250 )
  For $i = 0 To 99
    GUICtrlCreateListViewItem( "Cell " & $i & ".0" & "|Cell " & $i & ".1" & "|Cell " & $i & ".2", $idLV1 )
  Next
  $aLV[1][0] = GUICtrlGetHandle( $idLV1 )

  GUICtrlCreateTabItem( "Tab 2, LV 2" )
  Local $idLV2 = GUICtrlCreateListView( "Column 0|Column 1|Column 2", 225, 245, 250, 250 )
  For $i = 0 To 99
    GUICtrlCreateListViewItem( "Cell " & $i & ".0" & "|Cell " & $i & ".1" & "|Cell " & $i & ".2", $idLV2 )
  Next
  $aLV[2][0] = GUICtrlGetHandle( $idLV2 )

  GUICtrlCreateTabItem( "" )

  GUIRegisterMsg( $WM_NOTIFY, "WM_NOTIFY" )

  GUISetState()

  Local $iTab, $iTabPrev = 0
  While 1
    Switch GUIGetMsg()
      Case $idTab
        $iTab = GUICtrlRead( $idTab )
        $aLV[$iTabPrev][3] = $aHit[0]
        $aLV[$iTabPrev][4] = $aHit[1]
        $hLV = $aLV[$iTab][0]
        $iLVx = $aLV[$iTab][1]
        $iLVy = $aLV[$iTab][2]
        $aHit[0] = $aLV[$iTab][3]
        $aHit[1] = $aLV[$iTab][4]
        If $aHit[0] <> -1 Then _
          _GUICtrlListView_RedrawItems( $hLV, $aHit[0], $aHit[0] )
        $iTabPrev = $iTab
      Case $GUI_EVENT_CLOSE
        Exit
    EndSwitch
  WEnd

EndFunc

Func WM_NOTIFY( $hWnd, $iMsg, $wParam, $lParam )
  Local $tNMHDR, $hWndFrom, $iCode
  $tNMHDR = DllStructCreate( $tagNMHDR, $lParam )
  $hWndFrom = HWnd( DllStructGetData( $tNMHDR, "hWndFrom" ) )
  $iCode = DllStructGetData( $tNMHDR, "Code" )

  Switch $hWndFrom

    Case $hLV

      Switch $iCode

        Case $LVN_ITEMCHANGED
          Local $tNMLISTVIEW, $iItem, $aInfo
          $tNMLISTVIEW = DllStructCreate( $tagNMLISTVIEW, $lParam )
          $iItem = DllStructGetData( $tNMLISTVIEW, "Item" )
          _GUICtrlListView_SetItemSelected( $hLV, $iItem, False )
          _GUICtrlListView_SetItemState( $hLV, $iItem, 0, $LVIS_FOCUSED )

          Local $aInfo = GUIGetCursorInfo( $hGui )
          If $aInfo[2] Then
            $aInfo = _GUICtrlListView_SubItemHitTest( $hLV, $aInfo[0]-$iLVx, $aInfo[1]-$iLVy )
            If $aInfo[0] > -1 And $aInfo[1] > -1 And $aInfo[0] = $iItem Then
              If $aHit[0] > -1 Then _
                _GUICtrlListView_RedrawItems( $hLV, $aHit[0], $aHit[0] )
              If $aHit[0] <> $aInfo[0] Or $aHit[1] <> $aInfo[1] Then
                $aHit[0] = $aInfo[0] ; Row
                $aHit[1] = $aInfo[1] ; Col
              Else
                $aHit[0] = -1 ; Row
                $aHit[1] = -1 ; Col
              EndIf
              _GUICtrlListView_RedrawItems( $hLV, $iItem, $iItem )
            EndIf
          EndIf

        Case $NM_CUSTOMDRAW
          Local $tNMLVCUSTOMDRAW = DllStructCreate( $tagNMLVCUSTOMDRAW, $lParam )
          Local $dwDrawStage = DllStructGetData( $tNMLVCUSTOMDRAW, "dwDrawStage" )

          Switch $dwDrawStage                               ; Holds a value that specifies the drawing stage

            Case $CDDS_PREPAINT                             ; Before the paint cycle begins
              Return $CDRF_NOTIFYITEMDRAW                   ; Notify the parent window of any ITEM-related drawing operations

            Case $CDDS_ITEMPREPAINT                         ; Before painting an item
              Return $CDRF_NOTIFYSUBITEMDRAW                ; Notify the parent window of any SUBITEM-related drawing operations

            Case BitOR( $CDDS_ITEMPREPAINT, $CDDS_SUBITEM ) ; Before painting a subitem
              Local $dwItemSpec = DllStructGetData( $tNMLVCUSTOMDRAW, "dwItemSpec" ) ; Item index
              Local $iSubItem   = DllStructGetData( $tNMLVCUSTOMDRAW, "iSubItem"   ) ; Subitem index
              Local $uItemState = DllStructGetData( $tNMLVCUSTOMDRAW, "uItemState" ) ; Item state
              If $dwItemSpec = $aHit[0] Then ; Marked row
                Switch $iSubItem
                  Case $aHit[1] ; Marked column
                    DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText",   0xFFFFFF ) ; Forecolor white
                    DllStructSetData( $tNMLVCUSTOMDRAW, "clrTextBk", 0xCC6600 ) ; Backcolor dark blue, BGR
                  Case Else ; Other columns
                    DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText",   0x000000 ) ; Forecolor black
                    DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", 0xFFFFFF ) ; Backcolor white
                EndSwitch
              Else ; Other rows
                DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText",   0x000000 )
                DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", 0xFFFFFF )
              EndIf
              Return $CDRF_NEWFONT                          ; $CDRF_NEWFONT must be returned after changing font or colors

          EndSwitch

      EndSwitch

  EndSwitch

  Return $GUI_RUNDEFMSG
EndFunc

 

Edited by Melba23
Removed quote
Link to comment
Share on other sites

  • Moderators

rootx,

Delighted you found the solution, but when you reply, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - responders know what they wrote and it just pads the thread unnecessarily.

M23

 

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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

×
×
  • Create New...