如何获取ComboBox下拉框处在打开状态的值?
我想得到当ComboBox下拉框打开时,鼠标指向某一选项时(只是鼠标移到该待选项上,没有单击鼠标左键选择)该选项的
ListIndex 值,也就是在MouseMove时获得ListIndex值。请高手帮忙,先谢了!
[解决办法]
ComboBox 没有 Mousemove 事件。
可用 TextBox, ListBox 和 CommandButton 模拟一个 ComboBox。
在 ListBox 的 Mousemove 事件中,用 hWnd, x, y 值调用下面的函数,鼠标处的列表项可被选中:
Option Explicit
Private Const LB_SETCURSEL = &H186
Private Const LB_GETCURSEL = &H188
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function ClientToScreen Lib "user32" _
(ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function LBItemFromPt Lib "COMCTL32.DLL" _
(ByVal hLB As Long, ByVal ptX As Long, ByVal ptY As Long, _
ByVal bAutoScroll As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Long) As Long
Public Sub HighlightLBItem(ByVal LBHwnd As Long, _
ByVal X As Single, ByVal Y As Single)
Dim ItemIndex As Long
Dim AtThisPoint As POINTAPI
AtThisPoint.X = X \ Screen.TwipsPerPixelX
AtThisPoint.Y = Y \ Screen.TwipsPerPixelY
Call ClientToScreen(LBHwnd, AtThisPoint)
ItemIndex = LBItemFromPt(LBHwnd, AtThisPoint.X, _
AtThisPoint.Y, False)
If ItemIndex <> SendMessage(LBHwnd, LB_GETCURSEL, 0, 0) Then
Call SendMessage(LBHwnd, LB_SETCURSEL, ItemIndex, 0)
End If
End Sub
[解决办法]