Sets the font options and name for subsequent calls to the ToolTip command.
Parameters are the same as for Gui Font.
Pass "Default" for Options to restore the font setting to default.
ToolTipColor(Background, Text)
Sets the background and text colours for subsequent calls to the ToolTip command.
Parameters are the same as for Gui Color.
Pass "Default" for either parameter to restore it to its default. If both are default, the system visual style is used.
Code: Select all
; ToolTipOpt v1.004
; Changes:
; v1.001 - Pass "Default" to restore a setting to default
; v1.002 - ANSI compatibility
; v1.003 - Added workarounds for ToolTip's parameter being overwritten
; by code within the message hook.
; v1.004 - Fixed text colour.
ToolTipFont(Options := "", Name := "", hwnd := "") {
static hfont := 0
if (hwnd = "")
hfont := Options="Default" ? 0 : _TTG("Font", Options, Name), _TTHook()
else
DllCall("SendMessage", "ptr", hwnd, "uint", 0x30, "ptr", hfont, "ptr", 0)
}
ToolTipColor(Background := "", Text := "", hwnd := "") {
static bc := "", tc := ""
if (hwnd = "") {
if (Background != "")
bc := Background="Default" ? "" : _TTG("Color", Background)
if (Text != "")
tc := Text="Default" ? "" : _TTG("Color", Text)
_TTHook()
}
else {
VarSetCapacity(empty, 2, 0)
DllCall("UxTheme.dll\SetWindowTheme", "ptr", hwnd, "ptr", 0
, "ptr", (bc != "" && tc != "") ? &empty : 0)
if (bc != "")
DllCall("SendMessage", "ptr", hwnd, "uint", 1043, "ptr", bc, "ptr", 0)
if (tc != "")
DllCall("SendMessage", "ptr", hwnd, "uint", 1044, "ptr", tc, "ptr", 0)
}
}
_TTHook() {
static hook := 0
if !hook
hook := DllCall("SetWindowsHookExW", "int", 4
, "ptr", RegisterCallback("_TTWndProc"), "ptr", 0
, "uint", DllCall("GetCurrentThreadId"), "ptr")
}
_TTWndProc(nCode, _wp, _lp) {
Critical 999
;lParam := NumGet(_lp+0*A_PtrSize)
;wParam := NumGet(_lp+1*A_PtrSize)
uMsg := NumGet(_lp+2*A_PtrSize, "uint")
hwnd := NumGet(_lp+3*A_PtrSize)
if (nCode >= 0 && (uMsg = 1081 || uMsg = 1036)) {
_hack_ = ahk_id %hwnd%
WinGetClass wclass, %_hack_%
if (wclass = "tooltips_class32") {
ToolTipColor(,, hwnd)
ToolTipFont(,, hwnd)
}
}
return DllCall("CallNextHookEx", "ptr", 0, "int", nCode, "ptr", _wp, "ptr", _lp, "ptr")
}
_TTG(Cmd, Arg1, Arg2 := "") {
static htext := 0, hgui := 0
if !htext {
Gui _TTG: Add, Text, +hwndhtext
Gui _TTG: +hwndhgui +0x40000000
}
Gui _TTG: %Cmd%, %Arg1%, %Arg2%
if (Cmd = "Font") {
GuiControl _TTG: Font, %htext%
SendMessage 0x31, 0, 0,, ahk_id %htext%
return ErrorLevel
}
if (Cmd = "Color") {
hdc := DllCall("GetDC", "ptr", htext, "ptr")
SendMessage 0x138, hdc, htext,, ahk_id %hgui%
clr := DllCall("GetBkColor", "ptr", hdc, "uint")
DllCall("ReleaseDC", "ptr", htext, "ptr", hdc)
return clr
}
}
Code: Select all
ToolTip Standard tooltip
Sleep 1000
ToolTipFont("s20", "Comic Sans MS")
ToolTip ToolTip with custom font
Sleep 1000
ToolTipColor("Black", "White")
ToolTip ToolTip with custom font and color
Sleep 1000
ExitApp
While testing just me's ToolTipEx and writing my own GUI-based version, I realized that it's not necessary to reimplement the command just to stylize a tooltip. This script uses a message hook to modify the tooltip when AutoHotkey sends it an update message (TTM_UPDATETIPTEXT). It uses the following tricks:
- Use a CallWndProc hook to intercept messages sent to tooltip windows owned by by the script. It would be better to subclass the tooltips_class32 window class (within the current process), but that requires creating a tooltips_class32 window for use with GetClassLong/SetClassLong.
Caution: Hooking messages which can interrupt AutoHotkey's commands may cause the command's parameters to be overwritten. The hook callback must avoid certain operations that use the "deref buffer", such as concatenation (except when the result is directly assigned to a variable) or functions returning strings, including WinExist (which returns a hexadecimal string). - Use a hidden GUI to parse font options/names and colour values/names.
- Apply the WS_CHILD style to a GUI so that DetectHiddenWindows isn't needed.
- It's possible to embed a GUI into a tooltip window.
- It's possible to resize a tooltip window from within the message hook, but you must WinSet Region to clear the tooltip's shape first.