Jump to content

Selecting Multiple ListView Items with the Mouse


Recommended Posts

@Know_Fear
After an half-day of coding, I arrived to a possible "workaround", which involves the handling of WM_NOTIFY Message.
I'll try to explain has much as I can ( if comments are not enough! ) what I imagined in order to do what you were asking.
Since I "just" started to study WM_* and related, I said to me: "Why don't I capture the $LVN_BEGINDRAG Notification Code and see if from there I can do something?"
And it all started from there :)

So, the code below, "intercepts" the $LVN_BEGINDRAG and $LVN_ENDDRAG Notification Codes to select multiple items in the ListView.
In the $LVN_BEGINDRAG part, the "first" item to be selected is stored, and, in the $LVN_ENDDRAG part, the "last" item to be selected is stored as well.
In fact, you are dragging a ListView Item, but thanks to the $LVN_BEGINDRAG, you know where you want to start something ( the drag ), and thanks to $LVN_ENDDRAG, when to end something ( the drag ); and, thanks to this, you have the multiple items selection :)
I am sure that @LarsJ could be a lot better ( For LarsJ: you inspired and helped me a little bit with ListViews, and I want to thank you for it ), and I want to point him to the code below as a "starting" code to do something cool with :)

So, here it is the code:

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>        ; Added
#include <ListViewConstants.au3>
#include <MsgBoxConstants.au3>    ; Uncommented
#include <StructureConstants.au3> ; Added
#include <WindowsConstants.au3>

Global Const $LVN_ENDDRAG = ($LVN_FIRST - 21) ; Found on Internet.
                                              ; This Notification Code is sent either when I hover on a ListView Item.

; Global declarations to manage handles and controls in the WM_NOTIFY Message

Global $idListview, _           ; ID of the ListView
       $hdlListView, _          ; Handle of the ListView
       $hdlGUI, _               ; Handle of the GUI
       $blnBeginDrag = False    ; This flag prevents the $LVN_ENDDRAG to be fired when the mouse hovers on a ListView Item.


Example()

Func Example()

    ; Store the Handle of the GUI created
    $hdlGUI = GUICreate("listview items", 220, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)
    GUISetBkColor(0x00E0FFFF) ; will change background color

    Local $idListview = GUICtrlCreateListView("col1|col2|col3|col4|co5|col6|col7", 10, 10, 200, 150,BitOR($LVS_SHOWSELALWAYS, $LVS_REPORT), $LVS_EX_FULLROWSELECT)

    ; Store the Handle of ListView created
    $hdlListView = GUICtrlGetHandle($idListview)
    Local $idButton = GUICtrlCreateButton("Value?", 75, 170, 70, 20)
    Local $idItem1 = GUICtrlCreateListViewItem("item2|col22|col23|col22|col23|col22|col23", $idListview)
    Local $idItem2 = GUICtrlCreateListViewItem("item1|col12|col13|col22|col23|col22|col23", $idListview)
    Local $idItem3 = GUICtrlCreateListViewItem("item3|col32|col33|col22|col23|col22|col23", $idListview)
    Local $idItem4 = GUICtrlCreateListViewItem("item23|col632|col3433|col22|col23|col22|col23", $idListview)
    Local $idItem5 = GUICtrlCreateListViewItem("items3|col332|col7533|col22|col23|col22|col23", $idListview)
    Local $idItem6 = GUICtrlCreateListViewItem("item2|col22|col23|col22|col23|col22|col23", $idListview)
    Local $idItem7 = GUICtrlCreateListViewItem("item1|col12|col13|col22|col23|col22|col23", $idListview)
    Local $idItem8 = GUICtrlCreateListViewItem("item3|col32|col33|col22|col23|col22|col23", $idListview)
    Local $idItem9 = GUICtrlCreateListViewItem("item23|col632|col3433|col22|col23|col22|col23", $idListview)
    Local $idItem10 = GUICtrlCreateListViewItem("items3|col332|col7533|col22|col23|col22|col23", $idListview)
    GUICtrlCreateInput("", 20, 200, 150)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; to allow drag and dropping
    GUISetState(@SW_SHOW)
    GUICtrlSetData($idItem2, "ITEM1")
    GUICtrlSetData($idItem3, "||COL33")
    GUICtrlDelete($idItem1)

    ; Register the WM_NOTIFY Message to manage Windows Messages and Notification Codes sent from/to ListView
    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $idButton
                ; MsgBox($MB_SYSTEMMODAL, "listview item", GUICtrlRead(GUICtrlRead($idListview)))
                MsgBox($MB_ICONINFORMATION, "", "Another function!")

        EndSwitch
    WEnd
EndFunc   ;==>Example

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

    Local $hdlWindowFrom, _
          $intMessageCode, _
          $tNMHDR, _
          $tNMLISTVIEW
          
    Local Static $intSelectFrom = 0, _  ; Variable used to set the "first item" from which start the select
                 $intSelectTo = 0       ; Variable used to set the "last item" to which end the select

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    If @error Then Return

    $hdlWindowFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $intMessageCode = DllStructGetData($tNMHDR, "Code")

    Switch $hdlWindowFrom
        Case $hdlListView
            Switch $intMessageCode

                Case $LVN_BEGINDRAG

                    ; The drag has begun, so set to True the flag to enable the $LVN_ENDDRAG to be "managed"
                    $blnBeginDrag = True

                    Local $intItemRow = 0, _
                          $intItemColumn = 0

                    $tNMLISTVIEW = DllStructCreate($tagNMLISTVIEW, $lParam)
                    If @error Then Return

                    $intItemRow = DllStructGetData($tNMLISTVIEW, "Item")
                    $intItemColumn = DllStructGetData($tNMLISTVIEW, "SubItem")

                    ; Setting the "first" item to be selected
                    $intSelectFrom = $intItemRow

                    ConsoleWrite("---------------" & @CRLF & _
                                 "$LVN_BEGINDRAG on" & @CRLF & _
                                 "Item Index: " & $intItemRow & @CRLF & _
                                 "SubItem: " & $intItemColumn & @CRLF)

                Case $LVN_ENDDRAG

                    If $blnBeginDrag = True Then

                        Local $intItemRow = 0, _
                              $intItemColumn = 0

                        $tNMLISTVIEW  = DllStructCreate($tagNMLISTVIEW, $lParam)
                        If @error Then Return

                        $intItemRow = DllStructGetData($tNMLISTVIEW, "Item")
                        $intItemColumn = DllStructGetData($tNMLISTVIEW, "SubItem")

                        ; Setting the "last" item to be selected
                        $intSelectTo = $intItemRow

                        ConsoleWrite("---------------" & @CRLF & _
                                     "$LVN_ENDDRAG on" & @CRLF & _
                                     "Item Index: " & $intItemRow & @CRLF & _
                                     "SubItem: " & $intItemColumn & @CRLF & _
                                     "Number of Rows to select: " & ($intSelectTo - $intSelectFrom) + 1 & @CRLF)

                        ; Selecting ListView Items From $intSelectFrom To $intSelectTo
                        If ControlListView($hdlGUI, "", $idListview, "Select", $intSelectFrom + 1, $intSelectTo) Then
                            ConsoleWrite("All Items from #" & $intSelectFrom & " and #" & $intSelectTo & " have been selected!" & @CRLF)
                        Else
                            ConsoleWrite("Error while selecting multiple items." & @CRLF)
                        EndIf

                        ; The drag has ended, so set to False the flag to disable the $LVN_ENDDRAG to be "managed"
                        $blnBeginDrag = False

                    EndIf

            EndSwitch
    EndSwitch

EndFunc

Hope it helps :)


Best Regards.

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

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...