001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 |
|
Option Explicit
Public oButton() As New clsCommandButton
Private Declare PtrSafe Function SetTimer Lib "user32" ( _
ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr, _
ByVal uElapse As Long, ByVal lpTimerFunc As LongPtr) As LongPtr
Private Declare PtrSafe Function KillTimer Lib "user32" ( _
ByVal hwnd As LongPtr, ByVal nIDEvent As LongPtr) As Long
Private Declare PtrSafe Function GetCursorPos Lib "user32" ( _
lpPoint As POINTAPI) As Long
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Type POINTAPI
X As Long
Y As Long
End Type
Dim Pt As POINTAPI
Private hTimer As LongPtr, oCurObj As Object
Dim msOldBtnText As String
Dim miDelayCount As Long
Private Const ciDelayTime As Long = 50 ' Hier eine Verzögerung einstellen
Sub Tooltip_Initiate(WSh As Worksheet)
' Einlesen der vorhandenen Buttons in das Button-Array
' Nur für die Klassenprogrammierung nötig
Dim oOleObj As OLEObject, i As Integer
For Each oOleObj In WSh.OLEObjects
If TypeOf oOleObj.Object Is MSForms.CommandButton Then
i = i + 1
ReDim Preserve oButton(i)
Set oButton(i).moCommandButton = oOleObj.Object
End If
Next oOleObj
End Sub
Sub Tooltip_Create(oButton As Object, X As Single, Y As Single)
' Hier das Objekt formatieren
Dim sText As String, B As Integer, H As Integer, L As Currency
Dim sArr() As String, i As Integer, j As Integer, iBMax As Long
Dim T As String
If hTimer <> 0 Then Exit Sub ' Timer läuft noch
On Error GoTo Fehler
With oButton
msOldBtnText = .Name
Select Case .Name
' ##### Hier die Vorgabe der Tooltiptexte #####
' ¶ = CHR$(182) = Umbruchplatzhalter
' iBMax = Vorgabe der Textboxbreite, wenn 0, dann automatische Ermittlung
Case "CommandButton1": sText = "Dieses ist mein erster Tooltip!": iBMax = 122
Case "CommandButton2": sText = "Und hier¶machen wir einen Umbruch mit rein!"
Case "CommandButton3": sText = "1. Testen¶2. Testen¶3. Testen"
Case "CheckBox1": sText = "Für weitere Informationen hier klicken!": iBMax = 156
' #############################################
Case Else: Exit Sub
End Select
sText = Replace(sText, "¶", vbLf) ' Textumbrüche setzen
sArr = Split(sText, vbLf)
For i = 0 To UBound(sArr)
If iBMax = 0 Then
L = 0
For j = 1 To Len(sArr(i)) ' Textbreite ermitteln
T = Mid$(sArr(i), j, 1)
L = L + 2.75
If InStr(1, Chr$(34) & " !/()\''|,;.:1ijl", T, vbTextCompare) = 0 Then L = L + 2.5
If InStr(1, Chr$(34) & "wm_", T, vbTextCompare) > 0 Then L = L + 0.75
If Asc(T) > 64 And Asc(T) < 97 Then L = L + 1.5
Next j
If L > B Then B = L ' Textboxlänge ermitteln
End If
H = H + 12
Next i
If iBMax > 0 Then B = iBMax ' Feste Breitenvorgabe
Call ToolTip_Delete(.Parent) ' Evtl. vorhandene Tooltipbox löschen
Y = .Top + .Height + 2 + (Y \ 2)
X = .Left + X
With .Parent.Shapes.AddTextbox(1, X, Y, B, H)
.Name = "ToolTip"
.Visible = msoFalse
miDelayCount = 0
With .TextFrame2.TextRange.Characters
.Font.Size = 9
.Font.Name = "Arial"
.Text = sText
End With
With .Fill
.ForeColor.RGB = RGB(255, 255, 210) ' Hintergrundfarbe setzen
.Transparency = 0
.Solid
End With
With .TextFrame2
.AutoSize = msoAutoSizeShapeToFitText ' Textboxgröße automatisch
.MarginLeft = 1.5: .MarginTop = 1.5 ' Randabstände
.MarginBottom = 1.5: .MarginRight = 1.5
End With
End With
End With
hTimer = SetTimer(0&, 0&, 10, AddressOf Timer_Tick) ' Timer setzen für nächsten Check
Fehler:
End Sub
Sub Timer_Tick()
miDelayCount = miDelayCount + 1
DoEvents
GetCursorPos Pt ' Mausposition holen
On Error Resume Next
Set oCurObj = Application.Windows(1).RangeFromPoint(Pt.X, Pt.Y)
If Err <> 0 Then Err.Clear: Exit Sub ' Fehler => raus
If TypeName(oCurObj) = "OLEObject" Then
oCurObj.Parent.Shapes.Range("ToolTip").Visible = (miDelayCount > ciDelayTime)
If msOldBtnText <> oCurObj.Name Then
Call ToolTip_Delete(oCurObj.Parent) ' Textbox löschen
Call Tooltip_Create(oCurObj, oCurObj.X, oCurObj.Y)
End If
Else
Call ToolTip_Delete(ActiveSheet) ' Textbox löschen
End If
End Sub
Sub ToolTip_Delete(WSh As Worksheet)
If hTimer <> 0 Then KillTimer 0&, hTimer: hTimer = 0 ' Timer löschen
On Error Resume Next
WSh.Shapes.Range("ToolTip").Delete ' Evtl. vorhandene Textbox löschen
End Sub
|