没有做过的,千万不可妄加揣测。AutoList :在 ListBox 的 MouseMove 事件中,调用下列函数,将 ListBox 的 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 TypePrivate Declare Function ClientToScreen Lib "user32" _ (ByVal hwnd As Long, lpPoint As POINTAPI) As LongPrivate Declare Function LBItemFromPt Lib "COMCTL32.DLL" _ (ByVal hLB As Long, ByVal ptX As Long, ByVal ptY As Long, _ ByVal bAutoScroll As Long) As LongPrivate Declare Function SendMessage Lib "user32" Alias _ "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, lParam As Long) As LongPublic 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 IfEnd Sub
Option Explicit Private 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 LongPrivate 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) If idx < 65536 Then List1.ListIndex = idx End Sub Private Sub Form_Load() Dim I As Long For I = 0 To 100 List1.AddItem I Next End Sub
没有做过的,千万不可妄加揣测。AutoList :在 ListBox 的 MouseMove 事件中,调用下列函数,将 ListBox 的 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 TypePrivate Declare Function ClientToScreen Lib "user32" _
(ByVal hwnd As Long, lpPoint As POINTAPI) As LongPrivate Declare Function LBItemFromPt Lib "COMCTL32.DLL" _
(ByVal hLB As Long, ByVal ptX As Long, ByVal ptY As Long, _
ByVal bAutoScroll As Long) As LongPrivate Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Long) As LongPublic 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 IfEnd Sub
我试用了#3楼of123的代码,正可以解决ComboBox的下拉列表框仅显示8行就需滚动的不足。
Private 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 LongPrivate 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)
If idx < 65536 Then List1.ListIndex = idx
End Sub
Private Sub Form_Load()
Dim I As Long
For I = 0 To 100
List1.AddItem I
Next
End Sub