天天看點

擷取滑鼠指針在ListBox中的行位置

Listbox控件沒有提供這個屬性值可以調用,這裡我寫了個完整的示範程式,供大家參考.

建立一個工程,在Form1窗體上加上一個ListBox和一個Label.在窗體代碼中粘貼以下程式,然後運作即可.

Option Explicit

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 LB_ITEMFROMPOINT = &H1A9

Private Sub Form_Load()

    Dim i As Integer

    For i = 1 To 5

        List1.AddItem Trim(Str(i))

    Next

End Sub

Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

    Dim LstPOS As Long

    Dim XX As Long

    Dim YY As Long

    XX = CLng(X / Screen.TwipsPerPixelX)

    YY = CLng(Y / Screen.TwipsPerPixelY)

    LstPOS = SendMessage(List1.hwnd, LB_ITEMFROMPOINT, 0, ByVal ((YY * 65536) + XX))

    If LstPOS < List1.ListCount Then

       Label1.Caption = "目前位置為: " & LstPOS

    End If

End Sub

'-------------------------------------------

' 轉載請注明出處

' 作者:唐細剛

' 郵箱:[email protected]

'-------------------------------------------

繼續閱讀