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

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

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

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

Post by dirtyacc » 17 Oct 2019, 01:29

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

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

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

Post by dirtyacc » 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!
			}
	}
}
多谢大侠解答。很清楚了

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

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 该行不会被执行
执行过程为:
  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!
			}
	}
}

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

Post by dirtyacc » 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,后面的监听就不会生效了

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

Post by tmplinshi » 16 Oct 2019, 02:07

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

Code: Select all

Return

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

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

Post by dirtyacc » 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不运作,晕

Top