#include <ColorConstants.au3> ;背景颜色
#include <GUIConstantsEx.au3> ;窗体
#include <ListViewConstants.au3> ;列表
#include <WindowsConstants.au3> ;窗体
AutoItSetOption("GUIOnEventMode", 1)
Dim $nCurCol = -1
Dim $nSortDir = 1
Dim $bSet = 0
Dim $nCol = -1
Global Const $LVM_GETITEM = $LVM_FIRST + 5
Dim $aItemData[10] = ["ABC|666|10.05.2004|vn|dslfgshdl", _
"DEF|5|11.05.2005|yio|两个结论", _
"XYZ|31|12.05.2004|kjdfh|呵呵楼上的", _
"XYZ|22|12.05.2004|dsg|呵呵楼上的", _
"XYZ|45|12.05.2004|sda|呵呵楼上的", _
"XYZ|56|12.05.2004|cxvb|呵呵楼上的", _
"XYZ|765|12.05.2004|gfj|呵呵楼上的", _
"XYZ|54|12.05.2004|dsg|呵呵楼上的", _
"XYZ|34|12.05.2004|xvcz|呵呵楼上的", _
"XYZ|98|12.05.2004|weyt|呵呵楼上的"]
$hGUI = GUICreate("Test", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "funcGUISetOnEvent")
$lvDataResult = GUICtrlCreateListView("列1|列2|列3|列4|列5", 10, 10, 280, 180, BitOR($LVS_SHOWSELALWAYS, $LVS_REPORT))
GUICtrlSetBkColor($lvDataResult, 0xFFFFFF)
GUICtrlSetBkColor($lvDataResult, $GUI_BKCOLOR_LV_ALTERNATE)
GUICtrlSetOnEvent($lvDataResult, "funcListViewClick")
GUICtrlRegisterListViewSort($lvDataResult, "funcListViewSrot")
funcCreateItem()
GUISetState()
While 1
WEnd
Func funcCreateItem()
For $i = 0 To UBound($aItemData) - 1
GUICtrlCreateListViewItem($aItemData[$i], $lvDataResult)
GUICtrlSetBkColor (-1, 0xFFA500 );设置listviewitem的背景色
Next
EndFunc
Func funcListViewClick()
$bSet = 0
$nCurCol = $nCol
GUICtrlSendMsg($lvDataResult, $LVM_SETSELECTEDCOLUMN, GUICtrlGetState($lvDataResult), 0)
; 文件 返回类型 调用函数 "类型 1=窗口句柄 (指针)" 参数 1 类型 n" 参数n
DllCall("user32.dll", "int", "InvalidateRect", "hwnd", GUICtrlGetHandle($lvDataResult), "int", 0, "int", 1)
;InvalidateRect:向指定的窗体更新区域添加一个矩形,并重新绘制
EndFunc
;自定义的回调函数(listview控件ID, 第一个项目的lParam值-默认为该项目的控件ID, 第二个项目的lParam值-默认为该项目的控件ID, 点击排序的列)
Func funcListViewSrot($hWnd, $nItem1, $nItem2, $nColumn)
Local $nSort
;切换排序方向
If $nColumn = $nCurCol Then
If Not $bSet Then
$nSortDir = $nSortDir * - 1
$bSet = 1
EndIf
Else
$nSortDir = 1
EndIf
$nCol = $nColumn
$val1 = GetSubItemText($lvDataResult, $nItem1, $nColumn)
$val2 = GetSubItemText($lvDataResult, $nItem2, $nColumn)
;如果是第3列(开始于0)那么对比数据
If $nColumn = 2 Then
$val1 = StringRight($val1, 4) & StringMid($val1, 4, 2) & StringLeft($val1, 2)
$val2 = StringRight($val2, 4) & StringMid($val2, 4, 2) & StringLeft($val2, 2)
EndIf
$nResult = 0 ;item1和item2位置未改变
$val_1 = Number($val1)
If $val_1 = 0 Then $val_1 = $val1
$val_2 = Number($val2)
If $val_2 = 0 Then $val_2 = $val2
If $val_1 < $val_2 Then
$nResult = -1 ;把item2放在item1前面
ElseIf $val_1 > $val_2 Then
$nResult = 1 ;把item2放在item1后面
EndIf
$nResult = $nResult * $nSortDir
Return $nResult
EndFunc ;==>LVSort
;检索指定列的单元格文本
Func GetSubItemText($nCtrlID, $nItemID, $nColumn)
Local $stLvfi = DllStructCreate("uint;ptr;int;int[2];int")
DllStructSetData($stLvfi, 1, $LVFI_PARAM)
DllStructSetData($stLvfi, 3, $nItemID)
Local $stBuffer = DllStructCreate("char[260]")
$nIndex = GUICtrlSendMsg($nCtrlID, $LVM_FINDITEM, -1, DllStructGetPtr($stLvfi));
Local $stLvi = DllStructCreate("uint;int;int;uint;uint;ptr;int;int;int;int")
DllStructSetData($stLvi, 1, $LVIF_TEXT)
DllStructSetData($stLvi, 2, $nIndex)
DllStructSetData($stLvi, 3, $nColumn)
DllStructSetData($stLvi, 6, DllStructGetPtr($stBuffer))
DllStructSetData($stLvi, 7, 260)
GUICtrlSendMsg($nCtrlID, $LVM_GETITEM, 0, DllStructGetPtr($stLvi));
$sItemText = DllStructGetData($stBuffer, 1)
$stLvi = 0
$stLvfi = 0
$stBuffer = 0
Return $sItemText
EndFunc ;==>GetSubItemText
Func funcGUISetOnEvent()
Exit
EndFunc
请指教!