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:
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
}
}