Add tooltip to a control

Get help with using AutoHotkey and its commands and hotkeys
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Add tooltip to a control

17 Feb 2015, 06:13

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.
Attachments
toolttip.png
Everything is possible!
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: Add tooltip to a control

17 Feb 2015, 07:21

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)
}
User avatar
jNizM
Posts: 2639
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Add tooltip to a control

17 Feb 2015, 08:04

[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: Add tooltip to a control

18 Feb 2015, 05:30

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!
Everything is possible!

Return to “Ask For Help”

Who is online

Users browsing this forum: anhnha, Bing [Bot], boiler, Google [Bot], pnKed, SilasDeVis and 47 guests