#include <GuiListView.au3>
GUICreate("ListView Find Text", 400, 300)
$hListView = GUICtrlCreateListView("", 2, 2, 394, 268)
GUISetState()
_GUICtrlListView_AddColumn($hListView, "Items", 100)
_GUICtrlListView_BeginUpdate($hListView)
_GUICtrlListView_AddItem($hListView, "我爱你123" & 1)
_GUICtrlListView_AddItem($hListView, "123我爱你" & 2)
_GUICtrlListView_AddItem($hListView, "你爱我abc3")
For $iI = 4 To 49
_GUICtrlListView_AddItem($hListView, "你爱我" & $iI)
Next
_GUICtrlListView_AddItem($hListView, "Target item")
For $iI = 51 To 100
_GUICtrlListView_AddItem($hListView, "Item " & $iI)
Next
_GUICtrlListView_AddItem($hListView, "你爱d")
_GUICtrlListView_EndUpdate($hListView)
; 搜索目标项目
$iI = __GUICtrlListView_FindText($hListView, "你爱d", -1, true)
MsgBox(4160, "Information", "Target Item Index: " & $iI)
_GUICtrlListView_EnsureVisible($hListView, $iI)
Do
Until GUIGetMsg() = -3
GUIDelete()
Func __GUICtrlListView_FindText($hWnd, $sText, $iStart = -1, $fPartialOK = True, $fWrapOK = True)
Local $tFindInfo = DllStructCreate($tagLVFINDINFO)
Local $iFlags = $LVFI_STRING
If $fPartialOK Then $iFlags = BitOR($iFlags, $LVFI_PARTIAL)
If $fWrapOK Then $iFlags = BitOR($iFlags, $LVFI_WRAP)
DllStructSetData($tFindInfo, "Flags", $iFlags)
Return __GUICtrlListView_FindItem($hWnd, $iStart, $tFindInfo, $sText)
EndFunc ;==>__GUICtrlListView_FindText
Func __GUICtrlListView_FindItem($hWnd, $iStart, ByRef $tFindInfo, $sText = '')
Local $aSR = StringRegExp($sText, '[^\x00-\xff]', 3), $iL = UBound($aSR)
Local $iBuffer = StringLen($sText) + 1 + $iL
Local $tBuffer = DllStructCreate('char Text[' & $iBuffer & ']')
Local $pBuffer = DllStructGetPtr($tBuffer)
For $i = 0 To $iL - 2
$sText &= ' '
Next
DllStructSetData($tBuffer, 'Text', $sText)
Local $iRet
If IsHWnd($hWnd) Then
If _WinAPI_InProcess($hWnd, $_lv_ghLastWnd) Then
DllStructSetData($tFindInfo, 'Text', $pBuffer)
$iRet = _SendMessage($hWnd, $LVM_FINDITEM, $iStart, $tFindInfo, 0, 'wparam', 'struct*')
Else
Local $iFindInfo = DllStructGetSize($tFindInfo)
Local $tMemMap
Local $pMemory = _MemInit($hWnd, $iFindInfo + $iBuffer, $tMemMap)
Local $pText = $pMemory + $iFindInfo
DllStructSetData($tFindInfo, 'Text', $pText)
_MemWrite($tMemMap, $tFindInfo, $pMemory, $iFindInfo)
_MemWrite($tMemMap, $tBuffer, $pText, $iBuffer)
$iRet = _SendMessage($hWnd, $LVM_FINDITEM, $iStart, $pMemory, 0, 'wparam', 'ptr')
_MemFree($tMemMap)
EndIf
Else
DllStructSetData($tFindInfo, 'Text', $pBuffer)
$iRet = GUICtrlSendMsg($hWnd, $LVM_FINDITEM, $iStart, DllStructGetPtr($tFindInfo))
EndIf
Return $iRet
EndFunc ;==>__GUICtrlListView_FindItem