Jump to content

How a ListView acts as CTRL was pressed by default?


UEZ
 Share

Go to solution Solved by LarsJ,

Recommended Posts

I want that the Listview acts as CTRL key was pressed by default to avoid any deletion when the listview was clicked.

Here an example code:

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

$hGUI = GUICreate("ListView Test", 400, 550)
$iLV = GUICtrlCreateListView("", 0, 10, 400, 480, $LVS_SHOWSELALWAYS, BitOR($LVS_EX_FLATSB, $LVS_EX_CHECKBOXES, $LVS_EX_FULLROWSELECT, $LVS_EX_DOUBLEBUFFER))
_GUICtrlListView_AddColumn($iLV, "Date", 100, 1)
_GUICtrlListView_AddColumn($iLV, "Size (bytes)", 100, 1)
_GUICtrlListView_AddColumn($iLV, "File Name", 100, 1)
For $i = 0 to 150
    _GUICtrlListView_AddItem($iLV, "Row " & $i)
    _GUICtrlListView_AddSubItem($iLV, $i, $i * 2, 1)
    _GUICtrlListView_AddSubItem($iLV, $i, $i * 4, 2)
Next

$cButton = GUICtrlCreateButton("Reset", 10, 510, 80, 30)

GUISetState()
_GUICtrlListView_SetItemChecked($iLV, 0)
_GUICtrlListView_SetItemSelected($iLV, 0)
_GUICtrlListView_SetItemChecked($iLV, 1)
_GUICtrlListView_SetItemSelected($iLV, 1)
_GUICtrlListView_SetItemChecked($iLV, 2)
_GUICtrlListView_SetItemSelected($iLV, 2)
ControlFocus($hGUI, "", $iLV)

; Create array to hold selection state
Global $aSelected[_GUICtrlListView_GetItemCount($iLV)]

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton
            ; Clear all selections
             _GUICtrlListView_SetItemSelected($iLV, -1, False)
             ; Clear selected array
            Global $aSelected[_GUICtrlListView_GetItemCount($iLV)]
    EndSwitch
WEnd

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

    #forceref $hWnd, $iMsg, $wParam

    ; Struct = $tagNMHDR and "int Item;int SubItem" from $tagNMLISTVIEW
    Local $tStruct = DllStructCreate("hwnd;uint_ptr;int_ptr;int;int", $lParam)
    If @error Then Return
    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    Local $hWndFrom = HWnd($tNMHDR.hWndFrom)
    Local $iIDFrom = $tNMHDR.IDFrom
    Local $iCode = $tNMHDR.Code
    Local Static $iCount = 0
    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
    Switch $iCode
        Case $LVN_ITEMCHANGED
            If BitAND($tInfo.Changed, $LVIF_STATE) = $LVIF_STATE Then
                Switch $tInfo.NewState
                    Case 8192 ;item checked
                        _GUICtrlListView_SetItemSelected($iLV, $tInfo.Item, True)
                    Case 4096 ;item unchecked
                        _GUICtrlListView_SetItemSelected($iLV, $tInfo.Item, False)
                EndSwitch
            EndIf
        Case $NM_CLICK
           $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
;~          _DebugPrint("$NM_CLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _
;~                  "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _
;~                  "-->Code:" & @TAB & $iCode & @CRLF & _
;~                  "-->Index:" & @TAB & $tInfo.Index & @CRLF & _
;~                  "-->SubItem:" & @TAB & $tInfo.SubItem & @CRLF & _
;~                  "-->NewState:" & @TAB & $tInfo.NewState & @CRLF & _
;~                  "-->OldState:" & @TAB & $tInfo.OldState & @CRLF & _
;~                  "-->Changed:" & @TAB & $tInfo.Changed & @CRLF & _
;~                  "-->ActionX:" & @TAB & $tInfo.ActionX & @CRLF & _
;~                  "-->ActionY:" & @TAB & $tInfo.ActionY & @CRLF & _
;~                  "-->lParam:" & @TAB & $tInfo.lParam & @CRLF & _
;~                  "-->KeyFlags:" & @TAB & $tInfo.KeyFlags)
            _GUICtrlListView_SetItemChecked($iLV, $tInfo.Index, _GUICtrlListView_GetItemState($iLV, $tInfo.Index, $LVIS_SELECTED))
    EndSwitch
    Return $__LISTVIEWCONSTANT_GUI_RUNDEFMSG
EndFunc

Func _DebugPrint($s_Text , $sLine = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @CRLF & _
            "+======================================================" & @CRLF & _
            "-->Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $s_Text  & @CRLF & _
            "+======================================================" & @CRLF)
EndFunc   ;==>_DebugPrint

Just cklick the listview and you will see that the selection is deleted. Repeat it and hold the CTRL key pressed.

Any ideas?

I don't want to read the listview and check whether the checkbox is checked and mark the row appropriately! It's lagging...

 

Thanks,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

  • Solution

UEZ, May be you can use something like this:

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

$hGUI = GUICreate("ListView Test", 400, 550)
$iLV = GUICtrlCreateListView("", 0, 10, 400, 480, $LVS_SHOWSELALWAYS, BitOR($LVS_EX_FLATSB, $LVS_EX_CHECKBOXES, $LVS_EX_FULLROWSELECT, $LVS_EX_DOUBLEBUFFER))
_GUICtrlListView_AddColumn($iLV, "Date", 100, 1)
_GUICtrlListView_AddColumn($iLV, "Size (bytes)", 100, 1)
_GUICtrlListView_AddColumn($iLV, "File Name", 100, 1)
For $i = 0 to 150
    _GUICtrlListView_AddItem($iLV, "Row " & $i)
    _GUICtrlListView_AddSubItem($iLV, $i, $i * 2, 1)
    _GUICtrlListView_AddSubItem($iLV, $i, $i * 4, 2)
Next
_GUICtrlListView_SetItemChecked($iLV, 0)
_GUICtrlListView_SetItemParam($iLV, 0, True)
_GUICtrlListView_SetItemChecked($iLV, 1)
_GUICtrlListView_SetItemParam($iLV, 1, True)
_GUICtrlListView_SetItemChecked($iLV, 2)
_GUICtrlListView_SetItemParam($iLV, 2, True)
ControlFocus($hGUI, "", $iLV)

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
  #forceref $hWnd, $iMsg, $wParam
  Local $tNMHDR, $iCode
  $tNMHDR = DllStructCreate( $tagNMHDR, $lParam )
  $iCode = $tNMHDR.Code

  Switch $iCode
    Case $LVN_ITEMCHANGED
      Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
      If BitAND($tInfo.Changed, $LVIF_STATE) = $LVIF_STATE Then
        Switch $tInfo.NewState
          Case 8192 ;item checked
            _GUICtrlListView_SetItemParam($iLV, $tInfo.Item, True)
          Case 4096 ;item unchecked
            _GUICtrlListView_SetItemParam($iLV, $tInfo.Item, False)
        EndSwitch
      EndIf

    Case $NM_CLICK
      Local $aInfo = GUIGetCursorInfo()
      Local $aTest = _GUICtrlListView_SubItemHitTest( $iLV, $aInfo[0], $aInfo[1] )
      If Not ( Not $aTest[4] And $aTest[6] ) Then
        $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
        Local $fChecked = Not _GUICtrlListView_GetItemParam($iLV, $tInfo.Index)
        _GUICtrlListView_SetItemParam($iLV, $tInfo.Index, $fChecked)
        _GUICtrlListView_SetItemChecked($iLV, $tInfo.Index, $fChecked)
      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 $lItemlParam = $tNMLVCUSTOMDRAW.lItemlParam ; Item param
          Local $iSubItem    = $tNMLVCUSTOMDRAW.iSubItem    ; Subitem index
          Local $uItemState  = $tNMLVCUSTOMDRAW.uItemState  ; Item state
          If $lItemlParam Then ; Checked rows
            If Not BitAnd( $uItemState, $CDIS_FOCUS ) Then
              DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText",   0xFFFFFF ) ; Forecolor white
              DllStructSetData( $tNMLVCUSTOMDRAW, "clrTextBk", 0xCC6600 ) ; Backcolor dark blue, BGR
            EndIf
          Else ; Other rows
            If Not BitAnd( $uItemState, $CDIS_FOCUS ) Then
              DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText",   0x000000 )
              DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", 0xFFFFFF )
            EndIf
          EndIf
          Return $CDRF_NEWFONT                          ; $CDRF_NEWFONT must be returned after changing font or colors

      EndSwitch

  EndSwitch

  Return $GUI_RUNDEFMSG
EndFunc
Checked state (and selection) in ItemParam.
Link to comment
Share on other sites

LarsJ,

interesting idea to simulate the CTRL key behavior which didn't came to my mind although I used  $NM_CUSTOMDRAW to draw the grid in different color.  :thumbsup:

Just for my curiosity: is there a way without simulating? How is windows handling the lv when ctrl is pressed?

Thanks!

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

  • Moderators

LarsJ,

Another nice script. :thumbsup:

But could I make 2 suggested changes?

- 1. When adding extended styles to a ListView it is always advisable to use _GUICtrlListView_SetExtendedListViewStyle as some of these values are also used by $WS_Ex_* styles and can be confused with them. That is why the function exists. ;)

- 2. In ListViews created with the native function, AutoIt uses the item parameter to store the internal ControlID of the row. As explained in the Help file, it is best to use parameter values well above any possible existing ControlIDs (we recommend 1000+) as experience has shown that there can be "ghost" effects on the real control if this is not done. In this case you are only using True/False (1/0) and so there is no real possibility of this happening, but I raise it a useful hint for anyone reading. :)

Making those changes does not affect the script as you can see here (I use 1000/1001 as checked/unchecked):

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

$hGUI = GUICreate("ListView Test", 400, 550)
$iLV = GUICtrlCreateListView("", 0, 10, 400, 480, $LVS_SHOWSELALWAYS)
_GUICtrlListView_SetExtendedListViewStyle($iLV, BitOR($LVS_EX_FLATSB, $LVS_EX_CHECKBOXES, $LVS_EX_FULLROWSELECT, $LVS_EX_DOUBLEBUFFER))

_GUICtrlListView_AddColumn($iLV, "Date", 100, 1)
_GUICtrlListView_AddColumn($iLV, "Size (bytes)", 100, 1)
_GUICtrlListView_AddColumn($iLV, "File Name", 100, 1)
For $i = 0 To 150
    _GUICtrlListView_AddItem($iLV, "Row " & $i)
    _GUICtrlListView_SetItemParam($iLV, $i, 1000)
    _GUICtrlListView_AddSubItem($iLV, $i, $i * 2, 1)
    _GUICtrlListView_AddSubItem($iLV, $i, $i * 4, 2)
Next
_GUICtrlListView_SetItemChecked($iLV, 0)
_GUICtrlListView_SetItemParam($iLV, 0, 1001)
_GUICtrlListView_SetItemChecked($iLV, 1)
_GUICtrlListView_SetItemParam($iLV, 1, 1001)
_GUICtrlListView_SetItemChecked($iLV, 2)
_GUICtrlListView_SetItemParam($iLV, 2, 1001)
ControlFocus($hGUI, "", $iLV)

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $tNMHDR, $iCode
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $iCode = $tNMHDR.Code

    Switch $iCode
        Case $LVN_ITEMCHANGED
            Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
            If BitAND($tInfo.Changed, $LVIF_STATE) = $LVIF_STATE Then
                Switch $tInfo.NewState
                    Case 8192 ;item checked
                        _GUICtrlListView_SetItemParam($iLV, $tInfo.Item, 1001)
                    Case 4096 ;item unchecked
                        _GUICtrlListView_SetItemParam($iLV, $tInfo.Item, 1000)
                EndSwitch
            EndIf

        Case $NM_CLICK
            Local $aInfo = GUIGetCursorInfo()
            Local $aTest = _GUICtrlListView_SubItemHitTest($iLV, $aInfo[0], $aInfo[1])
            If Not (Not $aTest[4] And $aTest[6]) Then
                $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                Local $iChecked = _GUICtrlListView_GetItemParam($iLV, $tInfo.Index)
                ConsoleWrite($iChecked & @CRLF)
                If $iChecked = 1000 Then
                    _GUICtrlListView_SetItemParam($iLV, $tInfo.Index, 1001)
                    _GUICtrlListView_SetItemChecked($iLV, $tInfo.Index, True)
                Else
                    _GUICtrlListView_SetItemParam($iLV, $tInfo.Index, 1000)
                    _GUICtrlListView_SetItemChecked($iLV, $tInfo.Index, False)
                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 $lItemlParam = $tNMLVCUSTOMDRAW.lItemlParam ; Item param
                    Local $iSubItem = $tNMLVCUSTOMDRAW.iSubItem ; Subitem index
                    Local $uItemState = $tNMLVCUSTOMDRAW.uItemState ; Item state
                    If $lItemlParam = 1001 Then ; Checked rows
                        If Not BitAND($uItemState, $CDIS_FOCUS) Then
                            DllStructSetData($tNMLVCUSTOMDRAW, "ClrText", 0xFFFFFF) ; Forecolor white
                            DllStructSetData($tNMLVCUSTOMDRAW, "clrTextBk", 0xCC6600) ; Backcolor dark blue, BGR
                        EndIf
                    Else ; Other rows
                        If Not BitAND($uItemState, $CDIS_FOCUS) Then
                            DllStructSetData($tNMLVCUSTOMDRAW, "ClrText", 0x000000)
                            DllStructSetData($tNMLVCUSTOMDRAW, "ClrTextBk", 0xFFFFFF)
                        EndIf
                    EndIf
                    Return $CDRF_NEWFONT ; $CDRF_NEWFONT must be returned after changing font or colors

            EndSwitch

    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY
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

  • 3 weeks later...

At first glance it looks like what I want BUT it doesn't work when you have checkboxes and mark checked and unmark unchecked rows in the list view.
Only rows with checked checkboxes should be selected using _GUICtrlListView_SetItemSelected().


Is it possible in this section

...
                            If $wParam = $iListView_Web Then
                                Local $lItemlParam = $tNMLVCUSTOMDRAW.lItemlParam ; Item param
                                Local $iSubItem = $tNMLVCUSTOMDRAW.iSubItem ; Subitem index
                                Local $uItemState = $tNMLVCUSTOMDRAW.uItemState ; Item state

                                If $lItemlParam Then ; Checked rows
                                    If Not BitAND($uItemState, $CDIS_FOCUS) Then
                                        DllStructSetData($tNMLVCUSTOMDRAW, "ClrText", $iColor_Text) ; Forecolor white
                                        DllStructSetData($tNMLVCUSTOMDRAW, "clrTextBk", 0x00FF00) ;0xFF9933) ; Backcolor blue, BGR
                                    EndIf
                                Else ; Other rows
                                    If Not BitAND($uItemState, $CDIS_FOCUS) Then
                                        DllStructSetData($tNMLVCUSTOMDRAW, "ClrText", $iColor_Text)
                                        DllStructSetData($tNMLVCUSTOMDRAW, "ClrTextBk", 0x0000FF) ;$iColor_LV)
                                    EndIf
                                EndIf
                            EndIf
...

to set the background color only for checked rows?


Edit: seems that $CDIS_CHECKED, $CDIS_SELECTED, $CDIS_MARKED are doing partly the job. Continuing to test and search for a solution...


Btw, is there a way to get the color which the system uses to mark a row in LV? -> _WinAPI_GetSysColor($COLOR_HIGHLIGHT)


Forget it, it was my wrong implementation which caused the problems.


Br,
UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

One important thing. Because a row is drawn only in one color, the row should be drawn in the item part of the drawing stage, where the entire row is drawn at once. To draw such a row column by column in the subitem part is wrong, and leads to bad performance.

Here the rows are drawn in the item part of the drawing stage. The subitem part is deleted. A controlID offset with a value of 1000 is added to ItemParam.

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

$hGUI = GUICreate("ListView Test", 400, 550)
$iLV = GUICtrlCreateListView("", 0, 10, 400, 480, $LVS_SHOWSELALWAYS, BitOR($LVS_EX_FLATSB, $LVS_EX_CHECKBOXES, $LVS_EX_FULLROWSELECT, $LVS_EX_DOUBLEBUFFER))
_GUICtrlListView_AddColumn($iLV, "Date", 100, 1)
_GUICtrlListView_AddColumn($iLV, "Size (bytes)", 100, 1)
_GUICtrlListView_AddColumn($iLV, "File Name", 100, 1)
For $i = 0 to 150
    _GUICtrlListView_AddItem($iLV, "Row " & $i)
    _GUICtrlListView_AddSubItem($iLV, $i, $i * 2, 1)
    _GUICtrlListView_AddSubItem($iLV, $i, $i * 4, 2)
    _GUICtrlListView_SetItemParam($iLV, $i, 1000)
Next
_GUICtrlListView_SetItemChecked($iLV, 0)
_GUICtrlListView_SetItemParam($iLV, 0, 1001)
_GUICtrlListView_SetItemChecked($iLV, 1)
_GUICtrlListView_SetItemParam($iLV, 1, 1001)
_GUICtrlListView_SetItemChecked($iLV, 2)
_GUICtrlListView_SetItemParam($iLV, 2, 1001)
ControlFocus($hGUI, "", $iLV)

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
  #forceref $hWnd, $iMsg, $wParam
  Local $tNMHDR, $iCode
  $tNMHDR = DllStructCreate( $tagNMHDR, $lParam )
  $iCode = $tNMHDR.Code

  Switch $iCode
    Case $LVN_ITEMCHANGED
      Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
      If BitAND($tInfo.Changed, $LVIF_STATE) = $LVIF_STATE Then
        Switch $tInfo.NewState
          Case 8192 ;item checked
            _GUICtrlListView_SetItemParam($iLV, $tInfo.Item, 1001)
          Case 4096 ;item unchecked
            _GUICtrlListView_SetItemParam($iLV, $tInfo.Item, 1000)
        EndSwitch
      EndIf

    Case $NM_CLICK
      Local $aInfo = GUIGetCursorInfo()
      Local $aTest = _GUICtrlListView_SubItemHitTest( $iLV, $aInfo[0], $aInfo[1] )
      If Not ( Not $aTest[4] And $aTest[6] ) Then
        $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
        Local $fChecked = Not ( _GUICtrlListView_GetItemParam($iLV, $tInfo.Index) = 1001 )
        _GUICtrlListView_SetItemParam($iLV, $tInfo.Index, $fChecked + 1000)
        _GUICtrlListView_SetItemChecked($iLV, $tInfo.Index, $fChecked)
      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
          Local $lItemlParam = $tNMLVCUSTOMDRAW.lItemlParam - 1000 ; Item param
          Local $uItemState  = $tNMLVCUSTOMDRAW.uItemState         ; Item state
          If $lItemlParam Then ; Checked rows
            If Not BitAnd( $uItemState, $CDIS_FOCUS ) Then
              DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText",   0xFFFFFF ) ; Forecolor white
              DllStructSetData( $tNMLVCUSTOMDRAW, "clrTextBk", 0xCC6600 ) ; Backcolor dark blue, BGR
            EndIf
          Else ; Other rows
            If Not BitAnd( $uItemState, $CDIS_FOCUS ) Then
              DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText",   0x000000 )
              DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", 0xFFFFFF )
            EndIf
          EndIf
          Return $CDRF_NEWFONT                          ; $CDRF_NEWFONT must be returned after changing font or colors

      EndSwitch

  EndSwitch

  Return $GUI_RUNDEFMSG
EndFunc
Link to comment
Share on other sites

Thanks LarsJ.
 
One question how would you implement multi selection? I mean if you change $NM_CLICK to $NM_DBLCLK and select some rows by holding down the shift key. These lines should be marked and the checkbox should be set.

The same when unselecting some lines.
 

This is what I did:

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


Global Const $iColor_TextBk = _WinAPI_GetSysColor($COLOR_HIGHLIGHT)
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIListView.au3>

$hGUI = GUICreate("ListView Test", 400, 550)
$iLV = GUICtrlCreateListView("", 0, 10, 400, 480, $LVS_SHOWSELALWAYS)
_GUICtrlListView_SetExtendedListViewStyle($iLV, BitOR($LVS_EX_FLATSB, $LVS_EX_CHECKBOXES, $LVS_EX_FULLROWSELECT, $LVS_EX_DOUBLEBUFFER))

_GUICtrlListView_AddColumn($iLV, "Date", 100, 1)
_GUICtrlListView_AddColumn($iLV, "Size (bytes)", 100, 1)
_GUICtrlListView_AddColumn($iLV, "File Name", 100, 1)
For $i = 0 To 150
    _GUICtrlListView_AddItem($iLV, "Row " & $i)
    _GUICtrlListView_SetItemParam($iLV, $i, 1000)
    _GUICtrlListView_AddSubItem($iLV, $i, $i * 2, 1)
    _GUICtrlListView_AddSubItem($iLV, $i, $i * 4, 2)
Next
_GUICtrlListView_SetItemChecked($iLV, 0)
_GUICtrlListView_SetItemParam($iLV, 0, 1001)
_GUICtrlListView_SetItemChecked($iLV, 1)
_GUICtrlListView_SetItemParam($iLV, 1, 1001)
_GUICtrlListView_SetItemChecked($iLV, 2)
_GUICtrlListView_SetItemParam($iLV, 2, 1001)
ControlFocus($hGUI, "", $iLV)

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $tNMHDR, $iCode, $aIndices, $n
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $iCode = $tNMHDR.Code

    Switch $iCode
        Case $LVN_ITEMCHANGED
            Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
            If BitAND($tInfo.Changed, $LVIF_STATE) = $LVIF_STATE Then
                Switch $tInfo.NewState
                    Case 8192 ;item checked
                        _GUICtrlListView_SetItemParam($iLV, $tInfo.Item, 1001)
                    Case 4096 ;item unchecked
                        _GUICtrlListView_SetItemParam($iLV, $tInfo.Item, 1000)
                EndSwitch
            EndIf
        Case $NM_CLICK
            $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
            If $tInfo.KeyFlags = 4 Then
                Local $aIndices = _GUICtrlListView_GetSelectedIndices($iLV, True), $n
                For $n = 1 To $aIndices[0]
                    Switch Not _GUICtrlListView_GetItemChecked($iLV, $aIndices[$n])
                        Case False
                            _GUICtrlListView_SetItemParam($iLV, $aIndices[$n], 1000)
                            _GUICtrlListView_SetItemChecked($iLV, $aIndices[$n], False)
                        Case True
                            _GUICtrlListView_SetItemParam($iLV, $aIndices[$n], 1001)
                            _GUICtrlListView_SetItemChecked($iLV, $aIndices[$n], True)
                    EndSwitch
                Next
            EndIf
        Case $NM_DBLCLK
            Local $aInfo = GUIGetCursorInfo()
            Local $aTest = _GUICtrlListView_SubItemHitTest($iLV, $aInfo[0], $aInfo[1])
            If Not (Not $aTest[4] And $aTest[6]) Then
                $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
                Local $iChecked = _GUICtrlListView_GetItemParam($iLV, $tInfo.Index)
                If $iChecked = 1000 Then
                    _GUICtrlListView_SetItemParam($iLV, $tInfo.Index, 1001)
                    _GUICtrlListView_SetItemChecked($iLV, $tInfo.Index, True)
                Else
                    _GUICtrlListView_SetItemParam($iLV, $tInfo.Index, 1000)
                    _GUICtrlListView_SetItemChecked($iLV, $tInfo.Index, False)
                EndIf
            EndIf
        Case $NM_CLICK
            $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
            If $tInfo.KeyFlags = 4 Then
                $aIndices = _GUICtrlListView_GetSelectedIndices($iLV, True)
                ConsoleWrite(UBound($aIndices) - 1 & @CRLF)
                For $n = 1 To $aIndices[0]
                    Switch Not _GUICtrlListView_GetItemChecked($iLV, $aIndices[$n])
                        Case False
                            _GUICtrlListView_SetItemParam($iLV, $aIndices[$n], 1000)
                            _GUICtrlListView_SetItemChecked($iLV, $aIndices[$n], False)
                        Case True
                            _GUICtrlListView_SetItemParam($iLV, $aIndices[$n], 1001)
                            _GUICtrlListView_SetItemChecked($iLV, $aIndices[$n], True)
                    EndSwitch
                Next
            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
              Local $lItemlParam = $tNMLVCUSTOMDRAW.lItemlParam - 1000 ; Item param
              Local $uItemState  = $tNMLVCUSTOMDRAW.uItemState         ; Item state
              If $lItemlParam Then ; Checked rows
                If Not BitAnd( $uItemState, $CDIS_FOCUS ) Then
                  DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText",   0xFFFFFF ) ; Forecolor white
                  DllStructSetData( $tNMLVCUSTOMDRAW, "clrTextBk", $iColor_TextBk ) ; Backcolor dark blue, BGR
                EndIf
              Else ; Other rows
                If Not BitAnd( $uItemState, $CDIS_FOCUS ) Then
                  DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText",   0x000000 )
                  DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", 0xFFFFFF )
                EndIf
              EndIf
              Return $CDRF_NEWFONT                          ; $CDRF_NEWFONT must be returned after changing font or colors

          EndSwitch

    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

Br,
UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

I would separate checked and selected rows to make things easier:

 

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

$hGUI = GUICreate("ListView Test", 400, 550)
$iLV = GUICtrlCreateListView("", 0, 10, 400, 480, $LVS_SHOWSELALWAYS, BitOR($LVS_EX_FLATSB, $LVS_EX_CHECKBOXES, $LVS_EX_FULLROWSELECT, $LVS_EX_DOUBLEBUFFER))
_GUICtrlListView_AddColumn($iLV, "Date", 100, 1)
_GUICtrlListView_AddColumn($iLV, "Size (bytes)", 100, 1)
_GUICtrlListView_AddColumn($iLV, "File Name", 100, 1)
For $i = 0 to 150
    _GUICtrlListView_AddItem($iLV, "Row " & $i)
    _GUICtrlListView_AddSubItem($iLV, $i, $i * 2, 1)
    _GUICtrlListView_AddSubItem($iLV, $i, $i * 4, 2)
    _GUICtrlListView_SetItemParam($iLV, $i, 1000)
Next
_GUICtrlListView_SetItemChecked($iLV, 0)
_GUICtrlListView_SetItemParam($iLV, 0, 1001)
_GUICtrlListView_SetItemChecked($iLV, 1)
_GUICtrlListView_SetItemParam($iLV, 1, 1001)
_GUICtrlListView_SetItemChecked($iLV, 2)
_GUICtrlListView_SetItemParam($iLV, 2, 1001)
ControlFocus($hGUI, "", $iLV)

GUICtrlCreateLabel( "Space to invert checked state of selected items", 10, 500, 380, 20 )
GUICtrlSetFont( -1, 10, 700 )

GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
  #forceref $hWnd, $iMsg, $wParam
  Local $tNMHDR, $iCode
  $tNMHDR = DllStructCreate( $tagNMHDR, $lParam )
  $iCode = $tNMHDR.Code

  Switch $iCode
    Case $LVN_ITEMCHANGED
      Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
      If BitAND($tInfo.Changed, $LVIF_STATE) = $LVIF_STATE Then
        Switch $tInfo.NewState
          Case 8192 ;item checked
            _GUICtrlListView_SetItemParam($iLV, $tInfo.Item, 1001)
          Case 4096 ;item unchecked
            _GUICtrlListView_SetItemParam($iLV, $tInfo.Item, 1000)
        EndSwitch
      EndIf

    Case $LVN_KEYDOWN
      Local $tKey = DllStructCreate($tagNMLVKEYDOWN, $lParam)
      If $tKey.VKey = 0x20 Then ; $VK_SPACE = 0x20
        Local $aSelected = _GUICtrlListView_GetSelectedIndices($iLV, True)
        If $aSelected[0] > 1 Then ; $aSelected[0] = 1 is handled by default code
          For $i = 1 To $aSelected[0]
            If _GUICtrlListView_GetItemState($iLV, $aSelected[$i], $LVIS_FOCUSED ) Then ContinueLoop ; Handled by default code
            Local $fChecked = Not ( _GUICtrlListView_GetItemParam($iLV, $aSelected[$i]) = 1001 )
            _GUICtrlListView_SetItemParam($iLV, $aSelected[$i], $fChecked + 1000)
            _GUICtrlListView_SetItemChecked($iLV, $aSelected[$i], $fChecked)
            _GUICtrlListView_SetItemSelected($iLV, $aSelected[$i], False)
          Next
        EndIf
      EndIf

    Case $NM_DBLCLK
      Local $aInfo = GUIGetCursorInfo()
      Local $aTest = _GUICtrlListView_SubItemHitTest( $iLV, $aInfo[0], $aInfo[1] )
      If Not ( Not $aTest[4] And $aTest[6] ) Then
        $tInfo = DllStructCreate($tagNMITEMACTIVATE, $lParam)
        Local $fChecked = Not ( _GUICtrlListView_GetItemParam($iLV, $tInfo.Index) = 1001 )
        _GUICtrlListView_SetItemParam($iLV, $tInfo.Index, $fChecked + 1000)
        _GUICtrlListView_SetItemChecked($iLV, $tInfo.Index, $fChecked)
      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
          Local $lItemlParam = $tNMLVCUSTOMDRAW.lItemlParam - 1000 ; Item param
          Local $uItemState  = $tNMLVCUSTOMDRAW.uItemState         ; Item state
          If $lItemlParam Then ; Checked rows
            If Not BitAnd( $uItemState, $CDIS_FOCUS ) Then
              ;DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText",   0xFFFFFF ) ; Forecolor white
              ;DllStructSetData( $tNMLVCUSTOMDRAW, "clrTextBk", 0xCC6600 ) ; Backcolor dark blue, BGR
              DllStructSetData( $tNMLVCUSTOMDRAW, "clrTextBk", 0xFFFF00 ) ; Backcolor cyan, BGR
            EndIf
          Else ; Other rows
            If Not BitAnd( $uItemState, $CDIS_FOCUS ) Then
              ;DllStructSetData( $tNMLVCUSTOMDRAW, "ClrText",   0x000000 )
              ;DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", 0xFFFFFF )
              DllStructSetData( $tNMLVCUSTOMDRAW, "ClrTextBk", 0xFFFFFF )
            EndIf
          EndIf
          Return $CDRF_NEWFONT                          ; $CDRF_NEWFONT must be returned after changing font or colors

      EndSwitch

  EndSwitch

  Return $GUI_RUNDEFMSG
EndFunc
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...