(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
}