我已经找到了如何暂停特定进程,以及如何获取前台窗口的进程路径和名称,但我不确定如何将两者结合起来。
我找到了这个脚本并已经用 Spotify 对其进行了测试,到目前为止它运行良好。
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
!s::
Process_Suspend("Spotify.exe")
!r::
Process_Resume("Spotify.exe")
;============================== Working on WinXP+
Process_Suspend(PID_or_Name){
PID := (InStr(PID_or_Name,".")) ? ProcExist(PID_or_Name) : PID_or_Name
h:=DllCall("OpenProcess", "uInt", 0x1F0FFF, "Int", 0, "Int", pid)
If !h
Return -1
DllCall("ntdll.dll\NtSuspendProcess", "Int", h)
DllCall("CloseHandle", "Int", h)
}
Process_Resume(PID_or_Name){
PID := (InStr(PID_or_Name,".")) ? ProcExist(PID_or_Name) : PID_or_Name
h:=DllCall("OpenProcess", "uInt", 0x1F0FFF, "Int", 0, "Int", pid)
If !h
Return -1
DllCall("ntdll.dll\NtResumeProcess", "Int", h)
DllCall("CloseHandle", "Int", h)
}
ProcExist(PID_or_Name=""){
Process, Exist, % (PID_or_Name="") ? DllCall("GetCurrentProcessID") : PID_or_Name
Return Errorlevel
}
我还发现了这个关于获取当前进程 ID 的页面:https://www.autohotkey.com/docs/commands/WinGet.htm。
有没有办法将它们放在一起并使暂停脚本适用于任何前台进程,而不仅仅是脚本中指定的进程?
具体来说,如何获取 ProcessName 或 ProcessPath 并将其插入“Spotify.exe”当前所在的位置?这样的脚本会是什么样子的?
谢谢!
答案1
当脚本正在运行时,您需要做的就是获取活动进程的 PID 并调用Process_Suspend
。您应该保留 PID 以供恢复时使用。
如同:
pid = 0
!s::
WinGet, pid, PID , A ; A means the active window
Process_Suspend(pid)
Return ; otherwise the script jumbs to the next hotkey and the process resumes
!r:: Process_Resume(pid)