Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Mouse pointer anchored tooltip


  • Please log in to reply
2 replies to this topic
joelpt
  • Members
  • 23 posts
  • Last active: Dec 09 2011 10:05 PM
  • Joined: 24 Jun 2009
Here's a little utility script that will pop up a tooltip anchored to the mouse pointer. As the pointer is moved the tooltip follows it around.

Most users will just need to call Tip("my message") to get the desired result. A duration for the tooltip may also be specified, e.g. Tip("10 second tip", 10000). If not specified, the duration is automatically calibrated to how much text is in the tooltip.

I find this very useful and use it for all kinds of unintrusive messages generated by my various AHK scripts. I use this approach extensively in my Quickfort application for Dwarf Fortress, which uses a mouse anchored tooltip for the app's primary GUI: http://joelpt.net/quickfort.



#Persistent

; update mouse tooltip position this often, in ms
; 10 ms looks the smoothest, but you may prefer a higher value
; if the CPU load is too high with 10 ms
MouseTipUpdateInterval := 10

; a usage example
Tip("This is an example mousetip.")

;; Summon a mouse pointer anchored tooltip.
;; tip: message to show in tooltip
;; duration: how long the tooltip should persist; omit to let Tip() decide based on length of message
Tip(tip, duration = 0)
{
  global Tooltip

  ; Show our tip immediately
  Tooltip := tip
  TurnMouseTipOn()
  ForceMouseTipUpdate()

  ; Set the duration of the tip automatically unless specified
  if (duration == 0)
    duration := 100 * StrLen(Tooltip)

  ; Hide tip after duration
  SetTimer, HideMouseTip, %duration%
}

TurnMouseTipOn()
{
  global MouseTipUpdateInterval

  ; turn mouse tip on
  SetTimer, ShowMouseTip, %MouseTipUpdateInterval%

  ; let the timer tick, so the tip gets updated
  ; right after being turned on; a successive
  ; Send could block the timer otherwise
  Sleep % MouseTipUpdateInterval * 2
}

TurnMouseTipOff()
{
  SetTimer, ShowMouseTip, Off
  SetTimer, HideMouseTip, Off
  ToolTip,
}

ForceMouseTipUpdate()
{
  ForceMouseTipUpdateDelayed()
  SetTimer, ShowMouseTip, 1 ; "undelayed"
}

ForceMouseTipUpdateDelayed()
{
  global LastMouseTipX, LastMouseTipY

  ; this forces the mouse tip to get updated next timer tick
  LastMouseTipX := LastMouseTipY := 0
}

ShowMouseTip:
  SetTimer, ShowMouseTip, %MouseTipUpdateInterval%
  CoordMode Mouse, Relative
  MouseGetPos, xpos, ypos

  if (LastMouseTipMsg != Tooltip || LastMouseTipX != xpos || LastMouseTipY != ypos)
  {
    LastMouseTipMsg := Tooltip
    LastMouseTipX := xpos
    LastMouseTipY := ypos
    tip := Tooltip
    ToolTip, %tip%, xpos + 25, ypos + 10
  }
  return

HideMouseTip:
  TurnMouseTipOff()
  Tooltip := ""
  return


Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
Haven't tested it yet, but it looks like and interesting script.

Did you know the tooltip created with the "tooltip" command will be
displayed near the mouse cursor "If the coordinates are omitted..."

That tooltip will appear to follow the mouse cursor if the tooltip is updated
inside a subroutine (for example: called by settimer)

joelpt
  • Members
  • 23 posts
  • Last active: Dec 09 2011 10:05 PM
  • Joined: 24 Jun 2009
I did not know that. Cool, I'll try that out soon and see if it works as well as the more complicated method above.