暂停或取消暂停另一个脚本的唯一方法是:
- 通过发送菜单命令消息;即切换。
- 通过实现某种形式的进程间通信(消息传递)并让其他脚本自行挂起。
但是,您可以做的是在发送切换消息之前检查脚本是否已暂停。您可以通过检查“暂停热键”菜单项是否有复选标记来执行此操作。
ScriptSuspend(ScriptName, SuspendOn)
{
; Get the HWND of the script's main window (which is usually hidden).
dhw := A_DetectHiddenWindows
DetectHiddenWindows On
if scriptHWND := WinExist(ScriptName " ahk_class AutoHotkey")
{
; This constant is defined in the AutoHotkey source code (resource.h):
static ID_FILE_SUSPEND := 65404
; Get the menu bar.
mainMenu := DllCall("GetMenu", "ptr", scriptHWND)
; Get the File menu.
fileMenu := DllCall("GetSubMenu", "ptr", mainMenu, "int", 0)
; Get the state of the menu item.
state := DllCall("GetMenuState", "ptr", fileMenu, "uint", ID_FILE_SUSPEND, "uint", 0)
; Get the checkmark flag.
isSuspended := state >> 3 & 1
; Clean up.
DllCall("CloseHandle", "ptr", fileMenu)
DllCall("CloseHandle", "ptr", mainMenu)
if (!SuspendOn != !isSuspended)
SendMessage 0x111, ID_FILE_SUSPEND,,, ahk_id %scriptHWND%
; Otherwise, it's already in the right state.
}
DetectHiddenWindows %dhw%
}
用法如下:
SetTitleMatchMode 2 ; Allow filename instead of full path.
ScriptSuspend("script1.ahk", true) ; Suspend.
ScriptSuspend("script1.ahk", false) ; Unsuspend.
您可以通过将 ID_FILE_SUSPEND (65404) 替换为 ID_FILE_PAUSE (65403) 来对暂停执行相同操作。但是,您需要将 WM_ENTERMENULOOP (0x211) 和 WM_EXITMENULOOP (0x212) 消息发送到要更新暂停脚本复选标记的脚本。