;---------------------------------------
; 多个脚本间的简单通讯 v1.0 By FeiYue
;
; 使用方法:
; 1、各个脚本在脚本开头用 Say_Set(通讯号码) 设置自身通讯号码
; 2、各个脚本用 Say(要说的话, 通讯号码) 互相发送字符串
; 3、各个脚本在 Say_Get(s) 中处理收到的字符串,默认为显示
;---------------------------------------
; 下面是启动三个进程代替三个脚本互相通讯的例子
进程1代码 =
(` %
Say_Set(1) ; 设置自身通讯号码为1
return
F3:: Say("1号对0号说话", 0)
;===== 下面是通讯函数 =====
Say_Get(s) {
A_Args.s:=s ; 保存到全局变量,可以在其他函数中使用
MsgBox, 4096,, % s
}
Say(s, number) {
DetectHiddenWindows, On
number:=Format("{:d}", number)
IfWinExist, Say[%number%] ahk_class AutoHotkeyGUI
{
ControlSetText,, %s%
Critical, % (cri:=A_IsCritical)?"On":"On"
WinClose
Critical, %cri%
}
}
Say_Set(number, args*) {
if (number="WM_Close")
{
Critical
if (A_Gui="Say_Gui")
{
WinMenuSelectItem,,, 1&
return 1
}
}
else if (number="Menu_Go")
{
ControlGetText, s
Say_Get(s)
}
else
{
Gui, Say_Gui: +LastFoundExist
IfWinNotExist
{
r:=Func(A_ThisFunc).Bind("Menu_Go")
Menu, Say_Gui_Menu, Add, Go, %r%
Gui, Say_Gui: Add, Edit
Gui, Say_Gui: Menu, Say_Gui_Menu
OnMessage(0x10, Func(A_ThisFunc).Bind("WM_Close"))
}
number:=Format("{:d}", number)
Gui, Say_Gui: Show, Hide, Say[%number%]
}
}
)
进程2代码 =
(` %
Say_Set(2) ; 设置自身通讯号码为2
return
F4:: Say("2号对0号说话", 0)
;===== 下面是通讯函数 =====
Say_Get(s) {
A_Args.s:=s ; 保存到全局变量,可以在其他函数中使用
MsgBox, 4096,, % s
}
Say(s, number) {
DetectHiddenWindows, On
number:=Format("{:d}", number)
IfWinExist, Say[%number%] ahk_class AutoHotkeyGUI
{
ControlSetText,, %s%
Critical, % (cri:=A_IsCritical)?"On":"On"
WinClose
Critical, %cri%
}
}
Say_Set(number, args*) {
if (number="WM_Close")
{
Critical
if (A_Gui="Say_Gui")
{
WinMenuSelectItem,,, 1&
return 1
}
}
else if (number="Menu_Go")
{
ControlGetText, s
Say_Get(s)
}
else
{
Gui, Say_Gui: +LastFoundExist
IfWinNotExist
{
r:=Func(A_ThisFunc).Bind("Menu_Go")
Menu, Say_Gui_Menu, Add, Go, %r%
Gui, Say_Gui: Add, Edit
Gui, Say_Gui: Menu, Say_Gui_Menu
OnMessage(0x10, Func(A_ThisFunc).Bind("WM_Close"))
}
number:=Format("{:d}", number)
Gui, Say_Gui: Show, Hide, Say[%number%]
}
}
)
进程1:= new Thread(进程1代码)
进程2:= new Thread(进程2代码)
Say_Set(0) ; 设置自身通讯号码为0
return
F1:: Say("0号对1号说话", 0)
F2:: Say("0号对2号说话", 2)
;===== 下面是通讯函数 =====
Say_Get(s) {
A_Args.s:=s ; 保存到全局变量,可以在其他函数中使用
MsgBox, 4096,, % s
}
Say(s, number) {
DetectHiddenWindows, On
number:=Format("{:d}", number)
IfWinExist, Say[%number%] ahk_class AutoHotkeyGUI
{
ControlSetText,, %s%
Critical, % (cri:=A_IsCritical)?"On":"On"
WinClose
Critical, %cri%
}
}
Say_Set(number, args*) {
if (number="WM_Close")
{
Critical
if (A_Gui="Say_Gui")
{
WinMenuSelectItem,,, 1&
return 1
}
}
else if (number="Menu_Go")
{
ControlGetText, s
Say_Get(s)
}
else
{
Gui, Say_Gui: +LastFoundExist
IfWinNotExist
{
r:=Func(A_ThisFunc).Bind("Menu_Go")
Menu, Say_Gui_Menu, Add, Go, %r%
Gui, Say_Gui: Add, Edit
Gui, Say_Gui: Menu, Say_Gui_Menu
OnMessage(0x10, Func(A_ThisFunc).Bind("WM_Close"))
}
number:=Format("{:d}", number)
Gui, Say_Gui: Show, Hide, Say[%number%]
}
}
Say_Test(number) {
DetectHiddenWindows, On
number:=Format("{:d}", number)
WinGet, id, ID, Say[%number%] ahk_class AutoHotkeyGUI
return (id ? 1:0)
}
;===== 下面是多进程类 =====
Class Thread {
__New(args*) {
this.pid:=this.Exec(args*)
}
__Delete() {
pid:=this.pid
DetectHiddenWindows, On
IfWinExist, ahk_class AutoHotkey ahk_pid %pid%
{
PostMessage, 0x111, 65307
WinWaitClose,,, 0.5
IfEqual, ErrorLevel, 1, Process, Close, %pid%
}
}
Exec(s, Ahk:="", args:="") {
Ahk:=Ahk ? Ahk:A_IsCompiled ? A_ScriptDir "\AutoHotkey.exe":A_AhkPath
s:="DllCall(""SetWindowText"",""Ptr"",A_ScriptHwnd,""Str"",""<AHK>"")`n"
. StrReplace(s,"`r")
Try {
shell:=ComObjCreate("WScript.Shell")
oExec:=shell.Exec("""" Ahk """ /force * " args)
oExec.StdIn.Write(s)
oExec.StdIn.Close(), pid:=oExec.ProcessID
}
Catch {
f:=A_Temp "\~ahk.tmp"
s:="`n FileDelete, " f "`n" s
FileDelete, %f%
FileAppend, %s%, %f%
r:=this.Clear.Bind(this)
SetTimer, %r%, -3000
Run, "%Ahk%" /force "%f%" %args%,, UseErrorLevel, pid
}
return pid
}
Clear() {
FileDelete, % A_Temp "\~ahk.tmp"
SetTimer,, Off
}
}
牛逼 .
Say_Set()貌似要主动去运行,
要是弄成,收到另外脚本的信息,就自动执行的函数,就爽了.