Jump to content

Get ListView Header Order After Drag/Drop


Recommended Posts

Hi all,

So, I was following the Help file trying to get the resulting column order after dragging a column header in a listview. I can't seem to get the order after the drag as the results I receive are from before. It's rather odd. Is there a way I can get this information using WM_NOTIFY like in the below example?

Thanks for the help!

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

Global $g_ListView

Example()

Func Example()
    Local $hGUI

    ; Create GUI
    $hGUI = GUICreate("Header", 400, 300)
    $g_ListView = GUICtrlCreateListView("Column 1|Column 2|Column 3|Column 4", 0, 0, 400, 300, BitOR($LVS_SHOWSELALWAYS, $LVS_REPORT), BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_INFOTIP, $LVS_REPORT, $LVS_EX_HEADERDRAGDROP))
    GUISetState(@SW_SHOW)

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

    ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc   ;==>Example

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iCode
    Local $tNMHDR, $tNMHEADER, $tNMHDFILTERBTNCLICK, $tNMHDDISPINFO
    
    $hHwnd = GUICtrlGetHandle($g_ListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $iCode
        Case $HDN_ENDDRAG ; Sent by a header control when a drag operation has ended on one of its items
            ConsoleWrite("!> " & _GUICtrlListView_GetColumnOrder($g_ListView) & @CRLF); <<<<<<<<<<<<<<< This will return the order BEFORE the drop
            Return False ; To allow the control to automatically place and reorder the item
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
Edited by buymeapc
Link to comment
Share on other sites

Looks like I found an answer. I was hoping WM_NOTIFY would do all the work with any/all listview controls, but with this solution, each loop must check the flag in order to register the changed column order.

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

Global $bColumnOrderChanged = False

Global $g_hHeader
Global $g_ListView

Example()

Func Example()
    Local $hGUI

    ; Create GUI
    $hGUI = GUICreate("Header", 400, 300)
    $g_ListView = GUICtrlCreateListView("Column 1|Column 2|Column 3|Column 4", 0, 0, 400, 300, BitOR($LVS_SHOWSELALWAYS, $LVS_REPORT), BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES, $LVS_EX_INFOTIP, $LVS_REPORT, $LVS_EX_HEADERDRAGDROP))
    $g_hHeader = _GUICtrlListView_GetHeader($g_ListView)
    GUISetState(@SW_SHOW)

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

    ; Loop until the user exits.
    Do
        Sleep(10)
        If $bColumnOrderChanged Then
            $bColumnOrderChanged = False
            ConsoleWrite(_ArrayToString(_GUICtrlHeader_GetOrderArray($g_hHeader)) & @CRLF)
        EndIf
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc   ;==>Example

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    Local $hWndFrom, $iCode
    Local $tNMHDR, $tNMHEADER, $tNMHDFILTERBTNCLICK, $tNMHDDISPINFO
    
    $hHwnd = GUICtrlGetHandle($g_ListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $iCode
        Case $HDN_ENDDRAG ; Sent by a header control when a drag operation has ended on one of its items
            $bColumnOrderChanged = True
            Return False ; To allow the control to automatically place and reorder the item
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY
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...