Page 1 of 1

Add tooltip to a control

Posted: 17 Feb 2015, 06:13
by empardopo
To add tooltip I was using this code

Code: Select all

WM_MOUSEMOVE()
{
    static CurrControl, PrevControl, _TT  ; _TT is kept blank for use by the ToolTip command below.
    CurrControl := A_GuiControl
    If (CurrControl <> PrevControl and not InStr(CurrControl, " "))
    {
        ToolTip  ; Turn off any previous tooltip.
        SetTimer, rDisplayToolTip, 1000
        PrevControl := CurrControl
    }
    return

    rDisplayToolTip:
    SetTimer, rDisplayToolTip, Off
    ToolTip % %CurrControl%_TT  ; The leading percent sign tell it to use an expression.
    SetTimer, rRemoveToolTip, 4000 ;3000 Anado un segundo para mostrar los mensajes
    return

    rRemoveToolTip:
    SetTimer, rRemoveToolTip, Off
    ToolTip
    return
}
My problem is that I want add a tooltip to a text control, is it possible? It's impossible to me do it.
Thanks in advance.

Re: Add tooltip to a control

Posted: 17 Feb 2015, 07:21
by Coco
Instead of monitoring WM_MOUSEMOVE, you should use a more convenient / reliable method of attaching a tooltip to a control. This is just a demo function and does not support detachment of attached tooltip. There are better Tooltip functions out there.

Code: Select all

Gui New
Gui Font, s10, Consolas
Gui Margin, 5, 5

Gui Add, Text, HwndhText gGuiClose, The quick brown fox jumps over the lazy dog ; dummy gLabel for static controls
AttachTip(hText, "Sample Tooltip")
GuiControl -g, %hText% ; remove dummy gLabel

Gui Show
return
GuiClose:
	ExitApp

AttachTip(hCtrl, text:="")
{
	hGui := text!="" ? DlLCall("GetParent", "Ptr", hCtrl) : hCtrl
	static hTip
	if !hTip
	{
		hTip := DllCall("CreateWindowEx", "UInt", 0x8, "Str", "tooltips_class32"
		     ,  "Ptr", 0, "UInt", 0x80000002 ;// WS_POPUP:=0x80000000|TTS_NOPREFIX:=0x02
		     ,  "Int", 0x80000000, "Int",  0x80000000, "Int", 0x80000000, "Int", 0x80000000
		     ,  "Ptr", hGui, "Ptr", 0, "Ptr", 0, "Ptr", 0, "Ptr")

		; TTM_SETMAXTIPWIDTH = 0x0418
		DllCall("SendMessage", "Ptr", hTip, "Int", 0x0418, "Ptr", 0, "Ptr", 0)

		if (A_OsVersion == "WIN_XP")
			AttachTip(hGui)
	}

	static sizeof_TOOLINFO := 24 + (6 * A_PtrSize)
	VarSetCapacity(TOOLINFO, sizeof_TOOLINFO, 0)
	, NumPut(sizeof_TOOLINFO, TOOLINFO, 0, "UInt")
	, NumPut(0x11, TOOLINFO, 4, "UInt") ; TTF_IDISHWND:=0x0001|TTF_SUBCLASS:=0x0010
	, NumPut(hGui, TOOLINFO, 8, "Ptr")
	, NumPut(hCtrl, TOOLINFO, 8 + A_PtrSize, "Ptr")
	, NumPut(&text, TOOLINFO, 24 + (3 * A_PtrSize), "Ptr")

	static TTM_ADDTOOL := A_IsUnicode ? 0x0432 : 0x0404
	return DllCall("SendMessage", "Ptr", hTip, "Int", TTM_ADDTOOL, "Ptr", 0, "Ptr", &TOOLINFO)
}

Re: Add tooltip to a control

Posted: 17 Feb 2015, 08:04
by jNizM

Re: Add tooltip to a control

Posted: 18 Feb 2015, 05:30
by empardopo
Coco wrote:Instead of monitoring WM_MOUSEMOVE, you should use a more convenient / reliable method of attaching a tooltip to a control. This is just a demo function and does not support detachment of attached tooltip. There are better Tooltip functions out there.

Code: Select all

Gui New
Gui Font, s10, Consolas
Gui Margin, 5, 5

Gui Add, Text, HwndhText gGuiClose, The quick brown fox jumps over the lazy dog ; dummy gLabel for static controls
AttachTip(hText, "Sample Tooltip")
GuiControl -g, %hText% ; remove dummy gLabel

Gui Show
return
GuiClose:
	ExitApp

AttachTip(hCtrl, text:="")
{
	hGui := text!="" ? DlLCall("GetParent", "Ptr", hCtrl) : hCtrl
	static hTip
	if !hTip
	{
		hTip := DllCall("CreateWindowEx", "UInt", 0x8, "Str", "tooltips_class32"
		     ,  "Ptr", 0, "UInt", 0x80000002 ;// WS_POPUP:=0x80000000|TTS_NOPREFIX:=0x02
		     ,  "Int", 0x80000000, "Int",  0x80000000, "Int", 0x80000000, "Int", 0x80000000
		     ,  "Ptr", hGui, "Ptr", 0, "Ptr", 0, "Ptr", 0, "Ptr")

		; TTM_SETMAXTIPWIDTH = 0x0418
		DllCall("SendMessage", "Ptr", hTip, "Int", 0x0418, "Ptr", 0, "Ptr", 0)

		if (A_OsVersion == "WIN_XP")
			AttachTip(hGui)
	}

	static sizeof_TOOLINFO := 24 + (6 * A_PtrSize)
	VarSetCapacity(TOOLINFO, sizeof_TOOLINFO, 0)
	, NumPut(sizeof_TOOLINFO, TOOLINFO, 0, "UInt")
	, NumPut(0x11, TOOLINFO, 4, "UInt") ; TTF_IDISHWND:=0x0001|TTF_SUBCLASS:=0x0010
	, NumPut(hGui, TOOLINFO, 8, "Ptr")
	, NumPut(hCtrl, TOOLINFO, 8 + A_PtrSize, "Ptr")
	, NumPut(&text, TOOLINFO, 24 + (3 * A_PtrSize), "Ptr")

	static TTM_ADDTOOL := A_IsUnicode ? 0x0432 : 0x0404
	return DllCall("SendMessage", "Ptr", hTip, "Int", TTM_ADDTOOL, "Ptr", 0, "Ptr", &TOOLINFO)
}
Thanks very much,Coco! It works fine.

@jNizM, I'll take a look. Thanks!