Sort Arrows

Category: Controls - ListView

Date: 02-16-2022

Return to Index


 
'Compilable Example:  (Jose Includes)
'Feb 2003 thread:  http://www.powerbasic.com/support/pbforums/showthread.php?t=23690   lvdemo.zip file example
'Jun 2009 thread:  http://www.powerbasic.com/support/pbforums/showthread.php?t=40798&highlight=listview+sort+icons
'Jun 2009 comment by Bob Mechler on problem with right-aligned listview columns
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Include "CommCtrl.inc"
 
#Resource Icon up "sortascending.ico"
#Resource Icon down "sortdescending.ico"
 
%IDC_ListView     = 400
Global hDlg, hListView, hImageList, hHeader As Dword, SortDirection As Long
 
Function PBMain() As Long
   Dialog New Pixels, 0, "ListView Sort Arrows",300,300,200,220, %WS_OverlappedWindow To hDlg
   CreateListView
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Local LVData As NM_ListView
   Select Case Cb.Msg
      Case %WM_Notify
         Select Case Cb.NmId
            Case %IDC_ListView
               Select Case Cb.NmCode
                  Case %LVN_ColumnClick
 
                     Type Set LVData = Cb.NmHdr$(SizeOf(LVData))
                     SortDirection = SortDirection Xor 1
                     If SortDirection Then
                        ListView Sort hDlg, %IDC_ListView, LVData.iSubItem+1, Ascend
                     Else
                        ListView Sort hDlg, %IDC_ListView, LVData.iSubItem+1, Descend
                     End If
                     SetHeaderIconsOnRight LVData.iSubItem
               End Select
         End Select
   End Select
End Function
 
Sub CreateListView
   Local i As Long
 
   'create control
   Control Add ListView, hDlg, %IDC_ListView,"", 10,10,380,200
   Control Handle hDlg, %IDC_ListView To hListView
   ListView Set StyleXX hDlg, %IDC_ListView, %LVS_Ex_CheckBoxes Or %LVS_Ex_GridLines Or %LVS_Ex_FullRowSelect
   ListView Insert Column hDlg, %IDC_ListView, 1, "Col1", 100, 0
   ListView Insert Column hDlg, %IDC_ListView, 2, "Col2", 200, 0
 
   'create imagelist
   ImageList New Icon 16,16,24,3 To hImageList
   ImageList Add Icon hImageList, "up"
   ImageList Add Icon hImageList, "down"
 
   'sample data
   For i = 10 To 1 Step -1 : ListView Insert Item hDlg, %IDC_ListView, 10,0, "Row " + Format$(i,"00") : Next i
   For i = 1 To 10 : ListView Set Text hDlg, %IDC_ListView, i, 2, Format$(Rnd(i,99),"00") : Next i
 
   'assign imagelist to header
   hHeader = ListView_GetHeader(hListView)
   Header_SetImageList hHeader, hImagelist
End Sub
 
Sub old_SetHeaderIcons(ByVal iActiveColumn As Integer)
   Local i, iAlignRight As Integer, hdi As HD_ITEM
   For i = 0 To Header_GetItemCount(hHeader) - 1
      hdi.mask = %HDI_FORMAT Or %HDI_IMAGE
      Header_GetItem hHeader, i+1, hdi
      iAlignRight = (hdi.fmt = %HDF_RIGHT)
      hdi.fmt = %HDF_STRING Or (iAlignRight And %HDF_RIGHT)
      If i = iActiveColumn Then
         hdi.fmt = hdi.fmt Or %HDF_IMAGE Or ((iAlignRight = %FALSE) And %HDF_BITMAP_ON_RIGHT)
      End If
      hdi.iImage = SortDirection
      Header_SetItem(hHeader, i, hdi)
   Next
End Sub
 
Sub SetHeaderIconsOnRight(ByVal iActiveColumn As Integer)
   Local i, iAlignRight As Integer, hdi As HD_ITEM
   For i = 0 To Header_GetItemCount(hHeader) - 1
      hdi.mask = %HDI_FORMAT Or %HDI_IMAGE
      hdi.fmt = %HDF_String Or %HDF_Image Or %HDF_Bitmap_On_Right
      If i = iActiveColumn Then hdi.iImage = SortDirection Else hdi.iImage = 3
      Header_SetItem(hHeader, i, hdi)
   Next
End Sub
 
Sub SetHeaderIconsOnLeft(ByVal iActiveColumn As Integer)
   Local i, iAlignRight As Integer, hdi As HD_ITEM
   For i = 0 To Header_GetItemCount(hHeader) - 1
      hdi.mask = %HDI_FORMAT Or %HDI_IMAGE
      hdi.fmt = %HDF_String Or %HDF_Image           'left is the default
      If i = iActiveColumn Then hdi.iImage = SortDirection Else hdi.iImage = 3
      Header_SetItem(hHeader, i, hdi)
   Next i
End Sub
 
'gbs_00986
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm