AutoHotkey Community

It is currently May 27th, 2012, 3:03 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: June 11th, 2009, 3:00 pm 
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.....


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: June 11th, 2009, 3:09 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Code:
WinActivate, % "ahk_id" DllCall("WindowFromPoint", Int,320, Int,500 )


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 20th, 2010, 12:03 am 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 20th, 2010, 4:34 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Learning one wrote:
is it WinID or ControlID?


Seems to be Control's hWnd :roll:

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 )


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 20th, 2010, 6:55 pm 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
That's it! :D 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 March 20th, 2010, 10:48 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 20th, 2010, 8:49 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 20th, 2010, 10:46 pm 
Offline
User avatar

Joined: April 4th, 2009, 8:19 pm
Posts: 1143
Location: Croatia
Altered.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2010, 5:26 am 
Thanks for this info...
Only one thing is left to solve: how to determine the control Class NN at specific coordinates.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 28th, 2010, 9:48 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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 )
}


Report this post
Top
 Profile  
Reply with quote  
PostPosted: April 24th, 2012, 5:22 pm 
Offline

Joined: March 10th, 2011, 7:17 pm
Posts: 374
just wanted to add this link for reference:
http://blogs.msdn.com/b/oldnewthing/arc ... 10077.aspx

as well as
http://stackoverflow.com/questions/4324 ... wfrompoint


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: rbrtryn, Yahoo [Bot] and 29 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group