Page 1 of 1

Showing a ToolTip when Mouse over on item of ListBox

Posted: 17 Aug 2020, 12:07
by Loop
Hi,
Is there a possibility that when my mouse is over an item in LIstbox, it displays ToolTip.
Thx

Code: Select all

myText=
(join|
Lorem ipsum dolor sit amet, consetetur 
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy 
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut l
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor 
)

Gui, Font, s12 w500, Verdana
Gui, Add, ListBox, r5 w300  vmyListBox, %myText%
Gui, Show
Return
GuiClose:
ExitApp

Re: Showing a ToolTip when Mouse over on item of ListBox  Topic is solved

Posted: 18 Aug 2020, 18:33
by swagfag

Code: Select all

myText=
(join|
Lorem ipsum dolor sit amet, consetetur 
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy 
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut l
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor 
)

Gui, Font, s12 w500, Verdana
Gui, Add, ListBox, r5 w300 vmyListBox HwndhListBox, %myText%
Gui, Show


SetTimer showTooltips, 10
showTooltips() {
	static Tooltips := { "Lorem ipsum dolor sit amet, consetetur": "the first one"
					   , "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy": "the second"
					   , "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut l": "the third"
					   , "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed": "the 4th"
					   , "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor": "the last one" }

	CoordMode Mouse, Screen
	MouseGetPos x, y, , hCtrl, 2

	global hListBox
	if (hCtrl = hListBox)
	{
		Acc := Acc_ObjectFromPoint(id, x, y)
		itemText := Acc.accName(id)
		ToolTip % Tooltips.HasKey(itemText) ? Tooltips[itemText] : ""
	}
}

Return
GuiClose:
ExitApp

Acc_Init()
{
	Static	h
	If Not	h
		h:=DllCall("LoadLibrary","Str","oleacc","Ptr")
}

Acc_ObjectFromPoint(ByRef _idChild_ = "", x = "", y = "")
{
	Acc_Init()
	If	DllCall("oleacc\AccessibleObjectFromPoint", "Int64", x==""||y==""?0*DllCall("GetCursorPos","Int64*",pt)+pt:x&0xFFFFFFFF|y<<32, "Ptr*", pacc, "Ptr", VarSetCapacity(varChild,8+2*A_PtrSize,0)*0+&varChild)=0
	Return	ComObjEnwrap(9,pacc,1), _idChild_:=NumGet(varChild,8,"UInt")
}

Re: Showing a ToolTip when Mouse over on item of ListBox

Posted: 18 Aug 2020, 20:45
by kczx3
@swagfag why not just use LBItemFromPt?

Re: Showing a ToolTip when Mouse over on item of ListBox

Posted: 19 Aug 2020, 06:42
by swagfag
not knowing this function existed is just about the only reason

Re: Showing a ToolTip when Mouse over on item of ListBox

Posted: 19 Aug 2020, 06:53
by kczx3
:D :thumbup:

Re: Showing a ToolTip when Mouse over on item of ListBox

Posted: 19 Aug 2020, 14:31
by A_AhkUser
Here's my attempt base on kczx3's suggestion and using LBEX_ItemFromCursor and some functions of the LBEX library all by just me:

Code: Select all

#NoEnv
#Warn
SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance force

delimiter := "`n"

list=
(join%delimiter%
Lorem ipsum dolor sit amet, consetetur
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut l
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
)
Gui, +Delimiter%delimiter%
Gui, Font, s12 w500, Verdana
Gui, Add, ListBox, r2 w300 +HScroll +VScroll, % list
Gui, Add, ListBox, r2 w300 +HScroll +VScroll, % list
Gui, Show
SetTimer, label, 200
return

label() {
	static isTracking := 0x0
	static lastX := ""
	static lastY := ""
	static lastFound := 0x0
	static cb := Func("cb")
	static fn0
	static fn1
	MouseGetPos, x, y,, hControl, 2
	WinGetClass, controlClass, % "ahk_id " . hControl
	if (controlClass = "ListBox") {
		if (!isTracking || (hControl <> lastFound)) {
			isTracking := true, lastFound := hControl
			fn0 := cb.bind(hControl, false), fn1 := cb.bind(hControl, true)
		}
		if (x <> lastX || y <> lastY)  {
			lastX := x, lastY := y
			SetTimer % fn0, -1
			SetTimer % fn1, -500
			SetTimer % fn0, -2500
		}
	} else {
		ToolTip
		isTracking := 0x0
	}
}

cb(hControl, show) {
	MouseGetPos, x, y,, hwnd, 2
	if (hwnd <> hControl) {
		ToolTip
	return
	}
	itemPos := LBEX_ItemFromCursor(hControl)
	if (itemPos > -1) {
		WinGetPos, x,,,, % "ahk_id " . hControl
		CoordMode, ToolTip, Screen
		ToolTip % (show) ? LBEX_GetText(hControl, ++itemPos) : "", % x
	} else ToolTip
}
LBEX_ItemFromCursor(hListBox) { ; from LBEX_ItemFromCursor by 'just me' - https://www.autohotkey.com/boards/viewtopic.php?p=66813#profile66885
	static LB_ITEMFROMPOINT := 0x01A9
	local
	VarSetCapacity(_POINT_, 8, 0) ; POINT structure -> msdn.microsoft.com/en-us/library/dd162805(v=vs.85).aspx
	DllCall("GetCursorPos", "Ptr", &_POINT_) ; -> msdn.microsoft.com/en-us/library/ms648390(v=vs.85).aspx
	DllCall("ScreenToClient", "Ptr", hListBox, "Ptr", &_POINT_) ; -> msdn.microsoft.com/en-us/library/dd162952(v=vs.85).aspx
	x := NumGet(_POINT_, 0, "UShort") ; only 16 bits are used by LB_ITEMFROMPOINT
	y := NumGet(_POINT_, 4, "UShort") << 16 ; only 16 bits are used by LB_ITEMFROMPOINT
	SendMessage % LB_ITEMFROMPOINT, 0, % (x + y),, % "ahk_id " . hListBox ; LB_ITEMFROMPOINT -> msdn.microsoft.com/en-us/library/bb761323(v=vs.85).aspx
	if (ErrorLevel & 0xFFFF0000) ; the HIWORD of the return value is one if the cursor is outside the client area.
	return -1
	return (ErrorLevel & 0xFFFF) ; the return value contains the 0-based index of the item in the LOWORD.
}
LBEX_GetText(HLB, Index) { ; by 'just me' (https://github.com/AHK-just-me/LBEX/blob/master/Sources/LBEX.ahk)
   Static LB_GETTEXT := 0x0189
   Len := LBEX_GetTextLen(HLB, Index)
   If (Len = -1)
      Return ""
   VarSetCapacity(Text, Len << !!A_IsUnicode, 0)
   SendMessage, % LB_GETTEXT, % (Index - 1), % &Text, , % "ahk_id " . HLB
   Return StrGet(&Text, Len)
}
LBEX_GetTextLen(HLB, Index) { ; by 'just me' (https://github.com/AHK-just-me/LBEX/blob/master/Sources/LBEX.ahk)
   Static LB_GETTEXTLEN := 0x018A
   SendMessage, % LB_GETTEXTLEN, % (Index - 1), 0, , % "ahk_id " . HLB
   Return ErrorLevel
}
GuiClose:
ExitApp

A_AhkUser

Re: Showing a ToolTip when Mouse over on item of ListBox

Posted: 19 Aug 2020, 16:33
by Loop
Thanks for the work, both methods work very well.