關於listview控制元件中查詢問題
可以用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