监听系统消息后很多函数都不运行啊,怎么解决 Topic is solved

遇到了问题?请先进行搜索(中文和英文),然后在此提问

Moderators: tmplinshi, arcticir

dirtyacc
Posts: 10
Joined: 16 Oct 2019, 01:19

监听系统消息后很多函数都不运行啊,怎么解决

16 Oct 2019, 01:27

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
我用这个很多地方都能找到的代码来监听系统消息,但我发现在这代码之后,很多代码不运行好像没效果一样,比如这里的Msgbox, TEST 都不会弹出来,应该怎么解决呢?我原来是想弄一个定时重启脚本的,但发现settimer放在这句前面,监听不运作,放在监听后面,settimer不运作,晕
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 监听系统消息后很多函数都不运行啊,怎么解决

16 Oct 2019, 02:07

直接这两行也会导致 MsgBox 不会被执行:

Code: Select all

Return

Msgbox, TEST
Return 就是“返回”的意思,它会使脚本停止往下执行。解决方法应该不用我多说了吧。
dirtyacc
Posts: 10
Joined: 16 Oct 2019, 01:19

Re: 监听系统消息后很多函数都不运行啊,怎么解决

16 Oct 2019, 07:27

tmplinshi wrote:
16 Oct 2019, 02:07
直接这两行也会导致 MsgBox 不会被执行:

Code: Select all

Return

Msgbox, TEST
Return 就是“返回”的意思,它会使脚本停止往下执行。解决方法应该不用我多说了吧。

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
对不起,我多了return,那这个呢。如果settimer放在后面,像这样是没有问题的。但如果先放settimer,后面的监听就不会生效了
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: 监听系统消息后很多函数都不运行啊,怎么解决

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 该行不会被执行
执行过程为:
  1. Gosub, A 跳转到标签 A,弹出消息框 A,return 返回
  2. MsgBox, continue 弹出消息框 continue
  3. 继续往下执行弹出消息框 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!
			}
	}
}
dirtyacc
Posts: 10
Joined: 16 Oct 2019, 01:19

Re: 监听系统消息后很多函数都不运行啊,怎么解决

16 Oct 2019, 09:11

tmplinshi wrote:
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 该行不会被执行
执行过程为:
  1. Gosub, A 跳转到标签 A,弹出对话框 A,return 返回
  2. MsgBox, continue 弹出对话框 continue
  3. 继续往下执行弹出对话框 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!
			}
	}
}
多谢大侠解答。很清楚了
fwejifjjwk2
Posts: 89
Joined: 10 Aug 2019, 01:49

Re: 监听系统消息后很多函数都不运行啊,怎么解决  Topic is solved

16 Oct 2019, 22:38

editing
Last edited by fwejifjjwk2 on 05 Apr 2021, 20:34, edited 1 time in total.
dirtyacc
Posts: 10
Joined: 16 Oct 2019, 01:19

Re: 监听系统消息后很多函数都不运行啊,怎么解决

17 Oct 2019, 01:29

fwejifjjwk2 wrote:
16 Oct 2019, 22:38
@dirtyacc
題外話,這段代碼的功能是什麼呢?
这是一段监听窗口生成、切换的代码,很多ahk代码里面都用到,我直接抄的,但是学艺不精遇到问题了。

在那个swithmessage里面可以按不同窗口的生成、切换、关闭做很多工作。

Return to “请求帮助”

Who is online

Users browsing this forum: No registered users and 18 guests