Post by tmplinshi » 16 Oct 2019, 08:07
dirtyacc wrote: ↑16 Oct 2019, 07:27
但如果先放settimer,后面的监听就不会生效了
你忘记贴出代码了。我把你英文板块的
帖子的代码转过来吧:
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!
}
}
}
[quote=dirtyacc post_id=296754 time=1571228821 user_id=116099]但如果先放settimer,后面的监听就不会生效了[/quote]
你忘记贴出代码了。我把你英文板块的[url=https://www.autohotkey.com/boards/viewtopic.php?p=296743#p296743]帖子[/url]的代码转过来吧:
[code]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]
[size=150]解答如下:[/size]
hook 代码之所以没有被执行,是由于 SetTimer 后一行的 return 阻止了代码继续执行。看下面的例子容易理解一点:
[code]Gosub, A
MsgBox, continue
A:
MsgBox A
return ; 自动执行到此结束。
MsgBox 该行不会被执行[/code]
执行过程为:
[list=1][*][c]Gosub, A[/c] 跳转到标签 A,弹出消息框 A,return 返回
[*][c]MsgBox, continue[/c] 弹出消息框 continue
[*]继续往下执行弹出消息框 A,return 结束脚本自动执行。
[/list]
你的代码可以改成这样:
[code];=================================
; 自动执行
;=================================
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!
}
}
}[/code]