Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#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"
Type MyType
col1 As String * 50
col2 As String * 50
End Type
%IDC_ListView = 400
Global hDlg, hListView, hImageList, hHeader As Dword, SortDirection As Long
Global D() As MyType
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))
Incr SortDirection : If SortDirection > 2 Then SortDirection = 0
Select Case SortDirection
Case 0
LoadListView
Case 1
ListView Sort hDlg, %IDC_ListView, LVData.iSubItem+1, Ascend
Case 2
ListView Sort hDlg, %IDC_ListView, LVData.iSubItem+1, Descend
End Select
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,180,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", 90, 0
ListView Insert Column hDlg, %IDC_ListView, 2, "Col2", 90, 0
'create imagelist
ImageList New Icon 16,16,24,3 To hImageList
ImageList Add Icon hImageList, "down"
ImageList Add Icon hImageList, "up"
'sample data
ReDim D(1 To 10)
For i = 10 To 1 Step -1 : D(i).col1 = "Row " + Format$(Rnd(i,99),"00") : Next i
For i = 1 To 10 : D(i).col2 = Format$(Rnd(i,99),"00") : Next i
'assign imagelist to header
hHeader = ListView_GetHeader(hListView)
Header_SetImageList hHeader, hImagelist
LoadListView
End Sub
Sub LoadListView
Local i As Long
ListView Reset hDlg, %IDC_ListView
For i = 1 To UBound(D)
ListView Insert Item hDlg, %IDC_ListView, 1,0, D(i).col1
ListView Set Text hDlg, %IDC_ListView, 1, 2, D(i).col2
Next i
End Sub
Sub SetHeaderIconsOnRight(ByVal iActiveColumn As Integer)
Local i 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
hdi.iImage = 3
If (i=iActiveColumn) And SortDirection Then hdi.iImage = SortDirection-1
Header_SetItem(hHeader, i, hdi)
Next
End Sub
'gbs_01077
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm