ListBox中的字符串超长显示的解决方法

使用ToolTip来解决超长文字的显示问题是不错的方案,下面给出了例子的代码。

Option Explicit
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 Sub Form_Load()
    Dim i As Integer
    For i = 0 To 5
        List1.AddItem "ListItem字符串超长超长超长超长超长咯    " & i
    Next
End Sub

Private Sub List1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim lParam As Long
    Dim lResult As Long
    lParam = (CInt(Y / Screen.TwipsPerPixelY) * 2 ^ 16) + CInt(X / Screen.TwipsPerPixelX)
    lResult = SendMessage(List1.hwnd, LB_ITEMFROMPOINT, 0, ByVal lParam)
   
    If lResult < 0 Or lResult > 32767 Then
        List1.ToolTipText = ""
        Exit Sub
    End If
   
    Dim nIndex As Integer
    nIndex = CInt(lResult)
    List1.ToolTipText = List1.List(nIndex)
End Sub

在 Win2000 + VB6 中调试通过

你可能感兴趣的:(Visual,Basic,6)