End Property
Public Property Get MaxTipWidth() As Long
'
' If MaxTipWidth is -1, there is no word wrapping and
' text control characters are printed and not
' evaluated (i.e. a vbCrLf shows up as "||")
'
If (mnlgHwndTT = 0) Then Exit Property
MaxTipWidth = fLowWord(SendMessageT(mnlgHwndTT, TTM_GETMAXTIPWIDTH, 0, 0))
End Property
Private Function fLowWord(ByVal lngValue As Long) As Integer
'
' Returns the low-order word from a 32-bit value.
'
Call MoveMemory(fLowWord, lngValue, 2)
End Function
Public Property Let MaxTipWidth(ByVal lngWidth As Long)
'
' If MaxTipWidth is -1, there is no word wrapping and
' text control characters are printed and not
' evaluated (i.e. a vbCrLf shows up as "||")
'
If mnlgHwndTT = 0 Then Exit Property
If lngWidth < 1 Then lngWidth = -1
Call SendMessageT(mnlgHwndTT, TTM_SETMAXTIPWIDTH, 0, lngWidth)
End Property
Public Property Get ToolCount() As Long
If (mnlgHwndTT = 0) Then Exit Property
ToolCount = SendMessageT(mnlgHwndTT, TTM_GETTOOLCOUNT, 0, 0)
End Property
Public Property Get ToolTipHandle() As Long
ToolTipHandle = mnlgHwndTT
End Property
Public Property Get ToolText(ByRef ctrl As Control) As String
Dim ti As TOOLINFO
If (mnlgHwndTT = 0) Then Exit Property
If fGetToolInfo(ctrl.hWnd, ti, True) Then
ToolText = fGetStrFromBuffer(ti.lpszText)
End If
End Property
Private Function fGetStrFromBuffer(ByVal strValue As String) As String
If InStr(strValue, vbNullChar) Then
fGetStrFromBuffer = Left$(strValue, InStr(strValue, vbNullChar) - 1)
Else
'
' If strValue had no null char, the Left$ function
' above would rtn a zero length string ("").
'
fGetStrFromBuffer = strValue
End If
End Function
Public Property Let ToolText(ByRef ctrl As Control, ByVal strText As String)
Dim ti As TOOLINFO
If (mnlgHwndTT = 0) Then Exit Property
If fGetToolInfo(ctrl.hWnd, ti) Then
ti.lpszText = strText
mnlgMaxTip = fMax(mnlgMaxTip, Len(strText) + 1)
'
' The tooltip won't appear for the control
' if lpszText is an empty string
'
Call SendMessageT(mnlgHwndTT, TTM_UPDATETIPTEXT, 0, ti)
End If
End Property
Private Function fIsWindow(ByRef ctrl As Control) As Boolean
On Error GoTo ErrorHandler
fIsWindow = CBool(ctrl.hWnd)
ErrorHandler:
End Function
Private Function fGetToolInfo(ByVal lnghwndTool As Long, ti As TOOLINFO, _
Optional fGetText As Boolean = False) As Boolean
Dim nItems As Long
Dim i As Integer
ti.cbSize = Len(ti)
If fGetText Then ti.lpszText = String$(mnlgMaxTip, 0)
nItems = ToolCount
For i = 0 To nItems - 1
'
' Returns 1 on success, 0 on failure.
'
If SendMessageT(mnlgHwndTT, TTM_ENUMTOOLS, (i), ti) Then
If (lnghwndTool = ti.uId) Then
fGetToolInfo = True
Exit Function
End If
End If
Next
Private mnlgHwndTT As Long
Private mnlgMaxTip As Long
Public Function Create(ByRef frm As Form) As Boolean
If (mnlgHwndTT = 0) Then
Call InitCommonControls
'
' The hwndParent param lets the tooltip window
' be owned by the specified form and be destroyed
' along with it. We'll cleanup in Class_Terminate anyway.
' No WS_EX_TOPMOST or TTS_ALWAYSTIP per Win95 UI rules.
'
mnlgHwndTT = CreateWindowEx(0, TOOLTIPS_CLASS, vbNullString, TTS_ALWAYSTIP, 0, 0, 0, 0, frm.hWnd, 0, App.hInstance, ByVal 0)
End If
Create = CBool(mnlgHwndTT)
End Function
Private Sub Class_Terminate()
If mnlgHwndTT > 0 Then Call DestroyWindow(mnlgHwndTT)
End Sub
Public Function AddTool(ByRef ctrl As Control, Optional ByVal strText As String) As Boolean
Dim ti As TOOLINFO
If (mnlgHwndTT = 0) Then Exit Function
If (fGetToolInfo(ctrl.hWnd, ti) = False) Then
With ti
.cbSize = Len(ti)
'
' TTF_IDISHWND must be set to tell the tooltip
' control to retrieve the control's rect from
' it's hWnd specified in uId.
'
.uFlags = TTF_SUBCLASS Or TTF_IDISHWND
.hWnd = ctrl.Container.hWnd
.uId = ctrl.hWnd
If Len(strText) > 0 Then
.lpszText = strText
' Else
' .lpszText = "Tool" & ToolCount + 1
End If
'
' Maintain the maximun tip text
' length for fGetToolInfo.
'
mnlgMaxTip = fMax(mnlgMaxTip, Len(.lpszText) + 1)
End With
'
' Returns 1 on success, 0 on failure
'
AddTool = SendMessageT(mnlgHwndTT, TTM_ADDTOOL, 0, ti)
End If
End Function
Private Function fMax(ByVal lngParm1 As Long, ByVal lngParm2 As Long) As Long
'
' Returns the larger of the two values.
'
If lngParm1 > lngParm2 Then
fMax = lngParm1
Else
fMax = lngParm2
End If
End Function
Public Function RemoveTool(ByRef ctrl As Control) As Boolean
Dim ti As TOOLINFO
If (mnlgHwndTT = 0) Then Exit Function
If fGetToolInfo(ctrl.hWnd, ti) Then
Call SendMessageT(mnlgHwndTT, TTM_DELTOOL, 0, ti)
RemoveTool = True
End If
End Function
Public Property Get BackColor() As OLE_COLOR
If (mnlgHwndTT = 0) Then Exit Property
'
' OLE_COLOR is defined in stdole2.tlb
'
BackColor = SendMessageT(mnlgHwndTT, TTM_GETTIPBKCOLOR, 0, 0)
End Property
Public Property Let BackColor(clr As OLE_COLOR)
If (mnlgHwndTT = 0) Then Exit Property
Call SendMessageT(mnlgHwndTT, TTM_SETTIPBKCOLOR, clr, 0)
End Property
Public Property Get DelayTime(dwType As ttDelayTimeConstants) As Long
If (mnlgHwndTT = 0) Then Exit Property
DelayTime = SendMessageT(mnlgHwndTT, TTM_GETDELAYTIME, (dwType And ttDelayMask), 0&)
End Property
Public Property Let DelayTime(dwType As ttDelayTimeConstants, dwMilliSecs As Long)
If (mnlgHwndTT = 0) Then Exit Property
Call SendMessageT(mnlgHwndTT, TTM_SETDELAYTIME, (dwType And ttDelayMask), ByVal dwMilliSecs) ' no rtn val
End Property
Public Property Get ForeColor() As OLE_COLOR
If (mnlgHwndTT = 0) Then Exit Property
ForeColor = SendMessageT(mnlgHwndTT, TTM_SETTIPTEXTCOLOR, 0, 0)
End Property
Public Property Let ForeColor(clr As OLE_COLOR)
If (mnlgHwndTT = 0) Then Exit Property
Call SendMessageT(mnlgHwndTT, TTM_SETTIPTEXTCOLOR, clr, 0) ' no rtn val
End Property
Public Property Get hWnd() As Long
hWnd = mnlgHwndTT
End Property
Public Property Get Margin(dwType As ttMarginConstants) As Long
Dim rc As RECT
Dim mclsToolTip As New clsToolTip
Private Sub Form_Load()
Dim ctrl As Control
With mclsToolTip
'
' Create the tooltip window.
'
Call .Create(Me)
'
' Set the tooltip's width so that it displays
' multiline text and no tool's line length exceeds
' roughly 240 pixels.
'
.MaxTipWidth = 240
'
' Show the tooltip for 20 seconds.
'
.DelayTime(ttDelayShow) = 20000
'
' Add a tooltip tool to each control on the Form.
'
For Each ctrl In Controls
Call .AddTool(ctrl)
Next
'
' Set the text for Command1's tool.
'
.ToolText(Command1) = "This is a long tooltip for a " & vbCrLf & vbCrLf & vbTab & _
"command button that spans multiple lines." & _
vbCrLf & vbCrLf & vbTab & "Text formatting characters can also be used."
'
' Set the text for Command1's tool.
'
.ToolText(Picture1) = "This is a long tooltip for a " & vbCrLf & vbCrLf & vbTab & _
"picturebox that spans multiple lines." & _
vbCrLf & vbCrLf & vbTab & "Text formatting characters can also be used."
End With
End Sub
'module
Option Explicit
'
' The NMHDR structure contains information about
' a notification message. The pointer to this
' structure is specified as the lParam member of
' the WM_NOTIFY message.
'
Public Type NMHDR
hwndFrom As Long
idFrom As Long
code As Long
End Type
Public Type POINTAPI
x As Long
y As Long
End Type
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Const WM_USER = &H400
Public Const TOOLTIPS_CLASS = "tooltips_class32"
Public Const TTS_ALWAYSTIP = &H1
Public Const TTS_NOPREFIX = &H2
#Const WIN32_IE = &H400
Public Type TOOLINFO
cbSize As Long
uFlags As TT_Flags
hWnd As Long
uId As Long
RECT As RECT
hinst As Long
lpszText As String
#If (WIN32_IE >= &H300) Then
lParam As Long
#End If
End Type
Public Enum TT_Flags
TTF_IDISHWND = &H1
TTF_CENTERTIP = &H2
TTF_RTLREADING = &H4
TTF_SUBCLASS = &H10
#If (WIN32_IE >= &H300) Then
TTF_TRACK = &H20
TTF_ABSOLUTE = &H80
TTF_TRANSPARENT = &H100
TTF_DI_SETITEM = &H8000&
#End If
End Enum
Public Enum TT_DelayTime
TTDT_AUTOMATIC = 0
TTDT_RESHOW = 1
TTDT_AUTOPOP = 2
TTDT_INITIAL = 3
End Enum
#If (WIN32_IE >= &H400) Then
TTM_UPDATE = (WM_USER + 29)
#End If
End Enum
Public Enum TT_Notifications
TTN_FIRST = -520&
TTN_LAST = -549&
#If UNICODE Then
TTN_NEEDTEXT = (TTN_FIRST - 10)
#Else
TTN_NEEDTEXT = (TTN_FIRST - 0)
#End If
TTN_SHOW = (TTN_FIRST - 1)
TTN_POP = (TTN_FIRST - 2)
End Enum
Public Type NMTTDISPINFO
hdr As NMHDR
lpszText As Long
#If UNICODE Then
szText As String * 160
#Else
szText As String * 80
#End If
hinst As Long
uFlags As Long
#If (WIN32_IE >= &H300) Then
lParam As Long
#End If
End Type
'
' Exported by Comctl32.dll >= v4.00.950
' Ensures that the common control dynamic
' link library (DLL) is loaded.
'
' NOTE: API replaced by InitCommonControlsEx
Public Declare Sub InitCommonControls Lib "comctl32.dll" ()
Public Declare Function SendMessageT Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Public Declare Function CreateWindowEx Lib "user32" _
Alias "CreateWindowExA" (ByVal dwExStyle As Long, _
ByVal lpClassName As String, ByVal lpWindowName As String, _
ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, _
ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hwndParent As Long, ByVal hMenu As Long, _
ByVal hInstance As Long, lpParam As Any) As Long
Public Declare Function DestroyWindow Lib "user32" _
(ByVal hWnd As Long) As Long
Public Declare Sub MoveMemory Lib "kernel32" _
Alias "RtlMoveMemory" (pDest As Any, pSource As Any, _
ByVal dwLength As Long)
使用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 中调试通过