ahk 运行 pid 与 Window Spy 中显示的不同

ahk run pid is not same as shown in Window Spy

我正在尝试从 运行 命令捕获 PID 并使用 PID 调整大小 window。但没有运气。花了几个小时后,我意识到消息框中显示的 PID 与通过 Spy Window 检查的 PID 不同。有什么建议吗?

Run, "C:\Program Files\Git\git-bash.exe",,, PID
;Sleep, 300
MsgBox, %PID% is the process PID ; --> PID shown here is different from PID inspected via Window Spy
WinWait, ahk_pid %PID%
;Sleep, 300
;WinActivate, ahk_pid %PID%
WinMinimize, ahk_pid %PID%
;WinMove, ahk_pid %PID%, ,500, 250, 200, 100
return

这是因为出现的 window 不是来自 git-bash.exe 过程。
它来自 mintty.exe,它会在您 运行 git-bash.exe.

之后的某处启动

您可以做的最简单的事情就是在启动 git-bash.exe 后等待 window 出现,如下所示:

Run, % "C:\Program Files\Git\git-bash.exe"
;// sleep a bit because other things from mintty.exe seem to get lauched first as well
Sleep, 1000
WinWait, % "ahk_exe mintty.exe"
;// get the hwnd of the last found window (the window we waited for above)
hwnd := WinExist()

;// and if you for some reason really need the PID, you can do e.g this to get it
WinGet, pid, PID, % "ahk_id " hwnd
MsgBox, % pid