VB中给listview的item添加多行气泡式Tooltip

原创 2004年01月07日 11:24:00

一个类模块,命名为:CTooltip,代码如下:

Option Explicit

Private Declare Sub InitCommonControls Lib "comctl32.dll" ()

''Windows API Functions
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 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 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

''Windows API Constants
Private Const WM_USER = &H400
Private Const CW_USEDEFAULT = &H80000000

''Windows API Types
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

''Tooltip Window Constants
Private Const TTS_NOPREFIX = &H2
Private Const TTF_TRANSPARENT = &H100
Private Const TTF_CENTERTIP = &H2
Private Const TTM_ADDTOOLA = (WM_USER + 4)
Private Const TTM_ACTIVATE = WM_USER + 1
Private Const TTM_UPDATETIPTEXTA = (WM_USER + 12)
Private Const TTM_SETMAXTIPWIDTH = (WM_USER + 24)
Private Const TTM_SETTIPBKCOLOR = (WM_USER + 19)
Private Const TTM_SETTIPTEXTCOLOR = (WM_USER + 20)
Private Const TTM_SETTITLE = (WM_USER + 32)
Private Const TTS_BALLOON = &H40
Private Const TTS_ALWAYSTIP = &H1
Private Const TTF_SUBCLASS = &H10
Private Const TTF_IDISHWND = &H1
Private Const TTM_SETDELAYTIME = (WM_USER + 3)
Private Const TTDT_AUTOPOP = 2
Private Const TTDT_INITIAL = 3

Private Const TOOLTIPS_CLASSA = "tooltips_class32"

''Tooltip Window Types
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

'local variable(s) to hold property value(s)
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 data
Private m_lTTHwnd As Long ' hwnd of the tooltip
Private m_lParentHwnd As Long ' hwnd of the window the tooltip attached to
Private ti As TOOLINFO

Public Property Let Style(ByVal vData As ttStyleEnum)
   'used when assigning a value to the property, on the left side of an assignment.
   'Syntax: X.Style = 5
   mvarStyle = vData
End Property

Public Property Get Style() As ttStyleEnum
   'used when retrieving value of a property, on the right side of an assignment.
   'Syntax: Debug.Print X.Style
   Style = mvarStyle
End Property

Public Property Let Centered(ByVal vData As Boolean)
   'used when assigning a value to the property, on the left side of an assignment.
   'Syntax: X.Centered = 5
   mvarCentered = vData
End Property

Public Property Get Centered() As Boolean
   'used when retrieving value of a property, on the right side of an assignment.
   'Syntax: Debug.Print X.Centered
   Centered = mvarCentered
End Property

Public Function Create(ByVal ParentHwnd As Long) As Boolean
   Dim lWinStyle As Long
  
   If m_lTTHwnd <> 0 Then
      DestroyWindow m_lTTHwnd
   End If
  
   m_lParentHwnd = ParentHwnd
  
   lWinStyle = TTS_ALWAYSTIP Or TTS_NOPREFIX
  
   ''create baloon style if desired
   If mvarStyle = TTBalloon Then lWinStyle = lWinStyle Or TTS_BALLOON
   
   m_lTTHwnd = CreateWindowEx(0&, _
      TOOLTIPS_CLASSA, _
      vbNullString, _
      lWinStyle, _
      CW_USEDEFAULT, _
      CW_USEDEFAULT, _
      CW_USEDEFAULT, _
      CW_USEDEFAULT, _
      0&, _
      0&, _
      App.hInstance, _
      0&)
              
   ''now set our tooltip info structure
   With ti
      ''if we want it centered, then set that flag
      If mvarCentered Then
         .lFlags = TTF_SUBCLASS Or TTF_CENTERTIP Or TTF_IDISHWND
      Else
         .lFlags = TTF_SUBCLASS Or TTF_IDISHWND
      End If
      
      ''set the hwnd prop to our parent control's hwnd
      .hwnd = m_lParentHwnd
      .lId = m_lParentHwnd '0
      .hInstance = App.hInstance
      '.lpstr = ALREADY SET
      '.lpRect = lpRect
      .lSize = Len(ti)
   End With
  
   ''add the tooltip structure
   SendMessage m_lTTHwnd, TTM_ADDTOOLA, 0&, ti

   ''if we want a title or we want an icon
   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 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
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

Private Sub Class_Initialize()
   InitCommonControls
   mvarDelayTime = 500
   mvarVisibleTime = 5000
End Sub

Private Sub Class_Terminate()
   Destroy
End Sub

Public Sub Destroy()
   If m_lTTHwnd <> 0 Then
      DestroyWindow m_lTTHwnd
   End If
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

一个窗体,窗体上一个listview控件,代码如下:

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

Const LVM_FIRST = &H1000&
Const LVM_HITTEST = LVM_FIRST + 18

Private Type POINTAPI
    X As Long
    Y As Long
End Type

Private Type LVHITTESTINFO
   pt As POINTAPI
   flags As Long
   iItem As Long
   iSubItem As Long
End Type

Dim TT As CTooltip
Dim m_lCurItemIndex As Long

Private Sub Form_Load()
   With ListView1.ListItems
      .Add Text:="Test item #1"
      .Add Text:="Test item #2"
      .Add Text:="Long long long test item #3"
   End With

   Set TT = New CTooltip
   TT.Style = TTBalloon
   TT.Icon = TTIconInfo
End Sub

Private Sub ListView1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
   Dim lvhti As LVHITTESTINFO
   Dim lItemIndex As Long
  
   lvhti.pt.X = X / Screen.TwipsPerPixelX
   lvhti.pt.Y = Y / Screen.TwipsPerPixelY
   lItemIndex = SendMessage(ListView1.hwnd, LVM_HITTEST, 0, lvhti) + 1
  
   If m_lCurItemIndex <> lItemIndex Then
      m_lCurItemIndex = lItemIndex
      If m_lCurItemIndex = 0 Then   ' no item under the mouse pointer
         TT.Destroy
      Else
         TT.Title = "Multiline tooltip"
         TT.TipText = ListView1.ListItems(m_lCurItemIndex).Text
         TT.Create ListView1.hwnd
      End If
   End If
End Sub

VB中给listview的item添加多行气泡式Tooltip

一个类模块,命名为:CTooltip,代码如下:Option ExplicitPrivate Declare Sub InitCommonControls Lib "comctl32.dll" ()W...
  • lang_csdn
  • lang_csdn
  • 2004年10月24日 13:44
  • 716

VB中ToolTip工具提示的完美实现

我经常在一些软件(如Windows优化大师)上看到一些比较酷的ToolTip工具提示框,感觉不错也挺新鲜。我现在用VB做了一个,希望对用VB开发软件的朋友能有所帮助。若程序中有不完善的地方,请大家提出...
  • Dengyu1997
  • Dengyu1997
  • 2006年03月06日 13:35
  • 3452

listview 中设置tooltip 较好的解决方法

private ToolTip tip = new ToolTip();                private void FirstWindow_Load(object sender, Eve...
  • chenep
  • chenep
  • 2012年08月06日 20:50
  • 728

VB.NET为每一个控件创建气泡提示

在NET中如果是ToolStrip1工具栏,默认的气泡提示即是该按钮的text属性值。 如果是其他控件呢?不如说TextBox控件,以下代码可实现:              Dim t...
  • wl58796351
  • wl58796351
  • 2011年09月11日 18:18
  • 1190

Android为listview的item添加动画效果

Android为ViewGroup的item添加动画效果
  • reuxfhc
  • reuxfhc
  • 2015年04月30日 11:46
  • 722

Winform中ListView鼠标移动使用toolTip显示信息

首先设置Listview的MouseMove事件   1.获取当前坐标的项  ListViewItem lvi = this.listView.GetItemAt(e.X, e.Y)...
  • windowsliusheng
  • windowsliusheng
  • 2016年02月02日 12:01
  • 1254

【Android】Listview可以选择多行,点击选中行item字体颜色变化

界面是一个listview,初始字体均为红色,点击一行选中时为字体颜色为绿色,再次点击取消选中变为红色,可以同时选择多行记录  页面布局文件: ...
  • hellokittylin
  • hellokittylin
  • 2012年10月24日 19:20
  • 1696

到底应该如何在VB的ListView中动态加载数据库中的大量记录?

在使用VB6.0访问数据库时,经常会遇到记录数量过多,对数据列表进行初始化时耗费大量时间,如何在初始化时,只是先加载少量的数据,使控件快速完成初始化.然后当按垂直滚动条的时间再加载更多的其它内容,但是...
  • mjc1982
  • mjc1982
  • 2006年06月10日 14:09
  • 3869

为ListView子视图添加动画

看书的时候,看到给viewgroup子控件添加动画,所以就稍稍研究了一下。拿listview来说,当我们需要给listview中的item添加动画效果的时候,就需要用到 LayoutAnimation...
  • Langxingtianxi
  • Langxingtianxi
  • 2015年11月13日 16:46
  • 646

在ListView的Item之间添加分割线

前言: 作为一名程序媛,并且是刚入职的小菜鸟,深知自己能力和天赋有限,因此开启这个技术博客,希望能够形成一个技术知识的积累,希望自己能够成为一名优秀的,高效的,不需要加班的程序员! ...
  • u013708985
  • u013708985
  • 2015年08月17日 09:50
  • 2097
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:VB中给listview的item添加多行气泡式Tooltip
举报原因:
原因补充:

(最多只允许输入30个字)