AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to get the window id/title/whatever at a specific xy

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Guest1122
Guest





PostPosted: Thu Jun 11, 2009 2:00 pm    Post subject: How to get the window id/title/whatever at a specific xy Reply with quote

How do I get a window id/title/whatever at a specific position? Then activate it, without moving the mouse?

example:

x = 320
y = 500

??, x, y, wndw
WinActivate, ahk_id %wndw%

Thanks.....
Back to top
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Thu Jun 11, 2009 2:09 pm    Post subject: Reply with quote

Code:
WinActivate, % "ahk_id" DllCall("WindowFromPoint", Int,320, Int,500 )
Back to top
View user's profile Send private message Send e-mail
Learning one



Joined: 04 Apr 2009
Posts: 1000
Location: Croatia

PostPosted: Fri Mar 19, 2010 11:03 pm    Post subject: Reply with quote

I tried to write a function based on SKAN's code that gets info about window at given coordinates
But this works more like GetControlAtCoords, not as GetWinAtCoords...
What's wrong?

Code:
GetWinAtCoords(x,y,what="ID")      ; works like GetControlAtCoords?
{
   WinID := DllCall("WindowFromPoint", Int,x, Int,y)   ; by SKAN   --> is it WinID or ControlID?
   if what = ID
   Return WinID
   else if what = Class
   {
      WinGetClass, WinClass, ahk_id %WinID%
      Return WinClass
   }
   else if what = Title
   {
      WinGetTitle, WinTitle, ahk_id %WinID%
      Return WinTitle
   }
   else if what = PID
   {
      WinGet, WinPID, PID, ahk_id %WinID%
      Return WinPID
   }
}



;===Test===
1::
x := 100, y := 100
MsgBox, % GetWinAtCoords(x,y,"ID") "`n" GetWinAtCoords(x,y,"Class") "`n" GetWinAtCoords(x,y,"Title") "`n" GetWinAtCoords(x,y,"PID")
Return


WindowFromPoint Function ()
WindowFromPoint Function () wrote:
If the point is over a static text control, the return value is a handle to the window under the static text control.
So in this case window's (not control's) handle would be returned as well? Am I right?
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Sat Mar 20, 2010 3:34 pm    Post subject: Reply with quote

Learning one wrote:
is it WinID or ControlID?


Seems to be Control's hWnd Rolling Eyes

Try replacing
Code:
WinID := DllCall("WindowFromPoint", Int,x, Int,y)
with
Code:
   WinID := DllCall( "GetAncestor", UInt
           ,DllCall( "WindowFromPoint", Int,X, Int,Y )
           , UInt,GA_ROOTOWNER := 3 )
Back to top
View user's profile Send private message Send e-mail
Learning one



Joined: 04 Apr 2009
Posts: 1000
Location: Croatia

PostPosted: Sat Mar 20, 2010 5:55 pm    Post subject: Reply with quote

That's it! Very Happy Thank you! So here it is:
Code:
GetWinAtCoords(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
   {
      WinGetTitle, WinTitle, ahk_id %WinID%
      Return WinTitle
   }
   else if what = ID
   Return WinID
   else if what = Class
   {
      WinGetClass, WinClass, ahk_id %WinID%
      Return WinClass
   }
   else if what = PID
   {
      WinGet, WinPID, PID, ahk_id %WinID%
      Return WinPID
   }
}


Last edited by Learning one on Sat Mar 20, 2010 9:48 pm; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Sat Mar 20, 2010 7:49 pm    Post subject: Reply with quote

Oops! One alteration required:

Code:
   WinID := DllCall( "GetAncestor", UInt
           ,DllCall( "WindowFromPoint", Int,X, Int,Y )
           , UInt,GA_ROOT := 2 )


GA_ROOTOWNER returns Taskbar for Startmenu in WinXP!
GA_ROOT works in expected manner.
Back to top
View user's profile Send private message Send e-mail
Learning one



Joined: 04 Apr 2009
Posts: 1000
Location: Croatia

PostPosted: Sat Mar 20, 2010 9:46 pm    Post subject: Reply with quote

Altered.
Back to top
View user's profile Send private message Visit poster's website
Guest






PostPosted: Sun Mar 28, 2010 4:26 am    Post subject: Reply with quote

Thanks for this info...
Only one thing is left to solve: how to determine the control Class NN at specific coordinates.
Back to top
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Sun Mar 28, 2010 8:48 am    Post subject: Reply with quote

Anonymous wrote:
how to determine the control Class NN at specific coordinates.


Code:
; Ascertaining Control's hWnd/ClassNN for given coordinates   ;   By SKAN  | 28-March-2010
; Forum Topic :  www.autohotkey.com/forum/viewtopic.php?t=45186

If ! WinExist( "Calculator ahk_class SciCalc" ) {
   Run Calc
   WinWait, Calculator ahk_class SciCalc
}
hWnd    := WinExist( "Calculator ahk_class SciCalc" )
hCtrl   := Control_ByPos( hWnd,10,10 )
ClassNN := Control_GetClassNN( hWnd,hCtrl )
MsgBox, 0, Control @10`,10, %ClassNN%

Return                                                 ; // End of auto-execute section //

Control_ByPos( hPar, x=0, y=0 ) { ; www.autohotkey.com/forum/viewtopic.php?p=343110#343110
 hWnd := ( ID := WinExist( "ahk_id" hPar) ) ? ID : WinExist( hPar )
Return WinExist( "ahk_id" DllCall( "ChildWindowFromPoint", UInt,hWnd, Int,x, Int,y ) )
}

Control_GetClassNN( hPar,hCtrl ){ ; www.autohotkey.com/forum/viewtopic.php?p=301151#301151
 hWnd := ( ID := WinExist( "ahk_id" hPar) ) ? ID : WinExist( hPar )
 WinGet, CH, ControlListHwnd, ahk_id %hWnd%
 WinGet, CN, ControlList, ahk_id %hWnd%
 LF:= "`n",  CH:= LF CH LF, CN:= LF CN LF,  S:= SubStr( CH, 1, InStr( CH, LF hCtrl LF ) )
 StringReplace, S, S,`n,`n, UseErrorLevel
 StringGetPos, P, CN, `n, L%ErrorLevel%
Return SubStr( CN, P+2, InStr( CN, LF, 0, P+2 ) -P-2 )
}
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group