Underline Text in GUI when pointer hovers over text

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ShubhamM
Posts: 38
Joined: 08 Nov 2017, 21:38

Underline Text in GUI when pointer hovers over text

19 Nov 2017, 15:11

Hello, Everyone,

I have a GUI with a bunch of text and I want to underline that text whenever my mouse pointer hovers over that text. Anyone know if this is possible and, if so, how to implement it?

Thanks for any help you can provide!
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Underline Text in GUI when pointer hovers over text

19 Nov 2017, 19:40

I figured it out, but it requires a bit of a hack.

Code: Select all

Gui, New
Gui, Add, Text, vMyText, Text to be underlined when moused over
Gui, Show
OnMessage(0x200, "Underline")

Underline()
{
   Sleep 50
   MouseGetPos, MouseX, MouseY
   GuiControlGet, MyText, Pos

   If (MouseX > MyTextX) and (MouseY > (25*(A_ScreenDPI/96) + MyTextY)) and (MouseX < (MyTextX + MyTextW)) and (MouseY < (25*(A_ScreenDPI/96) + MyTextY + MyTextH))
      Gui, Font, underline
   Else
       Gui, Font, norm
       
   GuiControl, Font, MyText
}
The hack is that it requires checking mouse coordinates (and adding 25 for the title bar) to see if it's over the text. If you have margins within that region, the text will underline even when you're not directly over it. You can minimize that by splitting up each line of text into its own control. Now, if you wanted to set some other type of control, you'd simply do "If A_GuiControl = MyButton", but that doesn't work with text, apparently; instead, A_GuiControl is blank when you mouse over text. Anyways, so the above code works, but with the drawback that I mentioned.
ShubhamM
Posts: 38
Joined: 08 Nov 2017, 21:38

Re: Underline Text in GUI when pointer hovers over text

20 Nov 2017, 23:12

That's awesome! I'll give it a go. Do you happen to know how to change the pointer to the pointing hand that appears after you're hovering over a link?

Thanks a lot!
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Underline Text in GUI when pointer hovers over text

21 Nov 2017, 00:01

Based on Osprey's source, and the hacky way: you can put a hidden link control beneath the text control and respectively show/hide them depending on whether or not the mouse pointer is actually currently hovers them:

Code: Select all

text := "Text to be underlined when moused over"
Gui, New
Gui, Margin, 30, 30
Gui, Add, Text, xm ym vV0, % text
Gui, Add, Link, xm ym hp wp vV1 Hidden, % "<a id=""A"">" . text . "</a>"
Gui, Show
OnMessage(0x200, "Underline")
return


Underline() {
static __i := 0
CoordMode, Mouse, Client ; [EDIT]
Sleep 50
MouseGetPos, __x, __y
GuiControlGet, __pos, Pos, V0
; __v := 25*(A_ScreenDPI/96) + __posY
__v := __posY ; [EDIT]
	If ((__x > __posX) and (__y > __v) and (__x < (__posX + __posW)) and (__y < (__v + __posH))) {
		if not (__i) {
			GuiControl, Hide, V0
			GuiControl, Show, V1
		__i := 1
		}
	} else {
		if (__i) {
			GuiControl, Show, V0
			GuiControl, Hide, V1
		__i := 0
		}
	}
}
my scripts
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Underline Text in GUI when pointer hovers over text

21 Nov 2017, 01:45

Yeah, using Gui, Add, Link may be more suitable for him than what I came up with. It wasn't clear that he wanted something like a link.

FWIW, what I came up with works well for tooltips, if anyone similarly has a need to display tooltips over text or other controls (including disabled controls) that don't work with the standard GuiControl method.
ShubhamM
Posts: 38
Joined: 08 Nov 2017, 21:38

Re: Underline Text in GUI when pointer hovers over text

21 Nov 2017, 12:34

It's not an actual link that I wanted, but rather just the link select pointer. Nonetheless, this all helps a lot so I thank you guys a lot!! I thoroughly appreciate it!

Have an awesome day!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, ShatterCoder and 116 guests