|
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
Dim i As Long
With ListView1
For i = 1 To .ColumnHeaders.Count '还原无三角形的标题
.ColumnHeaders(i).Text = strTitle(i)
Next
If .SortKey = ColumnHeader.Index - 1 Then '排序设置,交替执行。每列第一次排序默认升序
.SortOrder = IIf(.SortOrder, 0, 1)
Else
.SortKey = ColumnHeader.Index - 1
.SortOrder = lvwAscending
End If
LvmSort ListView1, .SortKey, arrSort(.SortKey), .SortOrder '调用自定义排序程序
ColumnHeader.Text = IIf(.SortOrder, "▼" & ColumnHeader.Text, "▲" & ColumnHeader.Text) '在标题上显示升降序三角,也可用ColumnHeader.Icon属性,只要在ImageList放置两个三角形图片
End With
End Sub
Public Function LvmSort(lv As ListView, ByVal Index As Long, ByVal ItemType As LVItemTypes, ByVal SortOrder As LVSortTypes) As Boolean
With lv
.Sorted = False
.SortKey = Index
.SortOrder = SortOrder
m_lvSortColumn = Index
m_lvSortType = ItemType
m_lvHWnd = .hwnd
Call SendMessageLong(.hwnd, LVM_SORTITEMS, SortOrder, AddressOf LvmCompareProc)
End With
End Function
Private Function LvmCompareProc(ByVal lParam1 As Long, ByVal lParam2 As Long, ByVal SortOrder As Long) As Long
Dim dat1 As Date, dat2 As Date
Dim dbl1 As Double, dbl2 As Double
Dim str1 As String, str2 As String
str1 = LvmGetItemText(lParam1, m_lvHWnd)
str2 = LvmGetItemText(lParam2, m_lvHWnd)
Select Case m_lvSortType
Case lvAlphabetic
LvmCompareProc = StrComp(str1, str2, vbTextCompare)
Case lvNumber
If IsNumeric(str1) Then dbl1 = CDbl(str1) Else dbl1 = -4.94065645841247E-324
If IsNumeric(str2) Then dbl2 = CDbl(str2) Else dbl2 = -4.94065645841247E-324
LvmCompareProc = Sgn(dbl1 - dbl2)
Case lvDate
If IsDate(str1) Then dat1 = CDate(str1) Else dat1 = CDate(0)
If IsDate(str2) Then dat2 = CDate(str2) Else dat2 = CDate(0)
LvmCompareProc = Sgn(dat1 - dat2)
Case lvBinary
LvmCompareProc = StrComp(str1, str2, vbBinaryCompare)
Case Else
LvmCompareProc = StrComp(str1, str2, vbTextCompare)
End Select
If SortOrder = lvDescending Then LvmCompareProc = -LvmCompareProc
End Function
Private Function LvmGetItemText(lParam As Long, hwnd As Long) As String
Dim tpyFind As LV_FINDINFO
Dim tpyItem As LV_ITEM
Dim Index As Long
Dim lngRet As Long
With tpyFind
.flags = LVFI_PARAM
.lParam = lParam
End With
Index = SendMessage(hwnd, LVM_FINDITEM, -1, tpyFind)
With tpyItem
.Mask = LVIF_TEXT
.SubItem = m_lvSortColumn
.Text = Space(32)
.TextMax = Len(.Text)
End With
lngRet = SendMessage(hwnd, LVM_GETITEMTEXT, Index, tpyItem)
If lngRet Then LvmGetItemText = Left$(tpyItem.Text, lngRet)
End Function |
|