Option Explicit
'=====================================================================================================================
'功能:任意位置气泡
'注意:开始的时候Text1的属性IMEMode应该设置为1必须的
'Option Explicit
'Dim tip As New CTips
'Private Sub Command1_Click()
'tip.Style = ttballoon
'tip.Icon = TTIconError '图标类型
'tip.Title = "aaa" '标题
'tip.TipText = "bbb" '内容
'tip.PopupOnDemand = True
'tip.CreateToolTip Text1.hwnd '气泡所在窗口句柄
'tip.Show 'Text1.Width / Screen.TwipsPerPixelX, Text1.Height / Screen.TwipsPerPixelX / 2 - 1 '//In Pixel only
'End Sub
'Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'tip.Destroy
'End Sub
'======================================================================================================================
Private Declare Function CreateWindowEx Lib "user32.dll" 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, ByRef lpParam As Any) As Long
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Type TOOLINFO
lSize As Long
lFlags As Long
hwnd As Long
lId As Long
lpRect As RECT
hInstance As Long
lpStr As String
lParam As Long
End Type
Public Enum ttIconType
TTNoIcon = 0
TTIconInfo = 1
TTIconWarning = 2
TTIconError = 3
End Enum
Public Enum ttStyleEnum
TTStandard
ttballoon
End Enum
Private mvarBackColor As Long
Private mvarTitle As String
Private mvarForeColor As Long
Private mvarIcon As ttIconType
Private mvarCentered As Boolean
Private mvarStyle As ttStyleEnum
Private mvarTipText As String
Private mvarVisibleTime As Long
Private mvarDelayTime As Long
Private mvarPopupOnDemand As Boolean
Private m_lTTHwnd As Long
Private m_lParentHwnd As Long
Private ti As TOOLINFO
Private Sub Class_Initialize()
mvarDelayTime = 500
mvarVisibleTime = 5000
mvarPopupOnDemand = False
End Sub
Private Sub Class_Terminate()
Destroy
End Sub
Public Property Get VisibleTime() As Long
VisibleTime = mvarVisibleTime
End Property
Public Property Let VisibleTime(ByVal lData As Long)
mvarVisibleTime = lData
End Property
Public Property Get DelayTime() As Long
DelayTime = mvarDelayTime
End Property
Public Property Let DelayTime(ByVal lData As Long)
mvarDelayTime = lData
End Property
Public Property Let Icon(ByVal vData As ttIconType)
mvarIcon = vData
If m_lTTHwnd <> 0 And mvarTitle <> Empty And mvarIcon <> TTNoIcon Then
SendMessage m_lTTHwnd, TTM_SETTITLE, CLng(mvarIcon), ByVal mvarTitle
End If
End Property
Public Property Get Icon() As ttIconType
Icon = mvarIcon
End Property
Public Property Let ForeColor(ByVal vData As Long)
mvarForeColor = vData
If m_lTTHwnd <> 0 Then
SendMessage m_lTTHwnd, TTM_SETTIPTEXTCOLOR, mvarForeColor, 0&
End If
End Property
Public Property Get ForeColor() As Long
ForeColor = mvarForeColor
End Property
Public Property Let Title(ByVal vData As String)
mvarTitle = vData
If m_lTTHwnd <> 0 And mvarTitle <> Empty And mvarIcon <> TTNoIcon Then
SendMessage m_lTTHwnd, TTM_SETTITLE, CLng(mvarIcon), ByVal mvarTitle
End If
End Property
Public Property Get Title() As String '修改了下
'Title = ti.lpStr
Title = mvarTitle
End Property
Public Property Let PopupOnDemand(ByVal vData As Boolean)
mvarPopupOnDemand = vData
End Property
Public Property Get PopupOnDemand() As Boolean
PopupOnDemand = mvarPopupOnDemand
End Property
Public Property Let BackColor(ByVal vData As Long)
mvarBackColor = vData
If m_lTTHwnd <> 0 Then
SendMessage m_lTTHwnd, TTM_SETTIPBKCOLOR, mvarBackColor, 0&
End If
End Property
Public Property Get BackColor() As Long
BackColor = mvarBackColor
End Property
Public Property Let TipText(ByVal vData As String)
mvarTipText = vData
ti.lpStr = vData
If m_lTTHwnd <> 0 Then
SendMessage m_lTTHwnd, TTM_UPDATETIPTEXTA, 0&, ti
End If
End Property
Public Property Get TipText() As String
TipText = mvarTipText
End Property
Public Property Let Style(ByVal vData As ttStyleEnum)
mvarStyle = vData
End Property
Public Property Get Style() As ttStyleEnum
Style = mvarStyle
End Property
Public Property Let Centered(ByVal vData As Boolean)
mvarCentered = vData
End Property
Public Property Get Centered() As Boolean
Centered = mvarCentered
End Property
'显示出来
Public Sub Show(Optional x As Long = 0, Optional Y As Long = 0)
Dim pt As POINTAPI
Dim ptTip As Long
Dim ret As Long
With pt
.x = x
.Y = Y
End With
ret = ClientToScreen(m_lParentHwnd, pt)
ptTip = pt.Y * &H10000
ptTip = ptTip + pt.x
ret = SendMessage(m_lTTHwnd, TTM_TRACKPOSITION, 0, ByVal ptTip)
ret = SendMessage(m_lTTHwnd, TTM_TRACKACTIVATE, True, ti)
End Sub
'创建对象
Public Function CreateToolTip(ByVal ParentHwnd As Long) As Boolean
Dim lWinStyle As Long
If m_lTTHwnd <> 0 Then
DestroyWindow m_lTTHwnd
End If
m_lParentHwnd = ParentHwnd
If mvarStyle = ttballoon Then lWinStyle = lWinStyle Or TTS_BALLOON
m_lTTHwnd = CreateWindowEx(0&, TOOLTIPS_CLASSA, vbNullString, lWinStyle, 0&, 0&, 0&, 0&, m_lParentHwnd, 0&, 0&, 0&)
With ti
If mvarCentered Then
If mvarPopupOnDemand = False Then
.lFlags = TTF_SUBCLASS Or TTF_CENTERTIP Or TTF_IDISHWND
Else
.lFlags = TTF_IDISHWND Or TTF_TRACK Or TTF_CENTERTIP
End If
Else
If mvarPopupOnDemand = False Then
.lFlags = TTF_SUBCLASS Or TTF_IDISHWND
Else
.lFlags = TTF_IDISHWND Or TTF_TRACK Or TTF_TRANSPARENT
End If
End If
.hwnd = m_lParentHwnd
.lId = m_lParentHwnd
.hInstance = App.hInstance
.lSize = Len(ti)
End With
SendMessage m_lTTHwnd, TTM_ADDTOOLA, 0&, ti
If mvarTitle <> vbNullString Or mvarIcon <> TTNoIcon Then
SendMessage m_lTTHwnd, TTM_SETTITLE, CLng(mvarIcon), ByVal mvarTitle
End If
If mvarForeColor <> Empty Then
SendMessage m_lTTHwnd, TTM_SETTIPTEXTCOLOR, mvarForeColor, 0&
End If
If mvarBackColor <> Empty Then
SendMessage m_lTTHwnd, TTM_SETTIPBKCOLOR, mvarBackColor, 0&
End If
SendMessageLong m_lTTHwnd, TTM_SETDELAYTIME, TTDT_AUTOPOP, mvarVisibleTime
SendMessageLong m_lTTHwnd, TTM_SETDELAYTIME, TTDT_INITIAL, mvarDelayTime
End Function
'销毁对象
Public Sub Destroy()
If m_lTTHwnd <> 0 Then
DestroyWindow m_lTTHwnd
End If
End Sub