怎样使ListBox控件的列表项目随鼠标移动而自动亮显?
即鼠标移动时,其最近的列表项目加亮显示,而不是等单击左键时才加亮显示?类似Combobox的下拉列表或IE的地址栏的下拉列表。

解决方案 »

  1.   

    记得有个叫hottrack的属性,设置后就会自动有这样的功能,不知记错没
      

  2.   


    没有做过的,千万不可妄加揣测。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.   

    可能会有哪些副作用?
    我试用了#3楼of123的代码,正可以解决ComboBox的下拉列表框仅显示8行就需滚动的不足。
      

  4.   

    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
      

  5.   

    #5楼的代码会导致 List_Click( )事件,也许就是#1楼所说的副作用之一吧.