用VB实现一个气泡提示并不难,即使是实现在任意地方显示的气泡提示。难的是,如果是采用TTF_TRACK方式允许在任意地方显示时,气泡的箭头总是向上,而且还不能自动消失。为此,我写了一个增强的气泡提示类,希望对有此需要的朋友一些参考。
一、新建一个类,类名为clsTip,类代码如下:
代码如下:
Option Explicit
'* ******************************************** *
'* 模块名称:clsTip.cls
'* 功能:一个可以改变箭头方向的气泡提示类
'* 作者:lyserver
'* 联系方式:http://blog.csdn.net/lyserver
'* ******************************************** *
Private Type TOOLINFO
cbSize As Long
dwFlags As Long
hwnd As Long
dwID As Long
rtRect(3) As Long
hInst As Long
lpszText As String
lParam As Long
End Type
Private Declare Sub InitCommonControls Lib " comctl32" ()
Private 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
Private Const TOOLTIPS_CLASS As String = " tooltips_class32"
Private Declare Function DestroyWindow Lib " user32" ( ByVal hwnd As Long) As Long
' ToolTips Style
Public Enum StyleConstants
TTS_COMMON = &H0
TTS_BALLOON = &H40
End Enum
Private Const TTS_ALWAYSTIP As Long = &H1
Private Const TTS_NOANIMATE As Long = &H10
Private Const TTS_NOFADE As Long = &H20
Private Const TTS_NOPREFIX As Long = &H2
Private Const TTDT_AUTOPOP = 2
Private Const TTDT_INITIAL = 3
' ToolTips Flags
Private Const TTF_ABSOLUTE As Long = &H80
Private Const TTF_CENTERTIP As Long = &H2
Private Const TTF_DI_SETITEM As Long = &H8000
Private Const TTF_IDISHWND As Long = &H1
Private Const TTF_RTLREADING As Long = &H4
Private Const TTF_SUBCLASS As Long = &H10
Private Const TTF_TRACK As Long = &H20
Private Const TTF_TRANSPARENT As Long = &H100
' ToolTips Icon
Public Enum IconConstants
TTI_NONE = 0
TTI_INFO = 1
TTI_WARNING = 2
TTI_ERROR = 3
End Enum
'ToolTips Arrow Orientation
Public Enum OrientationConstants
Down = 0
Up = 1
End Enum
' ToolTips Message
Private Const WM_USER As Long = &H400
Private Const TTM_SETDELAYTIME As Long = (WM_USER + 3)
Private Const TTM_ADDTOOL As Long = (WM_USER + 4)
Private
'* ******************************************** *
'* 模块名称:clsTip.cls
'* 功能:一个可以改变箭头方向的气泡提示类
'* 作者:lyserver
'* 联系方式:http://blog.csdn.net/lyserver
'* ******************************************** *
Private Type TOOLINFO
cbSize As Long
dwFlags As Long
hwnd As Long
dwID As Long
rtRect(3) As Long
hInst As Long
lpszText As String
lParam As Long
End Type
Private Declare Sub InitCommonControls Lib " comctl32" ()
Private 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
Private Const TOOLTIPS_CLASS As String = " tooltips_class32"
Private Declare Function DestroyWindow Lib " user32" ( ByVal hwnd As Long) As Long
' ToolTips Style
Public Enum StyleConstants
TTS_COMMON = &H0
TTS_BALLOON = &H40
End Enum
Private Const TTS_ALWAYSTIP As Long = &H1
Private Const TTS_NOANIMATE As Long = &H10
Private Const TTS_NOFADE As Long = &H20
Private Const TTS_NOPREFIX As Long = &H2
Private Const TTDT_AUTOPOP = 2
Private Const TTDT_INITIAL = 3
' ToolTips Flags
Private Const TTF_ABSOLUTE As Long = &H80
Private Const TTF_CENTERTIP As Long = &H2
Private Const TTF_DI_SETITEM As Long = &H8000
Private Const TTF_IDISHWND As Long = &H1
Private Const TTF_RTLREADING As Long = &H4
Private Const TTF_SUBCLASS As Long = &H10
Private Const TTF_TRACK As Long = &H20
Private Const TTF_TRANSPARENT As Long = &H100
' ToolTips Icon
Public Enum IconConstants
TTI_NONE = 0
TTI_INFO = 1
TTI_WARNING = 2
TTI_ERROR = 3
End Enum
'ToolTips Arrow Orientation
Public Enum OrientationConstants
Down = 0
Up = 1
End Enum
' ToolTips Message
Private Const WM_USER As Long = &H400
Private Const TTM_SETDELAYTIME As Long = (WM_USER + 3)
Private Const TTM_ADDTOOL As Long = (WM_USER + 4)
Private