2012-08-14 212 views

回答

6

通过Honest Abe给出的答案是不正确。 Suitup想要将PID转换为窗口句柄。不是窗口句柄到PID。

为了得到一个PID的第一窗口类/ ID,你可以做到以下几点:

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 
    } 
} 

还有其他的方法来对PID等将大于IfWinExist我敢肯定,并且可以有具有相同类ID的多个进程。 :) 此外,您可以使用

+0

我还没有仔细研究,特别是提问/回答,但你肯定@Suitup的答案是不正确的? OP已被接受为正确。 – Magnilex 2015-01-04 18:47:56

+1

它回答了问题的反面。因此 - 这是错误的 – user1167442 2015-12-02 04:23:01

+0

一个进程可以有多达[10,000个窗口](https://blogs.msdn.microsoft.com/oldnewthing/20070718-00/?p=25963)。在_winget_中使用'ID'和'IDLast'和'ahk_PID'。如果结果相同,则只有一个hwnd。 – 2017-02-09 11:55:45

0

作为可重复使用的功能:

getHwndForPid(pid) { 
    pidStr := "ahk_pid " . pid 
    WinGet, hWnd, ID, %pidStr% 
    return hWnd 
}