Option Explicit
Implements IToolTip
Private WithEvents cmbrs As CommandBars
Private WithEvents wb As Workbook
Private Type POINTAPI
x As Long
y As Long
End Type
#If VBA7 Then
Private Declare PtrSafe Function GetActiveWindow Lib "user32" () As LongPtr
Private Declare PtrSafe Function GetCursorPos Lib "user32.DLL" (lpPoint As POINTAPI) As Long
Private Declare PtrSafe Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
#Else
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetCursorPos Lib "user32.DLL" (lpPoint As POINTAPI) As Long
Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
#End If
Private oShape As Shape
Private sngLeft As Single
Private sngTop As Single
Private sngWidth As Single
Private sngHeight As Single
Private sText As String
Private lTextColor As Long
Private lBackColor As Long
Private bAutoSize As Boolean
Private Sub IToolTip_Shape(ByVal shp As Shape)
Set oShape = shp
End Sub
Private Property Let IToolTip_AutoSize(ByVal RHS As Boolean)
bAutoSize = RHS
End Property
Private Property Let IToolTip_Left(ByVal RHS As Single)
sngLeft = RHS
End Property
Private Property Let IToolTip_Top(ByVal RHS As Single)
sngTop = RHS
End Property
Private Property Let IToolTip_Width(ByVal RHS As Single)
sngWidth = RHS
End Property
Private Property Let IToolTip_Height(ByVal RHS As Single)
sngHeight = RHS
End Property
Private Property Let IToolTip_ToolTipBackColor(ByVal RHS As Long)
lBackColor = RHS
End Property
Private Property Let IToolTip_ToolTipTextColor(ByVal RHS As Long)
lTextColor = RHS
End Property
Private Property Let IToolTip_ToolTipText(ByVal RHS As String)
sText = RHS
End Property
Private Sub IToolTip_Hook()
Dim i As Long, lCounter As Long
oShape.AlternativeText = oShape.Name & "||" & sngLeft & "||" & sngTop & _
"||" & sngWidth & "||" & sngHeight & "||" & sText & "||" & lTextColor & "||" & lBackColor
For i = 0 To VBA.UserForms.Count - 1
If VBA.UserForms(i).Name = "DummyForm" Then
lCounter = lCounter + 1
If lCounter > 1 Then Exit Sub
End If
Next i
Set cmbrs = Application.CommandBars
Call cmbrs_OnUpdate
End Sub
Private Sub cmbrs_OnUpdate()
Const COLOR_INFOBK = 24
Dim oObj As Object
Dim oToolTip As Shape
Dim tCurs As POINTAPI
Dim vAttributes As Variant
On Error Resume Next
If GetActiveWindow = Application.hwnd And ThisWorkbook Is ActiveWorkbook Then
Call GetCursorPos(tCurs)
Set oObj = ActiveWindow.RangeFromPoint(tCurs.x, tCurs.y)
If InStr(1, "RangeNothing", TypeName(oObj)) = 0 Then
If InStr(1, oObj.ShapeRange.AlternativeText, "||") Then
vAttributes = Split(oObj.ShapeRange.AlternativeText, "||")
oObj.Parent.Shapes("ToolTip").Delete
If vAttributes(1) = 0 Then vAttributes(1) = oObj.Width / 2 + oObj.Left
If vAttributes(2) = 0 Then vAttributes(2) = oObj.Height + oObj.Top + 10
If vAttributes(3) = 0 Then vAttributes(3) = 100
If vAttributes(4) = 0 Then vAttributes(4) = 25
Set oToolTip = oObj.Parent.Shapes.AddTextbox(msoTextOrientationHorizontal, vAttributes(1), vAttributes(2), vAttributes(3), vAttributes(4))
With oToolTip
.Name = "ToolTip"
If bAutoSize Then
.TextFrame2.AutoSize = msoAutoSizeShapeToFitText
End If
.TextFrame.Characters.Text = vAttributes(5)
.TextFrame2.TextRange.Characters.Font.Fill.ForeColor.RGB = vAttributes(6)
If vAttributes(7) = 0 Then
vAttributes(7) = GetSysColor(COLOR_INFOBK)
End If
.Fill.ForeColor.RGB = vAttributes(7)
End With
Else
oObj.Parent.Shapes("ToolTip").Delete
End If
Else
oObj.Parent.Shapes("ToolTip").Delete
End If
With Application.CommandBars.FindControl(ID:=2040)
.Enabled = Not .Enabled
End With
End If
End Sub
Private Sub wb_BeforeClose(Cancel As Boolean)
Set cmbrs = Nothing
End Sub