Jump to content

ListView items separate sort, by click on column.


rasim
 Share

Recommended Posts

Hi! This is my first example, any comments and wishs are accepted! :D

#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <GuiConstantsEx.au3>
#include <Array.au3>

$Gui = GUICreate("Test", 320, 220)

$hListView = _GUICtrlListView_Create($GUI, "Items|SubItems1|SubItems2", 10, 10, 300, 200, BitOR($LVS_EDITLABELS, $LVS_REPORT), $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_SUBITEMIMAGES, $LVS_EX_FULLROWSELECT))

_GUICtrlListView_SetColumnWidth($hListView, 0, 98)
_GUICtrlListView_SetColumnWidth($hListView, 1, 98)
_GUICtrlListView_SetColumnWidth($hListView, 2, 98)

$hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 3)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 11)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 22)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 33)

_GUICtrlListView_SetImageList($hListView, $hImage, 1)

_GUICtrlListView_AddItem($hListView, "Item1",0)
_GUICtrlListView_AddItem($hListView, "Item2",2)
_GUICtrlListView_AddItem($hListView, "Item3",1)
_GUICtrlListView_AddItem($hListView, "Item4",3)

_GUICtrlListView_AddSubItem ($hListView, 0,'44', 1, 2)
_GUICtrlListView_AddSubItem ($hListView, 1,'22', 1, 1)
_GUICtrlListView_AddSubItem ($hListView, 2,'11', 1, 0)
_GUICtrlListView_AddSubItem ($hListView, 3,'33', 1, 3)

_GUICtrlListView_AddSubItem ($hListView, 0,'New', 2, 1)
_GUICtrlListView_AddSubItem ($hListView, 1,'Page', 2, 3)
_GUICtrlListView_AddSubItem ($hListView, 2,'Sys', 2, 2)
_GUICtrlListView_AddSubItem ($hListView, 3,'Device', 2, 0)

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    $msg = GUIGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
WEnd

;================================================================================
; Function Name:    _ListView_SortSeparate.
; Description:      Sorting ListView items and subitems separate, by column.
; Parameter(s):     Column index
; Return Value(s):  None
; Requirement(s):   AutoIt 3.2.10.0
;=================================================================================
Func _ListView_SortSeparate($cIndex = 0)
    Local $ItemCounts, $iItemText, $iItemImage, $aItemsTemp[1][2], $iCurElement, $i
    
    $ItemCounts = _GUICtrlListView_GetItemCount($hListView)
    
    For $i = 0 To $ItemCounts - 1
        $iItemText = _GUICtrlListView_GetItemText($hListView, $i, $cIndex)
        $iItemImage = _GUICtrlListView_GetItemImage($hListView, $i, $cIndex)
        
        $aItemsTemp[0][0] += 1
        ReDim $aItemsTemp[$aItemsTemp[0][0] + 1][2]
        $aItemsTemp[$aItemsTemp[0][0]][0] = $iItemText
        $aItemsTemp[$aItemsTemp[0][0]][1] = $iItemImage
    Next
    
    $iCurElement = $aItemsTemp[1][0]
    _ArraySort($aItemsTemp, 0, 1, 0, 2)
    If StringInStr($iCurElement, $aItemsTemp[1][0]) Then _ArraySort($aItemsTemp, 1, 1, 0, 2)
    
    For $i = 1 To $aItemsTemp[0][0]
        _GUICtrlListView_SetItem($hListView, $aItemsTemp[$i][0], $i - 1, $cIndex, $aItemsTemp[$i][1])
    Next
    
EndFunc
;-----------------------------------------------------------------------------------

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iCode, $tNMHDR, $hWndListView, $tInfo, $iColumnIndex
    $hWndListView = $hListView
    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
    Case $hWndListView
        Switch $iCode
        Case $LVN_COLUMNCLICK
            $tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam)
            $iColumnIndex = DllStructGetData($tInfo, "SubItem")
            _ListView_SortSeparate($iColumnIndex)
            Return 0
        EndSwitch
    EndSwitch
    
    Return $GUI_RUNDEFMSG
EndFunc
Link to comment
Share on other sites

Way Cool! Just need to make it sort the entire row when the column is sorted and it is done. Great start!

_GUICtrlListView_RegisterSortCallBack in 3.2.11.0

For example cut out all the other code, add 2 lines and param to the additem (above 4099)

#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <GuiConstantsEx.au3>
#include <Array.au3>
#include <WindowsConstants.au3>

$Gui = GUICreate("Test", 320, 220)

$hListView = _GUICtrlListView_Create($GUI, "Items|SubItems1|SubItems2", 10, 10, 300, 200, BitOR($LVS_EDITLABELS, $LVS_REPORT), $WS_EX_CLIENTEDGE)
_GUICtrlListView_SetExtendedListViewStyle($hListView, BitOR($LVS_EX_SUBITEMIMAGES, $LVS_EX_FULLROWSELECT))

_GUICtrlListView_SetColumnWidth($hListView, 0, 98)
_GUICtrlListView_SetColumnWidth($hListView, 1, 98)
_GUICtrlListView_SetColumnWidth($hListView, 2, 98)

$hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 3)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 11)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 22)
_GUIImageList_AddIcon ($hImage, "shell32.dll", 33)

_GUICtrlListView_SetImageList($hListView, $hImage, 1)

_GUICtrlListView_AddItem($hListView, "Item1",0, 5000)
_GUICtrlListView_AddItem($hListView, "Item2",2, 5001)
_GUICtrlListView_AddItem($hListView, "Item3",1, 5002)
_GUICtrlListView_AddItem($hListView, "Item4",3, 5003)

_GUICtrlListView_AddSubItem ($hListView, 0,'44', 1, 2)
_GUICtrlListView_AddSubItem ($hListView, 1,'22', 1, 1)
_GUICtrlListView_AddSubItem ($hListView, 2,'11', 1, 0)
_GUICtrlListView_AddSubItem ($hListView, 3,'33', 1, 3)

_GUICtrlListView_AddSubItem ($hListView, 0,'New', 2, 1)
_GUICtrlListView_AddSubItem ($hListView, 1,'Page', 2, 3)
_GUICtrlListView_AddSubItem ($hListView, 2,'Sys', 2, 2)
_GUICtrlListView_AddSubItem ($hListView, 3,'Device', 2, 0)

GUISetState()

_GUICtrlListView_RegisterSortCallBack($hListView)

While 1
    $msg = GUIGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch
WEnd

_GUICtrlListView_UnRegisterSortCallBack($hListView)
Edited by GaryFrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

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