改进的ListBox中的字符串超长显示的解决方法(支持dirListBox,FileListBox)
程序代码:
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 Private Const LB_ITEMFROMPOINT = &H1A9 Private Function ShowListTips(ByRef objList As Control, Button As Integer, Shift As Integer, X As Single, Y As Single) As Long On Error GoTo errHandler '//! 支持dirListBox、FileListBox,ListBox Dim lXPoint As Long, lYPoint As Long Dim lIndex As Long, n As Long If Button = 0 Then 'if no button was pressed lXPoint = CLng(X / Screen.TwipsPerPixelX) lYPoint = CLng(Y / Screen.TwipsPerPixelY) With objList 'get selected item from list lIndex = SendMessage(.hwnd, LB_ITEMFROMPOINT, 0, ByVal ((lYPoint * 65536) + lXPoint)) 'get the hidden item count if the objList is a dirListbox 'If TypeOf objList Is DirListBox Then If TypeName(objList) = "DirListBox" Then n = UBound(Split(objList.List(0), "\")) End If 'show tip or clear last one If (lIndex >= 0) And (lIndex <= .ListCount+n) Then .ToolTipText = .List(lIndex-n) Else .ToolTipText = "" End If End With End If Exit Function errHandler: Debug.Print Err.Number, Err.Description End Function '调用示例: '在ListBox的MouseMove事件中 'Call ShowListTips(控件名称,Button,Shift,X,Y)
[ 本帖最后由 jiashie 于 2010-6-29 10:24 编辑 ]