如何在autohotkey中通过PID获取窗口句柄?

Sui*_*tUp 1 autohotkey

我希望通过PID在autohotkey中获取窗口句柄,因为窗口的标题总是在变化.如果有人想知道,我想得到last.fm主窗口的句柄.

小智 6

诚实的安倍给出的答案是错误的. Suitup想要将PID转换为Window句柄.不是PID的窗口句柄.

要获取PID的第一个窗口Class/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
    }
}
Run Code Online (Sandbox Code Playgroud)

还有其他方法可以转换除IfWinExist以外的PID,我确定,并且可以有多个具有相同类ID的进程.:)另外你可以使用

  • 它回答了与问题相反的问题。因此 - 这是错误的 (2认同)