ToolTip – Center the tip at the x,y coordinates instead of using them for the upper left corner? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rdllngr
Posts: 54
Joined: 22 Sep 2017, 09:39

ToolTip – Center the tip at the x,y coordinates instead of using them for the upper left corner?

14 May 2018, 08:53

Does anyone know a way to position the reference point location to the center, rather than the top left corner of ToolTip?

Below is a code just to illustrate. When you activate the ToolTip, it takes as reference the upper left corner as the start of the coordinates (X0, Y0).

Is it possible to change this reference point to its center?

Code: Select all

#SingleInstance Force

a:: ; Hold this Hotkey to follow the cursor
MouseGetPos, xpos, ypos
ToolTip, Hello!, xpos, ypos
return

x::
ExitApp
return

Qysh
Posts: 143
Joined: 24 Apr 2018, 09:16

Re: ToolTip – Center the tip at the x,y coordinates instead of using them for the upper left corner?  Topic is solved

14 May 2018, 09:36

It is not perfect but you can give it a try ;)

Code: Select all

#NoEnv
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#MaxThreadsPerHotkey 255
#KeyHistory 0
SetWorkingDir %A_ScriptDir%
SendMode Input

tooltip_text := "Hello!"

*a:: ; Hold this Hotkey to follow the cursor
while(GetKeyState("a", "p"))
{
	MouseGetPos, xpos, ypos
	length := StrLen(tooltip_text)
	xpos := xpos - ((length / 2) * 8)
	ypos := ypos - 20
	ToolTip, %tooltip_text%, %xpos%, %ypos%
}
ToolTip
return
Same as the first one but i fixed the flickering while not moving

Code: Select all

#NoEnv
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#MaxThreadsPerHotkey 255
#KeyHistory 0
SetWorkingDir %A_ScriptDir%
SendMode Input

tooltip_text := "Hello!"

*a:: ; Hold this Hotkey to follow the cursor
xpos_old := 0
xpos := 0
length := StrLen(tooltip_text)
while(GetKeyState("a", "p"))
{
	MouseGetPos, xpos, ypos
	if(xpos != xpos_old or ypos != ypos_old)
	{
		xpos_d := xpos - (length * 4)
		ypos_d := ypos - 20
		ToolTip, %tooltip_text%, %xpos_d%, %ypos_d%	
	}
	xpos_old := xpos
	ypos_old := ypos
}
ToolTip
return
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: ToolTip – Center the tip at the x,y coordinates instead of using them for the upper left corner?

14 May 2018, 10:22

Code: Select all

ToolTip, % "SampleTextSampleTextSampleTextSampleTextSampleTextSampleTextSampleText`n`n`nSampleTextSample"
toolHwnd := "ahk_id" . WinExist("ahk_class tooltips_class32")
WinGetPos, x, y, w, h, % toolHwnd
toolX := (A_ScreenWidth - w) // 2
toolY := (A_ScreenHeight - h) // 2
WinMove, % toolHwnd, , % toolX, % toolY 

Esc::ExitApp
rdllngr
Posts: 54
Joined: 22 Sep 2017, 09:39

Re: ToolTip – Center the tip at the x,y coordinates instead of using them for the upper left corner?

14 May 2018, 10:39

Qysh,

It is not perfect because depending on the letter, if it has smaller width (example: l, i, 1 ...) or greater width (m, o, 8, @) the positioning will change.

But your solution suits me very well. Thank you so much! It helped me a lot. :)
rdllngr
Posts: 54
Joined: 22 Sep 2017, 09:39

Re: ToolTip – Center the tip at the x,y coordinates instead of using them for the upper left corner?

14 May 2018, 12:00

With Sean's solution (to find out the width of any text), I adapted it over the Qysh code.

You need to know the font-family and font-size of your operating system for it to work. (In my case, Segoe UI size 9 in Windows 8).

I hope this helps someone in the future. Thank you! :)

Code: Select all

#NoEnv
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#MaxThreadsPerHotkey 255
#KeyHistory 0
SetWorkingDir %A_ScriptDir%
SendMode Input
#SingleInstance Force


; Change This:

YourText := "---> (  ) <---"
sFaceName := "Segoe UI"
nHeight := 9
length := GetTextWidth(YourText, sFaceName, nHeight)

; Hold this Hotkey - if the cursor is within (), then your ToolTip is centered!
 
*a::
while(GetKeyState("a", "p"))
	{
	MouseGetPos, xpos, ypos
	xpos := xpos - (length + 10) / 2
	ypos := ypos - 13
	ToolTip, %YourText%, %xpos%, %ypos%
	}
ToolTip
return

; This hotkey tells me the size of my text.

b::
MsgBox, % "The width of my text is " . length . " pixels."
return


; Sean's code below. Just keep it.

GetTextWidth(YourText, sFaceName, nHeight = 9, bBold = False, bItalic = False, bUnderline = False, bStrikeOut = False, nCharSet = 0)
{
	hDC := DllCall("GetDC", "Uint", 0)
	nHeight := -DllCall("MulDiv", "int", nHeight, "int", DllCall("GetDeviceCaps", "Uint", hDC, "int", 90), "int", 72)

	hFont := DllCall("CreateFont", "int", nHeight, "int", 0, "int", 0, "int", 0, "int", 400 + 300 * bBold, "Uint", bItalic, "Uint", bUnderline, "Uint", bStrikeOut, "Uint", nCharSet, "Uint", 0, "Uint", 0, "Uint", 0, "Uint", 0, "str", sFaceName)
	hFold := DllCall("SelectObject", "Uint", hDC, "Uint", hFont)

	DllCall("GetTextExtentPoint32", "Uint", hDC, "str", YourText, "int", StrLen(YourText), "int64P", nSize)

	DllCall("SelectObject", "Uint", hDC, "Uint", hFold)
	DllCall("DeleteObject", "Uint", hFont)
	DllCall("ReleaseDC", "Uint", 0, "Uint", hDC)

	nWidth  := nSize & 0xFFFFFFFF
	nHeight := nSize >> 32 & 0xFFFFFFFF

	Return nWidth
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: ToolTip – Center the tip at the x,y coordinates instead of using them for the upper left corner?

14 May 2018, 12:07

the only real question remains: why go through all this ceremony, when the same can be achieved by using 3 built-in functions and a bit of math?
rdllngr
Posts: 54
Joined: 22 Sep 2017, 09:39

Re: ToolTip – Center the tip at the x,y coordinates instead of using them for the upper left corner?

14 May 2018, 12:21

swagfag,

Thank you for your solution. Unfortunately it did not work for me. Here in my PC the ToolTip does not follow the cursor, and appears in the center of my screen only.

(...)

I apologize for "all this cerimony". I'm not a programmer, so my knowledge about code is very, very poor. (I use AHK to make my Photoshop workflow faster, just for my own use, so any solution is valid as long as it works)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: ToolTip – Center the tip at the x,y coordinates instead of using them for the upper left corner?

14 May 2018, 14:03

youre right. i didnt give u a fish, but instead merely pointed at the rod, maybe swung it, too

anyway, take care man
User avatar
racruvido
Posts: 1
Joined: 12 Dec 2020, 09:06

Re: ToolTip – Center the tip at the x,y coordinates instead of using them for the upper left corner?

12 Dec 2020, 13:13

swagfag wrote:
14 May 2018, 10:22

Code: Select all

ToolTip, % "SampleTextSampleTextSampleTextSampleTextSampleTextSampleTextSampleText`n`n`nSampleTextSample"
toolHwnd := "ahk_id" . WinExist("ahk_class tooltips_class32")
WinGetPos, x, y, w, h, % toolHwnd
toolX := (A_ScreenWidth - w) // 2
toolY := (A_ScreenHeight - h) // 2
WinMove, % toolHwnd, , % toolX, % toolY

Esc::ExitApp
It was just what I needed. I wanted the tooltip to appear in the bottom left corner of the screen, but above taskbar. I modified your code a little:

Code: Select all

; Set tooltip's text:
toolTipText := "This tooltip should be at bottom right corner of the screen, but above the taskbar.`nThanks, swagfag!"
; Get the taskbar's height:
WinGetPos,,,, taskBarHeight, ahk_class Shell_TrayWnd
; Show the damn tooltip:
ToolTip, % toolTipText, A_ScreenWidth, A_ScreenHeight
; Get tooltip's handler:
toolTipHandler := "ahk_id" . WinExist("ahk_class tooltips_class32")
; Get tooltip's dimensions:
WinGetPos,,, toolTipWidth, toolTipHeight, % toolTipHandler
; And, finally, move the tooltip to the desired position:
WinMove, % toolTipHandler, , % (A_ScreenWidth - toolTipWidth), % (A_ScreenHeight - toolTipHeight - taskBarHeight)
Example image:
Image
[Mod edit: Image fixed.]

:offtopic:
P.S.: Why can't I use the img tag!? :wtf: :wtf: :wtf:
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: ToolTip – Center the tip at the x,y coordinates instead of using them for the upper left corner?

12 Dec 2020, 13:42

racruvido wrote:
12 Dec 2020, 13:13

:offtopic:
P.S.: Why can't I use the img tag!? :wtf: :wtf: :wtf:
As a new user with just one post, you have some posting restrictions (necessary anti-spam measures). Sorry for the inconvenience. This will take a few more posts to change.
But you can already upload images directly to the forums via the Attachments tab (Add files...) in the forum editor.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 394 guests