Is it possible to determine the current line under the mouse cursor? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Loop
Posts: 169
Joined: 07 Jan 2019, 14:51

Is it possible to determine the current line under the mouse cursor?

26 Mar 2024, 15:37

Hello,
Is it possible to determine which line my mouse cursor is currently hovering over in a multi-line edit field?

Thank you

Code: Select all

#Requires Autohotkey v2.0
#SingleInstance Force

Message := "1A`n2B`n3C`n4D`n5E`n6F`n7G`n8H`n9I`n10J`n11K`n12L`n13M`n14N`n15O`n16P`n17Q`n18R`n19S`n20T`n21U`n22V`n23W`n24X`n25Y`n26Z"

MyGui := Gui("-Caption", "MyGui")
MyGui.BackColor := "0x333333"
MyGui.SetFont("s13 cWhite", "Georgia")
MyGui.Add("Edit", "r20 ReadOnly w600 Wrap Background333333", Message)
MyGui.OnEvent("Escape", (*)=>ExitApp())
OnMessage(0x0200, MouseMove)
MyGui.Show("AutoSize Center")


MouseMove(*){
Line := ""
ToolTip("Line number: " Line)
Sleep(500)
ToolTip()
}
Noitalommi_2
Posts: 224
Joined: 16 Aug 2023, 10:58

Re: Is it possible to determine the current line under the mouse cursor?

27 Mar 2024, 03:07

Hi.

Without clicking on the line first? Because if you click the line you can get the line number easily with EditGetCurrentLine.
Otherwise you'll probably have to do the math. Here is an example, I think i took most things into account.

Code: Select all

#Requires Autohotkey v2.0
#SingleInstance Force


Message := "1A`n2B`n3C`n4D`n5E`n6F`n7G`n8H`n9I`n10J`n11K`n12L`n13M`n14N`n15O`n16P`n17Q`n18R`n19S`n20T`n21U`n22V`n23W`n24X`n25Y`n26Z`n27`n28`n29`n30`n31`n32`n33`n34`n35`n36P`n37Q`n38R`n39S`n40T`n41U`n42V`n43W`n44X`n45Y`n46Z`n47`n48`n49`n50"

Lines := 20

MyGui := Gui("-Caption", "MyGui")
MyGui.BackColor := "0x333333"
MyGui.SetFont("s13 cWhite", "Georgia")
MyGuiEdit := MyGui.Add("Edit","r" Lines " ReadOnly w600 Wrap Background333333", Message)
MyGui.OnEvent("Escape", (*)=>ExitApp())
OnMessage(0x0200, MouseMove)
MyGui.Show("AutoSize Center")

Border := 2 ; Edit border, probably 2 pixels
DPIScale := 96/A_ScreenDPI ; if you use -DPIScale change this to DPIScale := 1
;DPIScale := 1

MouseMove(*){

	if WinActive("ahk_id" MyGui.Hwnd) {

		;CoordMode "Mouse", "Client" ; CoordMode must be client. If you don't use CoordMode somewhere else in the script, this line can be omitted.
		MyGuiEdit.GetPos(,&YE,, &Height)
		MouseGetPos(, &Y)
		if Y >= (YE-Border+Height)/DPIScale ; Cursor below the edit field
			Line := 0
		else
			Line := Round(((Y-YE+Border+Height/(Lines*2))/Height*Lines)*DPIScale) + ThumbPos("ahk_id" MyGui.Hwnd, "Edit1")
	}
	else
		Line := -1
	ToolTip Line
	Sleep 100
}

ThumbPos(WinTitle, Control) {

	static SIF_POS := 0x04
	, SB_VERT := 0x1

	if Hwnd := ControlGetHwnd(Control, WinTitle) {
		ScrollInfo := Buffer(28)
		NumPut("UInt", ScrollInfo.Size, ScrollInfo, 0), NumPut("UInt", SIF_POS, ScrollInfo, 4)
		DllCall("GetScrollInfo", "Ptr", Hwnd, "Int", SB_VERT, "Ptr", ScrollInfo)
		return NumGet(ScrollInfo, 20, "Int")
	}
}
Last edited by Noitalommi_2 on 27 Mar 2024, 06:07, edited 1 time in total.
Loop
Posts: 169
Joined: 07 Jan 2019, 14:51

Re: Is it possible to determine the current line under the mouse cursor?

27 Mar 2024, 04:05

Hello thank you,
when I run the script I get the following error, which I cannot understand
error.png
error.png (18.39 KiB) Viewed 145 times
Noitalommi_2
Posts: 224
Joined: 16 Aug 2023, 10:58

Re: Is it possible to determine the current line under the mouse cursor?

27 Mar 2024, 06:08

Strange, this message doesn't appear for me. I edited the code and wrote the line MyGuiEdit.GetPos(,&YE,, &Height) into the MouseMove function, it should now work for you as well.
Loop
Posts: 169
Joined: 07 Jan 2019, 14:51

Re: Is it possible to determine the current line under the mouse cursor?

27 Mar 2024, 09:56

-delete-
Last edited by Loop on 27 Mar 2024, 09:57, edited 1 time in total.
Loop
Posts: 169
Joined: 07 Jan 2019, 14:51

Re: Is it possible to determine the current line under the mouse cursor?

27 Mar 2024, 09:56

That's a bit strange.
The error occurs sporadically. I've run the script 5 times now, and the error has occurred 2 times. Is this a bug or is my pc too slow?
Noitalommi_2
Posts: 224
Joined: 16 Aug 2023, 10:58

Re: Is it possible to determine the current line under the mouse cursor?  Topic is solved

27 Mar 2024, 10:58

I went through the script again to find any mistakes, I also changed it a bit here and there to be able to test it better, but couldn't find anything what could be the cause of your problem.
It works for me, please test it again and maybe another forum user would be kind enough to test it and say whether it works or not.

Code: Select all

#Requires Autohotkey v2.0
#SingleInstance


Loop Lines := 1000
	Message .= A_Index  (A_Index < Lines ? "`n" : "")
MaxLines := 25
Border := 2 ; Edit border, probably 2 pixels
DPIScale := 96/A_ScreenDPI ; if you use -DPIScale change this to DPIScale := 1
;DPIScale := 1

MyGui := Gui("-Caption", "MyGui")
MyGui.BackColor := 0x333333
MyGui.SetFont("s16 cWhite", "Georgia")
MyGui.AddEdit("r" MaxLines " ReadOnly w600 Wrap Background333333 vEdit", Message)
MyGui["Edit"].GetPos(&Xe, &Ye, &We, &He)
MyGui.OnEvent("Escape", (*)=>ExitApp())
MyGui.Show("AutoSize Center")

OnMessage(0x0200, WM_MOUSEMOVE)
WM_MOUSEMOVE(*) {

	static ClassNN := MyGui["Edit"].ClassNN
	, Hwnd := "ahk_id" MyGui.Hwnd

	if WinActive(Hwnd) {

		;CoordMode "Mouse", "Client" ; CoordMode must be client. If you don't use CoordMode somewhere else in the script, this line can be omitted.
		MouseGetPos(&Xm, &Ym, &Wm, &Hm)
		if Ym >= (YE-Border+He)/DPIScale ; Cursor below the edit field
			LineNumber := 0
		else
			LineNumber := Round(((Ym-Ye+Border+He/(MaxLines*2))/He*MaxLines)*DPIScale) + ThumbPos(ClassNN, Hwnd)
	}
	else
		LineNumber := -1
	ToolTip LineNumber
	Sleep 100

	ThumbPos(Control, WinTitle) {

		static SIF_POS := 0x04
		, SB_VERT := 0x1
		, Hwnd := ControlGetHwnd(Control, WinTitle)
		, ScrollInfo := Buffer(28)
		, N := NumPut("UInt", ScrollInfo.Size, ScrollInfo, 0)
		, N := NumPut("UInt", SIF_POS, ScrollInfo, 4)

		DllCall("GetScrollInfo", "Ptr", Hwnd, "Int", SB_VERT, "Ptr", ScrollInfo)
		return NumGet(ScrollInfo, 20, "Int")
	}
}
Loop
Posts: 169
Joined: 07 Jan 2019, 14:51

Re: Is it possible to determine the current line under the mouse cursor?

27 Mar 2024, 11:30

Thank you very much for your work. It now works very well without any errors. Thank you again!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: alawsareps, reborn and 127 guests