获取listview的单击列的列索引

时间:2018-01-31 16:57:37

标签: listview vb6

我没有找到检索列索引的本机过程,该信息位于此处:

ListView1.ListItems.Item (ListView1.SelectedItem.Index) .ListSubItems (HERE)

我知道如何获得这样的行索引:

ListView1.SelectedItem.Index

我知道如何像这样恢复鼠标的位置:

Private Sub ListView1_MouseUp (ByVal Button As Integer, ByVal Shift As Integer, _
                               ByVal x As stdole.OLE_XPOS_PIXELS, _
                               ByVal y As stdole.OLE_YPOS_PIXELS)

我认为应该可以找到这个索引。 提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

本机方式是使用LVM_SUBITEMHITTEST。

模块:

Private Type POINTAPI
    X As Long
    Y As Long
End Type

Private Type LVHITTESTINFO
    pt As POINTAPI
    flags As Long
    iItem As Long
    iSubItem  As Long
End Type

Private Const LVM_SUBITEMHITTEST As Long = &H1039
Private Const LVHT_ONITEM        As Long = &HE

Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

形式:

Private Sub yourListView_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim hitTest As LVHITTESTINFO

    With hitTest
        .flags = LVHT_ONITEM
        .pt.X = (X \ Screen.TwipsPerPixelX)
        .pt.Y = (Y \ Screen.TwipsPerPixelY)
    End With

    SendMessage yourListView.hwnd, LVM_SUBITEMHITTEST, 0, hitTest

    If (hitTest.iItem < 0) Then Exit Sub

    If hitTest.iSubItem = 0 Then
        MsgBox yourListView.ListItems(hitTest.iItem + 1).Text
    Else
        MsgBox yourListView.ListItems(hitTest.iItem + 1).SubItems(hitTest.iSubItem)
    End If
End Sub