Reputation: 3182
I want to get window handle by PID in autohotkey, because title of the window is always changing. If anyone wonder, I want to get handle of last.fm main window.
Upvotes: 1
Views: 11162
Reputation: 8748
You can use the WinGet command with the Cmd paramter as ID
.
Cmd is the operation to perform, which if blank defaults to ID.
ID: Retrieves the unique ID number of a window. Also known as the window handle (HWND).
WinTitle can be a PID.
WinGet, UniqueID, ID, ahk_pid %VarContainingPID%
Another option is WinExist()
UniqueID := WinExist("ahk_pid" . VarContainingPID)
Upvotes: 0
Reputation: 111
To get the first window Class/ID of a PID you can do the following:
Process, Exist, "notepad.exe"
NewPID = %ErrorLevel% ; Save the value immediately since ErrorLevel is often changed.
if NewPID
{ ; process exists!
WinGetClass, ClassID, ahk_pid %NewPID% ; ClassID will be read here for the process
WinGetTitle, Title, ahk_pid %NewPID% ; Title will contain the processe's first window's title
IfWinExist ahk_class %ClassID% ; this will find the first window by the ClassID
{
WinGet, WinID, ID ; this will get the ID of the window into WinID variable
WinActivate ; this will bring this window to front (not necessary for example)
ListVars ; this will display your variables
Pause
}
IfWinExist %Title% ; this will find the first window with the window title
{
WinGet, WinID, ID
WinActivate ; this will bring this window to front (not necessary for example)
ListVars
Pause
}
}
there are other methods to convert the PID other than IfWinExist I'm sure, and it is possible to have more than one process with same class ID. :) Additionally you can use
Upvotes: 8
Reputation: 2507
As a reusable function:
getHwndForPid(pid) {
pidStr := "ahk_pid " . pid
WinGet, hWnd, ID, %pidStr%
return hWnd
}
Upvotes: 1