我們可以利用 ListIndex 屬性得知 ListBox 的選項, 但是當鼠標移到某一個選項上面(但還沒有選取),如何得知此一選項呢?方法是對 ListBox 送出LB_ITEMFROMPOINT 信息, 細節(jié)如下:
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
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

