【VisualFreeBasic】ListView控件点击列名排序
ListView控件,列表名点击一下,自动排序此列,同时显示小3角,再点一次自动切换正序与倒序,
啥都不说,直接上代码,复制去,调用即可。
使用例题
'用法:在列单击事件中加入下面内容即可
'Function FORM10_LISTVIEW2_LVN_COLUMNCLICK ( _
' ControlIndexAs Long, _' 在控件数组索引
' hWndForm As DWord, _' 窗体的句柄
' hWndControl As DWord, _' 控件的句柄
' ByVal lpNMV As NM_LISTVIEW Ptr_' 指针指向 NM_LISTVIEW
' ) As Long
' ' 排序 ListView
' JPL_ListView_ColumnToSort(hWndControl, lpNMV)
'End Function
排序源码:
' 要能够使用 ListView_ColumnToSort() 函数的结构
Type JPL_ListView_ComparePar
hWndControl As HWnd
Column As UInteger
SortOrder As UInteger
End Type
' 常数
Const SO_NONE = 0
Const SO_ASCENDING= 1
Const SO_DESCENDING = 2
'--------------------------------------------------------------------------------
Sub JPL_ListView_DrawHeaderArrow(hWndControl As HWnd , ByVal pColumn As Long, ByVal pSortOrder As Long)
Dim hHeader As HWnd
Dim HDI As HD_ITEM
hHeader =Cast(HWnd, SendMessage(hWndControl, LVM_GetHeader, 0, 0) )
HDI.mask = HDI_FORMAT
Header_GetItem(hHeader, pColumn, @HDI)
' 取决于排序次序 (SO_NONE, SO_ASCENDING, SO_DESCENDING)
Select Case pSortOrder
Case SO_NONE
' 删除向下箭头或向上箭头
HDI.fmt = HDI.fmt And Not (HDF_SORTDOWN Or HDF_SORTUP)
Case SO_ASCENDING
' 这一项目上绘制一个向上箭头
HDI.fmt = HDI.fmt And Not HDF_SORTDOWN
HDI.fmt = HDI.fmt Or HDF_SORTUP
Case SO_DESCENDING
' 在这个项目上绘制一个向下箭头
HDI.fmt = HDI.fmt And Not HDF_SORTUP
HDI.fmt = HDI.fmt Or HDF_SORTDOWN
End Select
Header_SetItem(hHeader, pColumn, @HDI)
End Sub
'--------------------------------------------------------------------------------
Function JPL_ListView_CompareFunc ( ByVal index1 As Long, _
ByVal index2 As Long, _
ByVal lpListView_ComparePar As JPL_ListView_ComparePar Ptr _
)As Long
Dim zItem1 As String
Dim zItem2 As String
Dim lNumeric1 As Double
Dim lNumeric2 As Double
Dim lReturn As Integer
' 从列表视图中获取价值
zItem1 =FF_ListView_GetItemText(lpListView_ComparePar->hWndControl, index1, lpListView_ComparePar->Column)
zItem2 =FF_ListView_GetItemText(lpListView_ComparePar->hWndControl, index2, lpListView_ComparePar->Column)
'--------------------------------------------------------------------------------------------
' 确定哪种类型的排序 (日期、 数字或字符) 根据列点击
'--------------------------------------------------------------------------------------------
' 日期
'If IsDate(zItem1) And IsDate(zItem2) Then '速度太慢,放弃
' lNumeric1 = DateValue(zItem1)
' lNumeric2 = DateValue(zItem2)
' If lNumeric1 < lNumeric2 Then lReturn = -1 Else If lNumeric1 > lNumeric2 Then lReturn = +1 Else lReturn = 0
'Else
' 数字
If JPL_IsNumeric(zItem1) And JPL_IsNumeric(zItem2) Then
zItem1=FF_Remove (zItem1," ") ' Remove Any numeric 1000's space
zItem2=FF_Remove (zItem2," ")
lNumeric1 = Val(zItem1)
lNumeric2 = Val(zItem2)
If lNumeric1 < lNumeric2 Then lReturn = -1 Else If lNumeric1 > lNumeric2 Then lReturn = +1 Else lReturn = 0
Else
' 字符
zItem1 = LTrim(zItem1)
zItem2 = LTrim( zItem2 )
lReturn = Lstrcmpi(zItem1, zItem2)
End If
'End If
If lpListView_ComparePar->SortOrder = SO_ASCENDING Then Function = lReturn Else Function = -lReturn
End Function
'--------------------------------------------------------------------------------
Sub JPL_ListView_ColumnToSort(hWndControl As HWnd , ByVal lpNMV As NM_LISTVIEW Ptr)
' 为比较结构
Dim lListView_ComparePar As JPL_ListView_ComparePar
' 局部变量
Dim lListViewSortOrder As Long
Dim lListViewLastSortedColumn As Long
' 加载列表视图的当前值
lListViewSortOrder = ValInt(FF_Control_GetTag (hWndControl))
lListViewLastSortedColumn = ValInt(FF_Control_GetTag2(hWndControl))
' 若要反转排序顺序
If lListViewSortOrder = SO_ASCENDING Then lListViewSortOrder = SO_DESCENDING Else lListViewSortOrder = SO_ASCENDING
' 遏止以前排序的列上的箭头 (包含零被单击的基于的列号)
If lListViewLastSortedColumn <> - 1 And _
lListViewLastSortedColumn <> lpNMV->iSubItem ThenJPL_ListView_DrawHeaderArrow(hWndControl, lListViewLastSortedColumn, SO_NONE )
' 排序列表视图中选择的列
lListView_ComparePar.hWndControl = hWndControl
lListView_ComparePar.Column = lpNMV->iSubItem
lListView_ComparePar.SortOrder = lListViewSortOrder
SendMessage( hWndControl , LVM_SORTITEMSEX ,Cast(wParam, @lListView_ComparePar ),Cast(lParam,@JPL_ListView_CompareFunc ) )
' 向下箭头或向上箭头上绘制这一项目。
JPL_ListView_DrawHeaderArrow(hWndControl, lpNMV->iSubItem, lListViewSortOrder)
' 要保存的已排序的列号
lListViewLastSortedColumn = lpNMV->iSubItem
' 保存对此列表视图的最新设置
FF_Control_SetTag(hWndControl , Str(lListViewSortOrder))
FF_Control_SetTag2(hWndControl, Str(lListViewLastSortedColumn))
End Sub
'--------------------------------------------------------------------------------
Function JPL_IsNumeric(pValue As String ) As Long
If UCase(Left(pValue,2))="&H" Then
If FF_Verify (1,Mid(pValue,3), "0123456789") = 0 Then Function = True Else Function = False
Else
If FF_Verify (1,pValue, "0123456789.,'+-") = 0 Then Function = True Else Function = False
End If
End Function
看官们是不是觉得和VB代码很像,但的确不是VB,而好VFB,VB想用?但需要改造,只是太麻烦了。 纠正错误:
在最后函数里,有16进制判断,
"0123456789" 应该写:"0123456789ABCDEF"
但还是存在大小写问题的,假如16进制用小写字母,就有问题。如何做,不用我说了吧。
你们懂的,楼主好人
页:
[1]