Code: Select all
Gui +LastFound
hWnd:=WinExist()
DllCall("RegisterShellHookWindow", UInt,hWnd)
MsgNum:=DllCall("RegisterWindowMessage", Str,"SHELLHOOK")
OnMessage(MsgNum, "SwitchMessage")
HSHELL_WINDOWCREATED:=1
Return
Msgbox, TEST
Moderators: tmplinshi, arcticir
Code: Select all
Gui +LastFound
hWnd:=WinExist()
DllCall("RegisterShellHookWindow", UInt,hWnd)
MsgNum:=DllCall("RegisterWindowMessage", Str,"SHELLHOOK")
OnMessage(MsgNum, "SwitchMessage")
HSHELL_WINDOWCREATED:=1
Return
Msgbox, TEST
Code: Select all
Return
Msgbox, TEST
tmplinshi wrote: ↑16 Oct 2019, 02:07直接这两行也会导致 MsgBox 不会被执行:Return 就是“返回”的意思,它会使脚本停止往下执行。解决方法应该不用我多说了吧。Code: Select all
Return Msgbox, TEST
Code: Select all
Gui +LastFound
hWnd:=WinExist()
DllCall("RegisterShellHookWindow", UInt,hWnd)
MsgNum:=DllCall("RegisterWindowMessage", Str,"SHELLHOOK")
OnMessage(MsgNum, "SwitchMessage")
HSHELL_WINDOWCREATED:=1
SwitchMessage( wParam,lParam )
{
If ( wParam != 1 )
{
If WinActive("ahk_exe explorer.exe")
{
MsgBox, explorer detected!
}
}
}
ReloadTimer := % 1000*5
;reload timer
Gosub,AutoReloadInit
AutoReloadInit:
SetTimer, SelfReload, % ReloadTimer
return
SelfReload:
MsgBox, TEST
reload
return
你忘记贴出代码了。我把你英文板块的帖子的代码转过来吧:
Code: Select all
ReloadTimer := % 1000*5
;reload timer
Gosub,AutoReloadInit
AutoReloadInit:
SetTimer, SelfReload, % ReloadTimer
return
SelfReload:
MsgBox, TEST
reload
return
Gui +LastFound
hWnd:=WinExist()
DllCall("RegisterShellHookWindow", UInt,hWnd)
MsgNum:=DllCall("RegisterWindowMessage", Str,"SHELLHOOK")
OnMessage(MsgNum, "SwitchMessage")
HSHELL_WINDOWCREATED:=1
SwitchMessage( wParam,lParam )
{
If ( wParam != 1 )
{
If WinActive("ahk_exe explorer.exe")
{
MsgBox, explorer detected!
}
}
}
Code: Select all
Gosub, A
MsgBox, continue
A:
MsgBox A
return ; 自动执行到此结束。
MsgBox 该行不会被执行
Code: Select all
;=================================
; 自动执行
;=================================
ReloadTimer := % 1000*5
Gosub,AutoReloadInit
Gui +LastFound
hWnd:=WinExist()
DllCall("RegisterShellHookWindow", UInt,hWnd)
MsgNum:=DllCall("RegisterWindowMessage", Str,"SHELLHOOK")
OnMessage(MsgNum, "SwitchMessage")
HSHELL_WINDOWCREATED:=1
return ; 自动执行结束
;=================================
; 标签
;=================================
AutoReloadInit:
SetTimer, SelfReload, % ReloadTimer
return
SelfReload:
MsgBox, TEST
reload
return
;=================================
; 函数
;=================================
SwitchMessage( wParam,lParam )
{
If ( wParam != 1 )
{
If WinActive("ahk_exe explorer.exe")
{
MsgBox, explorer detected!
}
}
}
多谢大侠解答。很清楚了tmplinshi wrote: ↑16 Oct 2019, 08:07你忘记贴出代码了。我把你英文板块的帖子的代码转过来吧:Code: Select all
ReloadTimer := % 1000*5 ;reload timer Gosub,AutoReloadInit AutoReloadInit: SetTimer, SelfReload, % ReloadTimer return SelfReload: MsgBox, TEST reload return Gui +LastFound hWnd:=WinExist() DllCall("RegisterShellHookWindow", UInt,hWnd) MsgNum:=DllCall("RegisterWindowMessage", Str,"SHELLHOOK") OnMessage(MsgNum, "SwitchMessage") HSHELL_WINDOWCREATED:=1 SwitchMessage( wParam,lParam ) { If ( wParam != 1 ) { If WinActive("ahk_exe explorer.exe") { MsgBox, explorer detected! } } }
解答如下:
hook 代码之所以没有被执行,是由于 SetTimer 后一行的 return 阻止了代码继续执行。看下面的例子容易理解一点:执行过程为:Code: Select all
Gosub, A MsgBox, continue A: MsgBox A return ; 自动执行到此结束。 MsgBox 该行不会被执行
你的代码可以改成这样:
- Gosub, A 跳转到标签 A,弹出对话框 A,return 返回
- MsgBox, continue 弹出对话框 continue
- 继续往下执行弹出对话框 A,return 结束脚本自动执行。
Code: Select all
;================================= ; 自动执行 ;================================= ReloadTimer := % 1000*5 Gosub,AutoReloadInit Gui +LastFound hWnd:=WinExist() DllCall("RegisterShellHookWindow", UInt,hWnd) MsgNum:=DllCall("RegisterWindowMessage", Str,"SHELLHOOK") OnMessage(MsgNum, "SwitchMessage") HSHELL_WINDOWCREATED:=1 return ; 自动执行结束 ;================================= ; 标签 ;================================= AutoReloadInit: SetTimer, SelfReload, % ReloadTimer return SelfReload: MsgBox, TEST reload return ;================================= ; 函数 ;================================= SwitchMessage( wParam,lParam ) { If ( wParam != 1 ) { If WinActive("ahk_exe explorer.exe") { MsgBox, explorer detected! } } }
这是一段监听窗口生成、切换的代码,很多ahk代码里面都用到,我直接抄的,但是学艺不精遇到问题了。
Users browsing this forum: No registered users and 67 guests