我們可以利用 ListIndex 屬性得知 ListBox 的選項(xiàng), 但是當(dāng)鼠標(biāo)移到某一個(gè)選項(xiàng)上面(但還沒有選取),如何得知此一選項(xiàng)呢?方法是對 ListBox 送出LB_ITEMFROMPOINT 信息, 細(xì)節(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
注:如果以上的聲明放在「一般模塊」底下, 應(yīng)在 Const 之前加上 Public 保留字, 并且將 Private 保留字去掉。
2. 調(diào)用例:(在表單上布置一個(gè) TextBox 及一個(gè) ListBox, 然后利用 MouseMove 事件程序來檢測鼠標(biāo)所在位置的選項(xiàng))
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 即等于鼠標(biāo)所在位置的選項(xiàng)
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
注:如果以上的聲明放在「一般模塊」底下, 應(yīng)在 Const 之前加上 Public 保留字, 并且將 Private 保留字去掉。
2. 調(diào)用例:(在表單上布置一個(gè) TextBox 及一個(gè) ListBox, 然后利用 MouseMove 事件程序來檢測鼠標(biāo)所在位置的選項(xiàng))
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 即等于鼠標(biāo)所在位置的選項(xiàng)
If Idx < 65536 Then Text1.Text = List1.List(idx)
End Sub

