CSDN博客

img tanaya
博客专家

获取鼠标指针在ListBox中的行位置

发表于2004/9/21 10:48:00  2300人阅读

分类: >> VB6

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

'-------------------------------------------
' 转载请注明出处
' 作者:唐细刚
' 邮箱:tanaya@163.com
'-------------------------------------------

0 0

相关博文

我的热门文章

img
取 消
img