可以用finditem方法进行查找,找到后用ensurevisible使该item可见。
那么如何能使这个item始终处于那一页的页顶呢??
就如msdn一样,用索引搜索某一信息时,底下listview控件总是把搜到的信息
放到该页的第一行.
我方法用遍了包括API,都找不到方法,请高手帮助
---------------------------------------------------------------
Option Explicit
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const SB_LINEDOWN As Long = 1
Private Const WM_VSCROLL As Long = &H115
Private Sub Command1_Click()
Dim itemX As ListItem
Set itemX = ListView1.FindItem(Text1.Text, , , lvwPartial)
If Not itemX Is Nothing Then
itemX.Selected = True
itemX.EnsureVisible
Call SetTopIndex(itemX.index)
End If
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 0 To 50
ListView1.ListItems.Add , , i & " item"
Next
End Sub
Private Sub SetTopIndex(ByVal index As Long)
Dim i As Long
Dim v As Long
v = (ListView1.ListItems(index).Top - ListView1.Top) / ListView1.ListItems(1).Height
For i = 1 To v
Call SendMessage(ListView1.hwnd, WM_VSCROLL, SB_LINEDOWN, ByVal 0&)
Next
End Sub
实在感谢上面的朋友,我对程序做了些修改,就可以了
v=(ListView1.ListItems(index).Top - ListView1.Top) / ListView1.GetFirstVisible.Height
另外用LockWindowUpdate来解决滚动条自我滚动时产生的闪烁。
就可以做到和msdn一样的效果了。
发送 消息 进行查找,速度 很快的。
================================================================
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type LVFINDINFO
flags As Long
psz As String
lParam As Long
pt As POINTAPI
vkDirection As Long
End Type
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LVM_FIRST = &H1000
Private Const LVM_FINDITEM = (LVM_FIRST + 13)
Private Const LVFI_PARAM = &H1
Private Const LVFI_STRING = &H2
Private Const LVFI_PARTIAL = &H8
Private Const LVFI_WRAP = &H20
Private Const LVFI_NEARESTXY = &H40
Private Sub Command1_Click()
Dim lRet As Long, LFI As LVFINDINFO
LFI.flags = LVFI_PARTIAL Or LVFI_WRAP
LFI.psz = Text1.Text
lRet = SendMessage(ListView1.hwnd, LVM_FINDITEM, -1, LFI)
If lRet >= 0 Then Set ListView1.SelectedItem = ListView1.ListItems(lRet + 1): ListView1.SetFocus
End Sub
Private Sub Form_Load()
Dim li As ListItem, k As Long
For k = 1 To 10
Set li = ListView1.ListItems.Add(, , "Line " & k): li.SubItems(1) = "L" & k
Next
End Sub