Getting window title from a particular x y coordinate with negative values

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jadams
Posts: 73
Joined: 13 Mar 2020, 10:48

Getting window title from a particular x y coordinate with negative values

Post by jadams » 17 May 2022, 14:16

Hello, I found the function below to return a window title under a particular x y coordinate and it works but it treats negative values as the absolute. For instance -1080 is treated the same as 1080. Can anybody update the function to allow negative x y values?

Code: Select all

WinGetAtCoords(x,y,what="Title") 
{     ; by SKAN and Learning one
	; Returns Title/ID/Class/PID of window at given coordinates 
    WinID := DllCall( "GetAncestor", UInt      ; by SKAN
           ,DllCall( "WindowFromPoint", Int,X, Int,Y )
           , UInt, GA_ROOT := 2)
	if (what = "Title" or what = "T") {
		WinGetTitle, WinTitle, ahk_id %WinID%
		Return WinTitle
	}
	else if (what = "ID" or what = "I")
		Return WinID
	else if (what = "Class" or what = "C") {
		WinGetClass, WinClass, ahk_id %WinID%
		Return WinClass
	}
	else if (what = "PID" or what = "P") {
		WinGet, WinPID, PID, ahk_id %WinID%
		Return WinPID
	}
}	; http://www.autohotkey.com/forum/viewtopic.php?p=341120#341120

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Getting window title from a particular x y coordinate with negative values

Post by mikeyww » 17 May 2022, 14:43

You could just use MouseGetPos and WinGetTitle. I think you would move the mouse first, so I'm not sure whether that poses a problem. Another approach could be to get the list of windows & their positions, and then see what matches your x and y.

Code: Select all

F3::MsgBox, 64, Window title, % windowTitle(558, 545)

windowTitle(x, y) {
 WinGet, win, List
 Loop, %win% {
  WinGetPos, x1, y1, width, height, % winTitle := "ahk_id" win%A_Index%
  x2 := x1 + width - 1, y2 := y1 + height - 1
  If x between %x1% and %x2%
   If y between %y1% and %y2%
   { WinGetTitle, title, %winTitle%
     Return title
   }
 }
}
Of course, one coordinate could correspond to many windows. This function returns the topmost window title.

jadams
Posts: 73
Joined: 13 Mar 2020, 10:48

Re: Getting window title from a particular x y coordinate with negative values

Post by jadams » 17 May 2022, 15:11

That doesn't work because what I need is the title of a window present at a particular coordinate without having activated the window and without knowing anything about the window that's there.

jadams
Posts: 73
Joined: 13 Mar 2020, 10:48

Re: Getting window title from a particular x y coordinate with negative values

Post by jadams » 17 May 2022, 15:12

mikeyww wrote:
17 May 2022, 14:43
You could just use MouseGetPos and WinGetTitle. I think you would move the mouse first, so I'm not sure whether that poses a problem. Another approach could be to get the list of windows & their positions, and then see what matches your x and y.

Code: Select all

F3::MsgBox, 64, Window title, % windowTitle(558, 545)

windowTitle(x, y) {
 WinGet, win, List
 Loop, %win% {
  WinGetPos, x1, y1, width, height, % winTitle := "ahk_id" win%A_Index%
  x2 := x1 + width - 1, y2 := y1 + height - 1
  If x between %x1% and %x2%
   If y between %y1% and %y2%
   { WinGetTitle, title, %winTitle%
     Return title
   }
 }
}
Of course, one coordinate could correspond to many windows. This function returns the topmost window title.
That doesn't work for me... I just need the title at a particular coordinate even when that is not the active window.

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Getting window title from a particular x y coordinate with negative values

Post by mikeyww » 17 May 2022, 15:13

You ran my script? What happened when you ran it?

jadams
Posts: 73
Joined: 13 Mar 2020, 10:48

Re: Getting window title from a particular x y coordinate with negative values

Post by jadams » 17 May 2022, 15:15

mikeyww wrote:
17 May 2022, 14:43
You could just use MouseGetPos and WinGetTitle. I think you would move the mouse first, so I'm not sure whether that poses a problem. Another approach could be to get the list of windows & their positions, and then see what matches your x and y.

Code: Select all

F3::MsgBox, 64, Window title, % windowTitle(558, 545)

windowTitle(x, y) {
 WinGet, win, List
 Loop, %win% {
  WinGetPos, x1, y1, width, height, % winTitle := "ahk_id" win%A_Index%
  x2 := x1 + width - 1, y2 := y1 + height - 1
  If x between %x1% and %x2%
   If y between %y1% and %y2%
   { WinGetTitle, title, %winTitle%
     Return title
   }
 }
}
Of course, one coordinate could correspond to many windows. This function returns the topmost window title.
Thank you :-) This still has the same issue though. If you pass a negative coordinate into it it doesn't work. For instance this doesn't work:

MsgBox, 64, Window title, % windowTitle(558, -1080)


jadams
Posts: 73
Joined: 13 Mar 2020, 10:48

Re: Getting window title from a particular x y coordinate with negative values

Post by jadams » 18 May 2022, 09:04

Anybody else have any ideas? It's not uncommon to have negative X Y values if your primary monitor is not the top left monitor. Does anyone know how to get anwindow title at a specific X Y value when the x or y value is negative?

iPhilip
Posts: 819
Joined: 02 Oct 2013, 12:21

Re: Getting window title from a particular x y coordinate with negative values

Post by iPhilip » 18 May 2022, 11:51

@jadams, The script below shows the position and title of any window, including windows with negative values for their positions. I tested it on my computer with multiple monitors.

Code: Select all

#NoEnv
#Persistent

SetTimer, Timer
return

Timer:
MouseGetPos,x,y
ToolTip % "The title of the window at (" x ", " y ") is`n" WinGetAtCoords(x,y)
return

Esc::ExitApp

WinGetAtCoords(x,y,what="Title") 
{     ; by SKAN and Learning one
	; Returns Title/ID/Class/PID of window at given coordinates 
    WinID := DllCall( "GetAncestor", UInt      ; by SKAN
           ,DllCall( "WindowFromPoint", Int,X, Int,Y )
           , UInt, GA_ROOT := 2)
	if (what = "Title" or what = "T") {
		WinGetTitle, WinTitle, ahk_id %WinID%
		Return WinTitle
	}
	else if (what = "ID" or what = "I")
		Return WinID
	else if (what = "Class" or what = "C") {
		WinGetClass, WinClass, ahk_id %WinID%
		Return WinClass
	}
	else if (what = "PID" or what = "P") {
		WinGet, WinPID, PID, ahk_id %WinID%
		Return WinPID
	}
}	; http://www.autohotkey.com/forum/viewtopic.php?p=341120#341120
I hope it helps.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Post Reply

Return to “Ask for Help (v1)”