AHK 进程管理

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效
tmplinshi
Posts: 1266
Joined: 01 Oct 2013, 14:57

AHK 进程管理

26 Oct 2016, 23:29

AhkScriptManager.png
AhkScriptManager.png (10.81 KiB) Viewed 317 times

Code: Select all

#NoEnv
#NoTrayIcon
#SingleInstance Force
SetBatchLines -1
DetectHiddenWindows On

CreateGUI()
CreateMenu()
RefreshList()
return

GuiClose:
GuiEscape:
ExitApp

CreateGUI() {
	global
	Gui, Font, s10, 微软雅黑
	Gui, Add, ListView, xm w700 r10 Grid HwndHLV gLvEvent AltSubmit, 文件名|文件路径|PID
	Loop, Parse, % "退出|重启|暂停|暂停热键|结束进程", |
		Gui, Add, Button, % (A_Index=1 ? "" : "x+40") . " Disabled gExecMenu", %A_LoopField%
	Gui, Add, Button, x+100 gReloadAll, 重启全部
	Gui, Add, Button, x+55 gRefreshList vBtnRefresh, 刷新列表
	Gui, Show,, AHK 进程管理 v1.05
}

CreateMenu() {
	Loop, Parse, % "退出|重启|暂停|暂停热键||结束进程", |
		Menu, lvMenu, Add, % A_LoopField, MenuHandler
}

RefreshList() {
	static selfPID := DllCall("GetCurrentProcessId")

	LV_Delete()

	WinGet, id, List, ahk_class AutoHotkey
	Loop, %id% {
		this_id := id%A_Index%
		WinGet, this_pid, PID, ahk_id %this_id%

		if (this_pid != selfPID)
		{
			WinGetTitle, this_title, ahk_id %this_id%
			fPath := RegExReplace(this_title, " - AutoHotkey v[\d.]+$")
			SplitPath, fPath, fName
			
			LV_Add("", fName, fPath, this_pid)
		}
	}

	LV_ModifyCol()
	LV_ModifyCol(1, "Sort")
	GuiControl,, BtnRefresh, 刷新列表
}

GuiContextMenu(GuiHwnd, CtrlHwnd) {
	global HLV
	if (CtrlHwnd = HLV) && LV_GetNext() {
		Menu, lvMenu, Show
	}
}

MenuHandler(ItemName) {
	static cmd := {重启: 65303, 暂停热键: 65305, 暂停: 65306, 退出: 65307}
	static WM_COMMAND := 0x111

	if (ItemName = "结束进程") {
		for i, obj in GetSelectedInfo()
			Process, Close, % obj.pid
	} else {
		for i, obj in GetSelectedInfo()
			PostMessage, WM_COMMAND, % cmd[ItemName],,, % obj.path " ahk_pid " obj.pid
	}

	if (ItemName = "重启") {
		GuiControl,, BtnRefresh, 刷新中...
		SetTimer, RefreshList, -3000
	} else if (ItemName ~= "退出|结束")
		SetTimer, RefreshList, -300
}

GetSelectedInfo() {
	RowNum := 0, arr := []
	while, RowNum := LV_GetNext(RowNum) {
		LV_GetText(path, RowNum, 2)
		LV_GetText(pid, RowNum, 3)
		arr.push( {pid: pid, path: path} )
	}
	return arr
}

LvEvent() {
	if !(A_GuiEvent == "I")
		return

	GuiControlGet, isEnabled, Enabled, 退出
	nSelected := LV_GetCount("Selected")
	if !(isEnabled && nSelected) || !(!isEnabled && !nSelected)
	{
		Loop, Parse, % "退出|重启|暂停|暂停热键|结束进程", |
			GuiControl, % "Enabled" !!nSelected, %A_LoopField%
	}
}

ExecMenu() {
	MenuHandler(A_GuiControl)
}

ReloadAll() {
	Loop, % LV_GetCount()
	{
		LV_GetText(path, A_Index, 2)
		LV_GetText(pid, A_Index, 3)
		PostMessage, 0x111, 65303,,, % path " ahk_pid " pid
	}
}
类似工具: 脚本调试辅助器
Last edited by tmplinshi on 21 Jun 2018, 08:15, edited 6 times in total.
User avatar
huyaowen
Posts: 100
Joined: 28 Jul 2014, 01:15

Re: AHK 进程管理

27 Oct 2016, 00:22

试过好几个都太啰嗦,这个好。简单干练。
Guest

Re: AHK 进程管理

25 Jan 2017, 22:49

感谢分享!但是我运行时报错:
AhkScriptManager.ahk (32) : ==> Functions cannot contain functions.
Specifically: RefreshList()
tmplinshi
Posts: 1266
Joined: 01 Oct 2013, 14:57

Re: AHK 进程管理

26 Jan 2017, 03:04

@Guest 文件要保存为 UTF-8 (含BOM)
et2010
Posts: 18
Joined: 25 Jan 2017, 22:37

Re: AHK 进程管理

26 Jan 2017, 08:12

@tmplinshi 感谢你的快速回复,另存为UTF-8(with BOM)后问题已解决。
pk23
Posts: 50
Joined: 24 Apr 2015, 00:49

Re: AHK 进程管理

26 Feb 2017, 21:36

感谢,小巧好用
hitman
Posts: 8
Joined: 10 Aug 2014, 06:47

Re: AHK 进程管理

13 Apr 2017, 22:05

thx,good!

Return to “脚本函数”

Who is online

Users browsing this forum: Google [Bot] and 2 guests