[Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

Post your working scripts, libraries and tools
User
Posts: 407
Joined: 26 Jun 2017, 08:12

[Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

08 Jan 2019, 22:46

Hi,

I could have written a function something like "OnWin(Event,Function,WinRef,RefMode,Options)" , but using that kind of format limit things a lot!

So I decided to use the below methods, which area easier and more practical to use than the function format mentioned above:

98_ z_ Image.png
98_ z_ Image.png (7.42 KiB) Viewed 3761 times

OnWin() - All the codes related to windows Events can be written inside this function!

Code: Select all

gui, +AlwaysOnTop

gui, add, text, w50 vNotepad_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "NotePad" window is activated!

gui, add, text, xm w50 vPaint_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "Paint" window is activated!

gui, add, text, xm w50 vCmd_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "Cmd" window is activated!

gui, show

run, notepad
run, Mspaint
run, cmd

return

Increase_Notepad_Counter:	;________________________

if NotePad_Counter is not number
NotePad_Counter = 1

guicontrol, , Notepad_Counter, % NotePad_Counter++

return

Increase_Paint_Counter:	;________________________

if Paint_Counter is not number
Paint_Counter = 1

guicontrol, , Paint_Counter, % Paint_Counter++

return

Increase_Cmd_Counter:	;________________________

if Cmd_Counter is not number
Cmd_Counter = 1

guicontrol, , Cmd_Counter, % Cmd_Counter++

return

GuiClose:	;______________________________
ExitApp


OnWin(Event, Hwnd)	;_____________________________________________
{
Static This_Func_Name := "OnWin"


	;https://autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/#entry116488


;Local	;Force Local Mode (Un-Comment "Local" if supported by your version of AHK)

	;Gui +LastFound		;This Script "Gui 1" will be used ("gui 1" is automatically created from here)
	;hWnd := WinExist()	;stores the above "Gui 1" window id in "hwnd" variable

	;if the above "Gui 1" is destroyed, the "onMessage()" below will not work anymore

Static RunAtScriptExecution1 := DllCall( "RegisterShellHookWindow", UInt, A_ScriptHwnd)		;"A_ScriptHwnd" in use instead the above "hWnd" variable
Static SH_MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
Static RunAtScriptExecution2 := OnMessage(SH_MsgNum, Func(This_Func_Name), 1000)		;1000 = MaxThreads / Func("Name_Of_Function") is recommended instead "Name_Of_Function" only

	if (Event == 4)		;"4" = HSHELL_WINDOWACTIVATED
	{
	WinGetClass, Class_Found, % "ahk_id" Hwnd
	
	if (Class_Found = "Notepad")
	gosub, Increase_Notepad_Counter

	if (Class_Found = "MsPaintApp")
	gosub, Increase_Paint_Counter

	if (Class_Found = "ConsoleWindowClass")
	gosub, Increase_Cmd_Counter
	}

}

On_WinType_Event() - codes related to windows Events can be written separately!

Code: Select all

gui, +AlwaysOnTop

gui, add, text, w50 vNotepad_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "NotePad" window is activated!

gui, add, text, xm w50 vPaint_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "Paint" window is activated!

gui, add, text, xm w50 vCmd_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "Cmd" window is activated!

gui, show

run, notepad
run, Mspaint
run, cmd

return

Increase_Notepad_Counter:	;________________________

if NotePad_Counter is not number
NotePad_Counter = 1

guicontrol, , Notepad_Counter, % NotePad_Counter++

return

Increase_Paint_Counter:	;________________________

if Paint_Counter is not number
Paint_Counter = 1

guicontrol, , Paint_Counter, % Paint_Counter++

return

Increase_Cmd_Counter:	;________________________

if Cmd_Counter is not number
Cmd_Counter = 1

guicontrol, , Cmd_Counter, % Cmd_Counter++

return

GuiClose:	;______________________________
ExitApp


On_Notepad_Active(Event, Hwnd)	;_____________________________________________
{
Static This_Func_Name := "On_Notepad_Active"


	;https://autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/#entry116488


;Local	;Force Local Mode (Un-Comment "Local" if supported by your version of AHK)

	;Gui +LastFound		;This Script "Gui 1" will be used ("gui 1" is automatically created from here)
	;hWnd := WinExist()	;stores the above "Gui 1" window id in "hwnd" variable

	;if the above "Gui 1" is destroyed, the "onMessage()" below will not work anymore

Static RunAtScriptExecution1 := DllCall( "RegisterShellHookWindow", UInt, A_ScriptHwnd)		;"A_ScriptHwnd" in use instead the above "hWnd" variable
Static SH_MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
Static RunAtScriptExecution2 := OnMessage(SH_MsgNum, Func(This_Func_Name), 1000)		;1000 = MaxThreads / Func("Name_Of_Function") is recommended instead "Name_Of_Function" only

	if (Event == 4)		;"4" = HSHELL_WINDOWACTIVATED
	{
	WinGetClass, Class_Found, % "ahk_id" Hwnd
	
	if (Class_Found = "Notepad")
	gosub, Increase_Notepad_Counter
	}

}


On_Paint_Active(Event, Hwnd)	;_____________________________________________
{
Static This_Func_Name := "On_Paint_Active"


	;https://autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/#entry116488


;Local	;Force Local Mode (Un-Comment "Local" if supported by your version of AHK)

	;Gui +LastFound		;This Script "Gui 1" will be used ("gui 1" is automatically created from here)
	;hWnd := WinExist()	;stores the above "Gui 1" window id in "hwnd" variable

	;if the above "Gui 1" is destroyed, the "onMessage()" below will not work anymore

Static RunAtScriptExecution1 := DllCall( "RegisterShellHookWindow", UInt, A_ScriptHwnd)		;"A_ScriptHwnd" in use instead the above "hWnd" variable
Static SH_MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
Static RunAtScriptExecution2 := OnMessage(SH_MsgNum, Func(This_Func_Name), 1000)		;1000 = MaxThreads / Func("Name_Of_Function") is recommended instead "Name_Of_Function" only

	if (Event == 4)		;"4" = HSHELL_WINDOWACTIVATED
	{
	WinGetClass, Class_Found, % "ahk_id" Hwnd
	
	if (Class_Found = "MsPaintApp")
	gosub, Increase_Paint_Counter
	}

}


On_Cmd_Active(Event, Hwnd)	;_____________________________________________
{
Static This_Func_Name := "On_Cmd_Active"


	;https://autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/#entry116488


;Local	;Force Local Mode (Un-Comment "Local" if supported by your version of AHK)

	;Gui +LastFound		;This Script "Gui 1" will be used ("gui 1" is automatically created from here)
	;hWnd := WinExist()	;stores the above "Gui 1" window id in "hwnd" variable

	;if the above "Gui 1" is destroyed, the "onMessage()" below will not work anymore

Static RunAtScriptExecution1 := DllCall( "RegisterShellHookWindow", UInt, A_ScriptHwnd)		;"A_ScriptHwnd" in use instead the above "hWnd" variable
Static SH_MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
Static RunAtScriptExecution2 := OnMessage(SH_MsgNum, Func(This_Func_Name), 1000)		;1000 = MaxThreads / Func("Name_Of_Function") is recommended instead "Name_Of_Function" only

	if (Event == 4)		;"4" = HSHELL_WINDOWACTIVATED
	{
	WinGetClass, Class_Found, % "ahk_id" Hwnd
	
	if (Class_Found = "ConsoleWindowClass")
	gosub, Increase_Cmd_Counter
	}

}
Last edited by User on 08 Jan 2019, 23:10, edited 1 time in total.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

08 Jan 2019, 22:51

Shell Hook (Event Messages)

https://autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/#entry116488

1 = HSHELL_WINDOWCREATED
2 = HSHELL_WINDOWDESTROYED (Window hidden or destroyed - click here for more info)
3 = HSHELL_ACTIVATESHELLWINDOW

4 = HSHELL_WINDOWACTIVATED (A handle to the activated window - Click for more info)
32772 = HSHELL_RUDEAPPACTIVATED (A handle to the activated window - Click for more info)


5 = HSHELL_GETMINRECT
6 = HSHELL_REDRAW
7 = HSHELL_TASKMAN
8 = HSHELL_LANGUAGE
9 = HSHELL_SYSMENU

10 = HSHELL_ENDTASK
11 = HSHELL_ACCESSIBILITYSTATE
12 = HSHELL_APPCOMMAND
13 = HSHELL_WINDOWREPLACED
14 = HSHELL_WINDOWREPLACING
15 = HSHELL_HIGHBIT
16 = HSHELL_FLASH
17 = HSHELL_RUDEAPPACTIVATED

lParam differs in type according to the value of wParam (above codes) received.
For most of the wParam values, the lParam is a handle to a window (Hwnd)!

Simply put, when wParam is 12 ( HSHELL_APPCOMMAND ), the HiWord of lParam contains one of the following constants:

APPCOMMAND_BROWSER_BACKWARD = 1
APPCOMMAND_BROWSER_FORWARD = 2
APPCOMMAND_BROWSER_REFRESH = 3
APPCOMMAND_BROWSER_STOP = 4
APPCOMMAND_BROWSER_SEARCH = 5
APPCOMMAND_BROWSER_FAVORITES = 6
APPCOMMAND_BROWSER_HOME = 7
APPCOMMAND_VOLUME_MUTE = 8
APPCOMMAND_VOLUME_DOWN = 9
APPCOMMAND_VOLUME_UP = 10
APPCOMMAND_MEDIA_NEXTTRACK = 11
APPCOMMAND_MEDIA_PREVIOUSTRACK = 12
APPCOMMAND_MEDIA_STOP = 13
APPCOMMAND_MEDIA_PLAY_PAUSE = 14
APPCOMMAND_LAUNCH_MAIL = 15
APPCOMMAND_LAUNCH_MEDIA_SELECT = 16
APPCOMMAND_LAUNCH_APP1 = 17
APPCOMMAND_LAUNCH_APP2 = 18
APPCOMMAND_BASS_DOWN = 19
APPCOMMAND_BASS_BOOST = 20
APPCOMMAND_BASS_UP = 21
APPCOMMAND_TREBLE_DOWN = 22
APPCOMMAND_TREBLE_UP = 23
APPCOMMAND_MICROPHONE_VOLUME_MUTE = 24
APPCOMMAND_MICROPHONE_VOLUME_DOWN = 25
APPCOMMAND_MICROPHONE_VOLUME_UP = 26
APPCOMMAND_HELP = 27
APPCOMMAND_FIND = 28
APPCOMMAND_NEW = 29
APPCOMMAND_OPEN = 30
APPCOMMAND_CLOSE = 31
APPCOMMAND_SAVE = 32
APPCOMMAND_PRINT = 33
APPCOMMAND_UNDO = 34
APPCOMMAND_REDO = 35
APPCOMMAND_COPY = 36
APPCOMMAND_CUT = 37
APPCOMMAND_PASTE = 38
APPCOMMAND_REPLY_TO_MAIL = 39
APPCOMMAND_FORWARD_MAIL = 40
APPCOMMAND_SEND_MAIL = 41
APPCOMMAND_SPELL_CHECK = 42
APPCOMMAND_DICTATE_OR_COMMAND_CONTROL_TOGGLE = 43
APPCOMMAND_MIC_ON_OFF_TOGGLE = 44
APPCOMMAND_CORRECTION_LIST = 45
Last edited by User on 20 Jan 2019, 09:18, edited 2 times in total.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

10 Jan 2019, 12:34

In Line example for "On_WinType_Event()"

Code: Select all

gui, +AlwaysOnTop

gui, add, text, w50 vNotepad_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "NotePad" window is activated!

gui, add, text, xm w50 vPaint_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "Paint" window is activated!

gui, add, text, xm w50 vCmd_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "Cmd" window is activated!

gui, show

run, notepad
run, Mspaint
run, cmd

return

Increase_Notepad_Counter:	;________________________________________

if NotePad_Counter is not number
NotePad_Counter = 1

guicontrol, , Notepad_Counter, % NotePad_Counter++

On_Notepad_Active(Event, Hwnd)	;___
{
Static This_Func_Name := "On_Notepad_Active"


	;https://autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/#entry116488


;Local	;Force Local Mode (Un-Comment "Local" if supported by your version of AHK)

	;Gui +LastFound		;This Script "Gui 1" will be used ("gui 1" is automatically created from here)
	;hWnd := WinExist()	;stores the above "Gui 1" window id in "hwnd" variable

	;if the above "Gui 1" is destroyed, the "onMessage()" below will not work anymore

Static RunAtScriptExecution1 := DllCall( "RegisterShellHookWindow", UInt, A_ScriptHwnd)		;"A_ScriptHwnd" in use instead the above "hWnd" variable
Static SH_MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
Static RunAtScriptExecution2 := OnMessage(SH_MsgNum, Func(This_Func_Name), 1000)		;1000 = MaxThreads / Func("Name_Of_Function") is recommended instead "Name_Of_Function" only

	if (Event == 4)		;"4" = HSHELL_WINDOWACTIVATED
	{
	WinGetClass, Class_Found, % "ahk_id" Hwnd
	
	if (Class_Found = "Notepad")
	gosub, Increase_Notepad_Counter
	}

}

return

Increase_Paint_Counter:	;_______________________________________________

if Paint_Counter is not number
Paint_Counter = 1

guicontrol, , Paint_Counter, % Paint_Counter++

On_Paint_Active(Event, Hwnd)	;___
{
Static This_Func_Name := "On_Paint_Active"


	;https://autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/#entry116488


;Local	;Force Local Mode (Un-Comment "Local" if supported by your version of AHK)

	;Gui +LastFound		;This Script "Gui 1" will be used ("gui 1" is automatically created from here)
	;hWnd := WinExist()	;stores the above "Gui 1" window id in "hwnd" variable

	;if the above "Gui 1" is destroyed, the "onMessage()" below will not work anymore

Static RunAtScriptExecution1 := DllCall( "RegisterShellHookWindow", UInt, A_ScriptHwnd)		;"A_ScriptHwnd" in use instead the above "hWnd" variable
Static SH_MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
Static RunAtScriptExecution2 := OnMessage(SH_MsgNum, Func(This_Func_Name), 1000)		;1000 = MaxThreads / Func("Name_Of_Function") is recommended instead "Name_Of_Function" only

	if (Event == 4)		;"4" = HSHELL_WINDOWACTIVATED
	{
	WinGetClass, Class_Found, % "ahk_id" Hwnd
	
	if (Class_Found = "MsPaintApp")
	gosub, Increase_Paint_Counter
	}

}

return

Increase_Cmd_Counter:	;____________________________________________

if Cmd_Counter is not number
Cmd_Counter = 1

guicontrol, , Cmd_Counter, % Cmd_Counter++

On_Cmd_Active(Event, Hwnd)	;___
{
Static This_Func_Name := "On_Cmd_Active"


	;https://autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/#entry116488


;Local	;Force Local Mode (Un-Comment "Local" if supported by your version of AHK)

	;Gui +LastFound		;This Script "Gui 1" will be used ("gui 1" is automatically created from here)
	;hWnd := WinExist()	;stores the above "Gui 1" window id in "hwnd" variable

	;if the above "Gui 1" is destroyed, the "onMessage()" below will not work anymore

Static RunAtScriptExecution1 := DllCall( "RegisterShellHookWindow", UInt, A_ScriptHwnd)		;"A_ScriptHwnd" in use instead the above "hWnd" variable
Static SH_MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
Static RunAtScriptExecution2 := OnMessage(SH_MsgNum, Func(This_Func_Name), 1000)		;1000 = MaxThreads / Func("Name_Of_Function") is recommended instead "Name_Of_Function" only

	if (Event == 4)		;"4" = HSHELL_WINDOWACTIVATED
	{
	WinGetClass, Class_Found, % "ahk_id" Hwnd
	
	if (Class_Found = "ConsoleWindowClass")
	gosub, Increase_Cmd_Counter
	}

}

return

GuiClose:	;______________________________________________
ExitApp
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

10 Jan 2019, 20:33

Hide \ Destroy Event

97_ z_ image.png
97_ z_ image.png (20.08 KiB) Viewed 3653 times

Code: Select all

gui, +AlwaysOnTop

gui, add, text, w50 vNotepad_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "NotePad" window is activated!

gui, add, text, xm w50 vPaint_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "Paint" window is activated!

gui, add, text, xm w50 vCmd_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "Cmd" window is activated!

;_

gui, add, text, xm y+30 w50 vHide_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a window is Hidden!

gui, add, text, xm w50 vDestroy_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a window is Destroyed!

;_

gui, show

run, notepad
run, Mspaint
run, cmd

XButton := "Destroy"

loop,6
{

	if (a_index = 4)
	{
	Name := "x"

	XButton := "Hide"
	}

gui, % a_index Name "Test:default"

gui, % "+LabelGuiTest" Name 

gui, add, button, gExecute, Hide

gui, add, button, gExecute, Destroy

gui, add, text, , % "Window ''X'' button = " XButton

gui, show, w300 h200, % a_index Name "Test"

}

Execute()
{
if (a_guicontrol = "hide")
gui, hide
else
gui, destroy
}

GuiTestClose()
{
gui, destroy
}

return

Increase_Notepad_Counter:	;________________________

if NotePad_Counter is not number
NotePad_Counter = 1

guicontrol, , Notepad_Counter, % NotePad_Counter++

return

Increase_Paint_Counter:	;________________________

if Paint_Counter is not number
Paint_Counter = 1

guicontrol, , Paint_Counter, % Paint_Counter++

return

Increase_Cmd_Counter:	;________________________

if Cmd_Counter is not number
Cmd_Counter = 1

guicontrol, , Cmd_Counter, % Cmd_Counter++

return

Increase_Hide_Counter:	;________________________

if Hide_Counter is not number
Hide_Counter = 1

guicontrol, , Hide_Counter, % Hide_Counter++

return

Increase_Destroy_Counter:	;________________________

if Destroy_Counter is not number
Destroy_Counter = 1

guicontrol, , Destroy_Counter, % Destroy_Counter++

return

GuiClose:	;______________________________
ExitApp


OnWin(Event, Hwnd)	;_____________________________________________
{
Static This_Func_Name := "OnWin"


	;https://autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/#entry116488


;Local	;Force Local Mode (Un-Comment "Local" if supported by your version of AHK)

	;Gui +LastFound		;This Script "Gui 1" will be used ("gui 1" is automatically created from here)
	;hWnd := WinExist()	;stores the above "Gui 1" window id in "hwnd" variable

	;if the above "Gui 1" is destroyed, the "onMessage()" below will not work anymore

Static RunAtScriptExecution1 := DllCall( "RegisterShellHookWindow", UInt, A_ScriptHwnd)		;"A_ScriptHwnd" in use instead the above "hWnd" variable
Static SH_MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
Static RunAtScriptExecution2 := OnMessage(SH_MsgNum, Func(This_Func_Name), 1000)		;1000 = MaxThreads / Func("Name_Of_Function") is recommended instead "Name_Of_Function" only

	if (Event == 4)		;"4" = HSHELL_WINDOWACTIVATED
	{
	WinGetClass, Class_Found, % "ahk_id" Hwnd
	
	if (Class_Found = "Notepad")
	gosub, Increase_Notepad_Counter

	if (Class_Found = "MsPaintApp")
	gosub, Increase_Paint_Counter

	if (Class_Found = "ConsoleWindowClass")
	gosub, Increase_Cmd_Counter
	}

	if (Event == "2")		;"2", a window was hidden or destroyed (HSHELL_WINDOWDESTROYED)
	{
	T_DetectHiddenWindows := A_DetectHiddenWindows
	DetectHiddenWindows, On

	if WinExist("ahk_id" Hwnd)
	Window_Hidden := 1

	DetectHiddenWindows, % T_DetectHiddenWindows

		if (Window_Hidden = 1)
		{
			;msgbox, % "Window " Hwnd " / " Format("{1:#x}", Hwnd) " was hidden!"

		gosub, Increase_Hide_Counter
		}
		else
		{
			;msgbox, % "Window " Hwnd " / " Format("{1:#x}", Hwnd) " was destroyed!"

		gosub, Increase_Destroy_Counter
		}

	}

}
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

16 Jan 2019, 12:05

murataygun wrote:
16 Jan 2019, 09:56
Didnt worked on windows 10
AHK version? 32\64 bit? Unicode\Ansi?
murataygun
Posts: 26
Joined: 07 Aug 2015, 15:53

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

16 Jan 2019, 12:08

AHK : 1.1.30.1 Unicode 64 bit.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

16 Jan 2019, 12:09

murataygun wrote:
16 Jan 2019, 12:08
AHK : 1.1.30.1 Unicode 64 bit.
error message?
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

16 Jan 2019, 12:26

murataygun wrote:
16 Jan 2019, 12:08
AHK : 1.1.30.1 Unicode 64 bit.
AHK_L 1.1.30.1 Unicode 64 bit - Win 7 Professional 64 bit (No issues found!)

Sorry, I can't test on windows 10!
murataygun
Posts: 26
Joined: 07 Aug 2015, 15:53

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

16 Jan 2019, 12:31

Code: Select all

    if (Event == 4)     ;"4" = HSHELL_WINDOWACTIVATED
    {
	(Here is not invoking) (This part is not invoking)
}
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

16 Jan 2019, 12:35

murataygun wrote:
16 Jan 2019, 12:31
I don't know, did you change the original code or something?
A_AhkUser
Posts: 1078
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

16 Jan 2019, 12:57

Hi User, hi murataygun,

Maybe:

Code: Select all

if ((event = 32772) || (event = 4)) {
	(This part should invoking)
}
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

16 Jan 2019, 13:54

A_AhkUser wrote:
16 Jan 2019, 12:57
if ((event = 32772) || (event = 4)) {

Hehe, Windows and its Oddities!

It seems that if any application is in "FullScreen" Mode, windows sends "32772" instead "4" when any other window is activated!

HSHELL_WINDOWACTIVATED = 4 (A handle to the activated window)

HSHELL_RUDEAPPACTIVATED = 32772 (A handle to the activated window)

https://stackoverflow.com/questions/117 ... pactivated

https://docs.microsoft.com/en-us/window ... hookwindow

https://msdn.microsoft.com/en-us/librar ... s.85).aspx

Thanks for sharing!
murataygun
Posts: 26
Joined: 07 Aug 2015, 15:53

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

16 Jan 2019, 16:34

User wrote:
16 Jan 2019, 13:54
A_AhkUser wrote:
16 Jan 2019, 12:57
if ((event = 32772) || (event = 4)) {

Hehe, Windows and its Oddities!

It seems that if any application is in "FullScreen" Mode, windows sends "32772" instead "4" when any other window is activated!

HSHELL_WINDOWACTIVATED = 4 (A handle to the activated window)

HSHELL_RUDEAPPACTIVATED = 32772 (A handle to the activated window)

https://stackoverflow.com/questions/117 ... pactivated

https://docs.microsoft.com/en-us/window ... hookwindow

https://msdn.microsoft.com/en-us/librar ... s.85).aspx

Thanks for sharing!
Now it works perfectly. Thank you both. :angel:
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

20 Jan 2019, 09:57

murataygun wrote:
16 Jan 2019, 16:34
Now it works perfectly. Thank you both. :angel:

You Welcome!

HSHELL_RUDEAPPACTIVATED = 32772 (Example below!)

Code: Select all

gui, +AlwaysOnTop

gui, add, text, w50 vNotepad_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "NotePad" window is activated!

gui, add, text, xm w50 vPaint_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "Paint" window is activated!

gui, add, text, xm w50 vCmd_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a "Cmd" window is activated!

;_

gui, add, text, xm y+30 w50 vHide_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a window is Hidden!

gui, add, text, xm w50 vDestroy_Counter +border center, 0
gui, add, text, x+5, The "counter" increases every time a window is Destroyed!

;_

gui, show

run, notepad
run, Mspaint
run, cmd

XButton := "Destroy"

loop,6
{

	if (a_index = 4)
	{
	Name := "x"

	XButton := "Hide"
	}

gui, % a_index Name "Test:default"

gui, % "+LabelGuiTest" Name 

gui, add, button, gExecute, Hide

gui, add, button, gExecute, Destroy

gui, add, text, , % "Window ''X'' button = " XButton

gui, show, w300 h200, % a_index Name "Test"

}

Execute()
{
if (a_guicontrol = "hide")
gui, hide
else
gui, destroy
}

GuiTestClose()
{
gui, destroy
}

return

Increase_Notepad_Counter:	;________________________

if NotePad_Counter is not number
NotePad_Counter = 1

guicontrol, , Notepad_Counter, % NotePad_Counter++

return

Increase_Paint_Counter:	;________________________

if Paint_Counter is not number
Paint_Counter = 1

guicontrol, , Paint_Counter, % Paint_Counter++

return

Increase_Cmd_Counter:	;________________________

if Cmd_Counter is not number
Cmd_Counter = 1

guicontrol, , Cmd_Counter, % Cmd_Counter++

return

Increase_Hide_Counter:	;________________________

if Hide_Counter is not number
Hide_Counter = 1

guicontrol, , Hide_Counter, % Hide_Counter++

return

Increase_Destroy_Counter:	;________________________

if Destroy_Counter is not number
Destroy_Counter = 1

guicontrol, , Destroy_Counter, % Destroy_Counter++

return

GuiClose:	;______________________________
ExitApp


OnWin(Event, Hwnd)	;_____________________________________________
{
Static This_Func_Name := "OnWin"


	;https://autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/#entry116488


;Local	;Force Local Mode (Un-Comment "Local" if supported by your version of AHK)

	;Gui +LastFound		;This Script "Gui 1" will be used ("gui 1" is automatically created from here)
	;hWnd := WinExist()	;stores the above "Gui 1" window id in "hwnd" variable

	;if the above "Gui 1" is destroyed, the "onMessage()" below will not work anymore

Static RunAtScriptExecution1 := DllCall( "RegisterShellHookWindow", UInt, A_ScriptHwnd)		;"A_ScriptHwnd" in use instead the above "hWnd" variable
Static SH_MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
Static RunAtScriptExecution2 := OnMessage(SH_MsgNum, Func(This_Func_Name), 1000)		;1000 = MaxThreads / Func("Name_Of_Function") is recommended instead "Name_Of_Function" only

	if (Event == 4 or Event == 32772)	;4 = HSHELL_WINDOWACTIVATED / 32772 = HSHELL_RUDEAPPACTIVATED
	{
	WinGetClass, Class_Found, % "ahk_id" Hwnd
	
	if (Class_Found = "Notepad")
	gosub, Increase_Notepad_Counter

	if (Class_Found = "MsPaintApp")
	gosub, Increase_Paint_Counter

	if (Class_Found = "ConsoleWindowClass")
	gosub, Increase_Cmd_Counter
	}

	if (Event == "2")		;"2", a window was hidden or destroyed (HSHELL_WINDOWDESTROYED)
	{
	T_DetectHiddenWindows := A_DetectHiddenWindows
	DetectHiddenWindows, On

	if WinExist("ahk_id" Hwnd)
	Window_Hidden := 1

	DetectHiddenWindows, % T_DetectHiddenWindows

		if (Window_Hidden = 1)
		{
			;msgbox, % "Window " Hwnd " / " Format("{1:#x}", Hwnd) " was hidden!"

		gosub, Increase_Hide_Counter
		}
		else
		{
			;msgbox, % "Window " Hwnd " / " Format("{1:#x}", Hwnd) " was destroyed!"

		gosub, Increase_Destroy_Counter
		}

	}

}
burque505
Posts: 1312
Joined: 22 Jan 2017, 19:37

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

25 Jan 2019, 17:34

@User, this is really interesting. Thank you for sharing it.

Do you have any idea how it could be adapted to determine when MsgBox windows appear?

Neither "Event == 4" (no surprise there) or "Event == 32772" seem to pick that up.

By the way, check out "ShellHook Messages" in this old post by Serenity.
ShellHook Messages.gif
ShellHook Messages.gif (42.27 KiB) Viewed 3307 times
Regards,
burque505.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: [Functions] - OnWin() - On_WinType_Event() - etc (SHELL HOOK)

25 Jan 2019, 23:23

burque505 wrote:
25 Jan 2019, 17:34
Well, it seems that "#32770" is the class name for message boxes (at least for AHk's msgboxes)!

something like:

if event 1
get winclass
if winclass #32770
do this

Return to “Scripts and Functions”

Who is online

Users browsing this forum: elvo11 and 97 guests