提问:AutoHotkey如何结束taskmgr
回答:WinMenuSelectItem % "ahk_id " . WinExist("任务管理器"),, 0&, 关闭
如果用命令行关闭任务管理器,则需要管理员权限运行ahk脚本,示范如下,有需要关闭的进程请把程序名配置到代码第30行处:
#Persistent
DetectHiddenWindows,on
#NoTrayIcon
#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.
DetectHiddenWindows,on
;管理员权限运行
Loop, %0% ; For each parameter:
{
param := %A_Index% ; Fetch the contents of the variable whose name is contained in A_Index.
params .= A_Space . param
}
ShellExecute := A_IsUnicode ? "shell32\ShellExecute":"shell32\ShellExecuteA"
if not A_IsAdmin
{
If A_IsCompiled
DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_ScriptFullPath, str, params , str, A_WorkingDir, int, 1)
Else
DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_AhkPath, str, """" . A_ScriptFullPath . """" . A_Space . params, str, A_WorkingDir, int, 1)
ExitApp
}
winstr=
(LTrim join|
SohuNews.exe
taskmgr.exe
)
win:=StrSplit(winstr,"|")
for k,v in win
{
GroupAdd,game,% "ahk_exe " v
}
SetTimer, Alert1, 500
return
Alert1:
IfWinExist,ahk_group game
{
dokill()
}
else
{
Sleep,1000
}
return
;重启脚本 热键
^!r::reload
dokill()
{
global
for k,v in win
{
runwait, %ComSpec% /c taskkill /f /IM %v%, , Hide
}
}