本文主要是介绍获取鼠标指针在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
'-------------------------------------------
' 转载请注明出处
' 作者:唐细刚
' 邮箱:tanaya@163.com
'-------------------------------------------
这篇关于获取鼠标指针在ListBox中的行位置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!