Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


Using api you can create ballon style tooltip. You can also set varous parameter for tooltip i.e. Delay time, Style, Color, font etc...

I have used IFont to get font handle from StdFont but IFont may create problem sometimes. Check the following article to convert StdFont to APIFont

http://www.vbaccelerator.com/home/VB/Tips/Create_an_API_hFont_from_a_VB_StdFont_object/article.asp


To run this demo perform the following steps
- Create a standard exe project
- Add a class module rename class1 to CBalloonToolTip
- Add 2 textbox and one command button on form1
- Place Following code in form1

Form1.frm

Click here to copy the following block
Dim TT1 As New CBalloonToolTip  '//Demo for On Demand tooltip
Dim TT2 As New CBalloonToolTip  '//Demo for mouse over tooltip

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Public Function CapsLockOn() As Boolean
  Dim iKeyState As Integer
  iKeyState = GetKeyState(vbKeyCapital)
  CapsLockOn = (iKeyState = 1 Or iKeyState = -127)
End Function

Private Sub Command1_Click()
  If Text1.Text <> "mypassword" Then
    TT1.Style = TTBalloon
    TT1.Icon = TTIconError
    TT1.Title = IIf(Text1.Text = "", "Blank Password", "Invalid Password")
    TT1.TipText = "Please Enter Correct Password..."
    TT1.PopupOnDemand = True
    TT1.CreateToolTip Text1.hwnd
    TT1.Show 0, Text1.Height / Screen.TwipsPerPixelX - 1, Me.Text1.hwnd  '//In Pixel only
  End If
End Sub

Private Sub Form_Load()
  TT2.Style = TTBalloon
  TT2.Icon = TTIconInfo
  TT2.Title = "Information"
  TT2.TipText = "tooltip with cool font & color for Textbox2"
  TT2.PopupOnDemand = False
  TT2.ForeColor = vbWhite
  TT2.BackColor = &HCE7110

  '//Tooltip font setting
  Dim f As New StdFont
  f.Name = "Verdana"
  f.Underline = True
  TT2.TipFont = f

  TT2.VisibleTime = 6000  'After 6 Seconds tooltip will go away
  TT2.CreateToolTip Text2.hwnd
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  TT1.Destroy
End Sub

Private Sub Form_Paint()
  TT1.Destroy
End Sub

Private Sub Form_Resize()
  TT1.Destroy
End Sub
Private Sub Form_Unload(Cancel As Integer)
  TT1.Destroy
  TT2.Destroy
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
  If CapsLockOn Then
    TT1.Style = TTBalloon
    TT1.Icon = TTIconWarning
    TT1.Title = "Caps Lock is on"
    TT1.TipText = "Check your caps lock key..."
    TT1.CreateToolTip Text1.hwnd
    TT1.Show 0, Text1.Height / Screen.TwipsPerPixelX - 1, Me.Text1.hwnd
  Else
    TT1.Destroy
  End If
End Sub

Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  TT1.Destroy
End Sub

Add the following code in class module

CBalloonToolTip.cls

Click here to copy the following block
Option Explicit

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

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

''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

Private Type POINTAPI
  X As Long
  Y As Long
End Type

Private Const WM_SETFONT = &H30

'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_TRACK = &H20
Private Const TTF_IDISHWND = &H1
Private Const TTM_SETDELAYTIME = (WM_USER + 3)
Private Const TTDT_AUTOPOP = 2
Private Const TTDT_INITIAL = 3
Private Const TTM_TRACKACTIVATE = WM_USER + 17
Private Const TTM_TRACKPOSITION = WM_USER + 18
Private Const WS_POPUP = &H80000000

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 mvarPopupOnDemand As Boolean

'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
Private mvarFont As IFont

Private Sub Class_Initialize()
  InitCommonControls
  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 TipFont(ByVal vData As StdFont)
  Set mvarFont = vData
  If m_lTTHwnd <> 0 Then
    SendMessage m_lTTHwnd, WM_SETFONT, mvarFont.hFont, 1
  End If
End Property
Public Property Get TipFont() As StdFont
  Set TipFont = mvarFont
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 PopupOnDemand(ByVal vData As Boolean)
  mvarPopupOnDemand = vData
  'If m_lTTHwnd <> 0 Then
  'End If
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

'X and Y are in Pixel so dont send vbTwips value
Public Sub Show(Optional X As Long = 0, Optional Y As Long = 0, Optional hWndClient 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(hWndClient, pt)

  ptTip = pt.Y * &H10000
  ptTip = ptTip + pt.X

  ' These two messages will set the position of the tooltip:
  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

  ''create baloon style if desired
  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&)

  ''now set our tooltip info structure
  With ti
    '//dont incude TTF_SUBCLASS for on demand
    ''if we want it centered, then set that flag
    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

    '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

  If Not (mvarFont Is Nothing) Then
    SendMessage m_lTTHwnd, WM_SETFONT, mvarFont.hFont, 1&
  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


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.