Displaying a tooltip on demand (LBB)

by Richard Russell, August 2012

As a rule, a tooltip is displayed when the mouse moves into the tool's rectangle and stays there for a specified minimum period of time; typically this is when the user 'hovers' the mouse over a control or other region of interest. However it is also possible to force a tooltip to display 'on demand', under the control of your program.

The code listing below demonstrates how to do that:

      nomainwin
 
      TTM.ADDTOOL = 1028
      TTM.UPDATETIPTEXT = 1036
      TTM.TRACKACTIVATE = 1041
      TTS.ALWAYSTIP = 1
      TTS.NOPREFIX = 2
      TTS.BALLOON = 64
 
      button #w.b1, "Click me", [activate], UL, 120, 120
      open "Tooltip on demand" for window as #w
      #w "trapclose [quit]"
      hw = hwnd(#w)
 
      style = _WS_POPUP or TTS.NOPREFIX or TTS.ALWAYSTIP or TTS.BALLOON
      calldll #user32, "CreateWindowExA", _WS_EX_TOPMOST as long, _
          "tooltips_class32" as ptr, 0 as long, style as long, _
          _CW_USEDEFAULT as long, _CW_USEDEFAULT as long, _
          _CW_USEDEFAULT as long, _CW_USEDEFAULT as long, _
          hw as ulong, 0 as long, 0 as long, 0 as long, _
          hwndTT as long
 
      struct ti, cbSize as ulong, uFlags as ulong, _
          hwnd as ulong, uID as ulong, rectl as long, _
          rectt as long, rectr as long, rectb as long, _
          hinst as ulong, lpszText as ptr
 
      ti.cbSize.struct = LEN(ti.struct)
      ti.hwnd.struct = hw
      ti.lpszText.struct = ""
 
      calldll #user32, "SendMessageA", hwndTT as ulong, _
          TTM.ADDTOOL as long, 0 as long, ti as struct, _
          retval as long
      wait
 
  [activate]
      ti.lpszText.struct = "Hello tooltip!"
      calldll #user32, "SendMessageA", hwndTT as ulong, _
          TTM.UPDATETIPTEXT as long, 0 as long, ti as struct, _
          retval as long
      calldll #user32, "SendMessageA", hwndTT as ulong, _
          TTM.TRACKACTIVATE as long, 1 as long, ti as struct, _
          retval as long
      timer 2000, [deactivate]
      wait
 
  [deactivate]
      timer 0
      ti.lpszText.struct = ""
      calldll #user32, "SendMessageA", hwndTT as ulong, _
          TTM.UPDATETIPTEXT as long, 0 as long, ti as struct, _
          retval as long
      wait
 
  [quit]
      close #w
      end

As shown, clicking the button will cause a balloon tooltip (available in Windows XP or later) to be displayed at the mouse position, and to remain displayed for two seconds. If the TTS_BALLOON style is omitted a regular tooltip will be displayed.