我们可以利用 ListIndex 属性得知 ListBox 的选项, 但是当鼠标移到某一个选项上面(但还没有选取),如何得知此一选项呢?方法是对 ListBox 送出LB_ITEMFROMPOINT 信息, 细节如下:
1. API 的声明:
Const LB_ITEMFROMPOINT = &.H1A9
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
注:如果以上的声明放在「一般模块」底下, 应在 Const 之前加上 Public 保留字, 并且将 Private 保留字去掉。
2. 调用例:(在表单上布置一个 TextBox 及一个 ListBox, 然后利用 MouseMove 事件程序来检测鼠标所在位置的选项)
Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim pos As Long, idx As Long
pos = X / Screen.TwipsPerPixelX Y / Screen.TwipsPerPixelY * 65536
idx = SendMessage(List1.hwnd, LB_ITEMFROMPOINT, 0, ByVal pos)
idx 即等于鼠标所在位置的选项
If Idx < 65536 Then Text1.Text = List1.List(idx)
End Sub