首页 > 解决方案 > ahk 运行 pid 与 Window Spy 中显示的不同

问题描述

我正在尝试从运行命令中捕获 PID 并使用 PID 来调整窗口大小。但没有运气。花了几个小时后,我意识到消息框中显示的 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

标签: autohotkey

解决方案


这是因为出现的窗口不是来自git-bash.exe进程。
它来自mintty.exe,它会在你运行后的某个地方启动git-bash.exe

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

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

推荐阅读