How to appear a msgbox when I activate specific window?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

How to appear a msgbox when I activate specific window?

18 Nov 2018, 14:08

Hello friends..


I have to work on a specific software (Any Video Converter Ultimate) and I want to automate some stuff in this software, so I want that whenever I activate/maximize this software's window it should automate that staff. Please look at this screen shot-

Image


As you can see that, the software (Any Video Converter Ultimate) is minimized on my task bar and I can maximize this software either by left clicking on its minimized window or by pressing alt+tab. So my present codes are-
(The following codes are fictitious/dummy only for demonstration purpose)

Code: Select all

~LButton::
~!tab::
Sleep 500
WinGetActiveTitle, Active_Window
if (Active_Window <> "Any Video Converter Ultimate")
var:= 1 
if (Active_Window = "Any Video Converter Ultimate") && (var = 1)
{
 MsgBox, *** You activated %Active_Window% ***
var:= ""
}
return
The above codes are not much compatible, however they are working fine, but, they are improvised. I want some stable and more compatible codes for this. I think it can more professionally and compatibly be done by some other way, but don't know how?

Please help me..

Thanks a lot..
I don't normally code as I don't code normally.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: How to appear a msgbox when I activate specific window?

18 Nov 2018, 16:18

Hello Sebastian Caine,

Here's one idea:

Code: Select all

#NoEnv
#SingleInstance force
CoordMode, ToolTip, Screen
#Warn


Gui +HWNDID
DllCall("RegisterShellHookWindow", "UInt", ID)
msgNum := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK")
OnMessage(msgNum, "ShellMessage")
return ; en of the auto-execute part of the script

ShellMessage(_wParam, _lParam) {
	if (_wParam = 32772) {
		_ID := _lParam
		WinGetTitle, _title, % "ahk_id " . _ID
		ToolTip % _title, 0, 0
	}
}
see also: [How to] Hook on to Shell to receive its messages?

[EDIT] If you only need to handle the activation of a specific kind of window (i.e. ahk_exe, ahk_class), you can use WinWait[Not]Active:

Code: Select all

title := "ahk_class Notepad"
SetTimer, check, -1
return

check:
	WinWaitActive % title
	MsgBox, TEST
	WinWaitNotActive % title
	SetTimer,, -1
return
my scripts
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to appear a msgbox when I activate specific window?

19 Nov 2018, 12:25

A_AhkUser wrote:
18 Nov 2018, 16:18
Hello Sebastian Caine,

Here's one idea:

Code: Select all

#NoEnv
#SingleInstance force
CoordMode, ToolTip, Screen
#Warn


Gui +HWNDID
DllCall("RegisterShellHookWindow", "UInt", ID)
msgNum := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK")
OnMessage(msgNum, "ShellMessage")
return ; en of the auto-execute part of the script

ShellMessage(_wParam, _lParam) {
	if (_wParam = 32772) {
		_ID := _lParam
		WinGetTitle, _title, % "ahk_id " . _ID
		ToolTip % _title, 0, 0
	}
}
see also: [How to] Hook on to Shell to receive its messages?

[EDIT] If you only need to handle the activation of a specific kind of window (i.e. ahk_exe, ahk_class), you can use WinWait[Not]Active:

Code: Select all

title := "ahk_class Notepad"
SetTimer, check, -1
return

check:
	WinWaitActive % title
	MsgBox, TEST
	WinWaitNotActive % title
	SetTimer,, -1
return


Thanks a lot dear A_AhkUser for your kind reply... you are really awesome..


sir, these codes are not working at all-

Code: Select all

#NoEnv
#SingleInstance force
CoordMode, ToolTip, Screen
#Warn


Gui +HWNDID
DllCall("RegisterShellHookWindow", "UInt", ID)
msgNum := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK")
OnMessage(msgNum, "ShellMessage")
return ; en of the auto-execute part of the script

ShellMessage(_wParam, _lParam) {
	if (_wParam = 32772) {
		_ID := _lParam
		WinGetTitle, _title, % "ahk_id " . _ID
		ToolTip % _title, 0, 0
	}
}
Please tell me how to make them work???

However, your these codes are working-

Code: Select all

title := "ahk_class Notepad"
SetTimer, check, -1
return

check:
	WinWaitActive % title
	MsgBox, TEST
	WinWaitNotActive % title
	SetTimer,, -1
return
Thanks a lot sir...
I don't normally code as I don't code normally.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: How to appear a msgbox when I activate specific window?

19 Nov 2018, 19:54

Here is a class I created called WinHook that makes working with Window Shell Hooks and Window Event Hooks much easier.

Code: Select all

WinHook.Shell.Add("Created",,, "NOTEPAD.EXE",1) ; Notepad Window Created

Created(Win_Hwnd, Win_Title, Win_Class, Win_Exe, Win_Event)
{
	MsgBox Created
	WinGet, PID, PID, ahk_id %Win_Hwnd%
	WinHook.Event.Add(0x0016, 0x0016, "Minimized", PID) 
	WinHook.Event.Add(0x0017, 0x0017, "Restored", PID) 
}

Minimized(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
{
	MsgBox Minimized
}

Restored(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
{
	MsgBox Restored
}

; [Class] WinHook
; Fanatic Guru
; 2018 10 16
;
; Class to set hooks of windows or processes
;
;{-----------------------------------------------
;	
;	Class (Nested):		WinHook.Shell
;
;		Method:
; 			Add(Func, wTitle:="", wClass:="", wExe:="", Event:=1)
;
;		Desc: Add Shell Hook
;
;   	Parameters:
;		   	1) {Func}			Function name or Function object to call on event
;   		2) {wTitle}		window Title to watch for event (default = "", all windows)
;   		3) {wClass}		window Class to watch for event (default = "", all windows)
;   		4) {wExe}			window Exe to watch for event (default = "", all windows)
;   		5) {Event}			Event (default = 1, window created)
;
;				Shell Hook Events:
;				1 = HSHELL_WINDOWCREATED
;				2 = HSHELL_WINDOWDESTROYED
;				3 = HSHELL_ACTIVATESHELLWINDOW
;				4 = HSHELL_WINDOWACTIVATED
;				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
;
;		Note: ObjBindMethod(obj, Method) can be used to create a function object to a class method
;					WinHook.Shell.Add(ObjBindMethod(TestClass.TestNestedClass, "MethodName"), wTitle, wClass, wExe, Event)
;
; ----------
;
;		Desc: Function Called on Event
;			FuncOrMethod(Win_Hwnd, Win_Title, Win_Class, Win_Exe, Win_Event)
;		
;		Parameters:
;		   	1) {Win_Hwnd}		window handle ID of window with event 
;   		2) {Win_Title}		window Title of window with event
;   		3) {Win_Class}		window Class of window with event
;   		4) {Win_Exe}			window Exe of window with event
;   		5) {Win_Event}		window Event
;
;}
;	Class (Nested):		WinHook.Event
;
;		Method:
;			Add(eventMin, eventMax, eventProc, idProcess, WinTitle := "") 			
;
;		Desc: Add Event Hook
;
;   	Parameters:
;		   	1) {eventMin}		lowest Event value handled by the hook function
;   		2) {eventMax}		highest event value handled by the hook function
;   		3) {eventProc}		event hook function, call be function name or function object
;   		4) {idProcess}		ID of the process from which the hook function receives events
;   		5) {WinTitle}			WinTitle to identify which windows to operate on, (default = "", all windows)
;
;				Event Hook Events:
;				0x8012 = EVENT_OBJECT_ACCELERATORCHANGE
;				0x8017 = EVENT_OBJECT_CLOAKED
;				0x8015 = EVENT_OBJECT_CONTENTSCROLLED
;				0x8000 = EVENT_OBJECT_CREATE
;				0x8011 = EVENT_OBJECT_DEFACTIONCHANGE
;				0x800D = EVENT_OBJECT_DESCRIPTIONCHANGE
;				0x8001 = EVENT_OBJECT_DESTROY
;				0x8021 = EVENT_OBJECT_DRAGSTART
;				0x8022 = EVENT_OBJECT_DRAGCANCEL
;				0x8023 = EVENT_OBJECT_DRAGCOMPLETE
;				0x8024 = EVENT_OBJECT_DRAGENTER
;				0x8025 = EVENT_OBJECT_DRAGLEAVE
;				0x8026 = EVENT_OBJECT_DRAGDROPPED
;				0x80FF = EVENT_OBJECT_END
;				0x8005 = EVENT_OBJECT_FOCUS
;				0x8010  = EVENT_OBJECT_HELPCHANGE
;				0x8003 = EVENT_OBJECT_HIDE
;				0x8020 = EVENT_OBJECT_HOSTEDOBJECTSINVALIDATED
;				0x8028 = EVENT_OBJECT_IME_HIDE
;				0x8027 = EVENT_OBJECT_IME_SHOW
;				0x8029 = EVENT_OBJECT_IME_CHANGE
;				0x8013 = EVENT_OBJECT_INVOKED
;				0x8019 = EVENT_OBJECT_LIVEREGIONCHANGED
;				0x800B = EVENT_OBJECT_LOCATIONCHANGE
;				0x800C = EVENT_OBJECT_NAMECHANGE
;				0x800F = EVENT_OBJECT_PARENTCHANGE
;				0x8004 = EVENT_OBJECT_REORDER
;				0x8006 = EVENT_OBJECT_SELECTION
;				0x8007 = EVENT_OBJECT_SELECTIONADD
;				0x8008 = EVENT_OBJECT_SELECTIONREMOVE
;				0x8009 = EVENT_OBJECT_SELECTIONWITHIN
;				0x8002 = EVENT_OBJECT_SHOW
;				0x800A = EVENT_OBJECT_STATECHANGE
;				0x8030 = EVENT_OBJECT_TEXTEDIT_CONVERSIONTARGETCHANGED
;				0x8014 = EVENT_OBJECT_TEXTSELECTIONCHANGED
;				0x8018 = EVENT_OBJECT_UNCLOAKED
;				0x800E = EVENT_OBJECT_VALUECHANGE
;				0x0002 = EVENT_SYSTEM_ALERT
;				0x8016 = EVENT_SYSTEM_ARRANGMENTPREVIEW
;				0x0009 = EVENT_SYSTEM_CAPTUREEND
;				0x0008 = EVENT_SYSTEM_CAPTURESTART
;				0x000D = EVENT_SYSTEM_CONTEXTHELPEND
;				0x000C = EVENT_SYSTEM_CONTEXTHELPSTART
;				0x0020 = EVENT_SYSTEM_DESKTOPSWITCH
;				0x0011 = EVENT_SYSTEM_DIALOGEND
;				0x0010 = EVENT_SYSTEM_DIALOGSTART
;				0x000F = EVENT_SYSTEM_DRAGDROPEND
;				0x000E = EVENT_SYSTEM_DRAGDROPSTART
;				0x00FF = EVENT_SYSTEM_END
;				0x0003 = EVENT_SYSTEM_FOREGROUND
;				0x0007 = EVENT_SYSTEM_MENUPOPUPEND
;				0x0006 = EVENT_SYSTEM_MENUPOPUPSTART
;				0x0005 = EVENT_SYSTEM_MENUEND
;				0x0004 = EVENT_SYSTEM_MENUSTART
;				0x0017 = EVENT_SYSTEM_MINIMIZEEND
;				0x0016 = EVENT_SYSTEM_MINIMIZESTART
;				0x000B = EVENT_SYSTEM_MOVESIZEEND
;				0x000A = EVENT_SYSTEM_MOVESIZESTART
;				0x0013 = EVENT_SYSTEM_SCROLLINGEND
;				0x0012 = EVENT_SYSTEM_SCROLLINGSTART
;				0x0001 = EVENT_SYSTEM_SOUND
;				0x0015 = EVENT_SYSTEM_SWITCHEND
;				0x0014 = EVENT_SYSTEM_SWITCHSTART
;	   
class WinHook
{
	class Shell
	{
		Add(Func, wTitle:="", wClass:="", wExe:="", Event:=1)
		{
			if !WinHook.Shell.Hooks
			{
				WinHook.Shell.Hooks := {}, WinHook.Shell.Events := {}
				DllCall("RegisterShellHookWindow", UInt, A_ScriptHwnd)
				MsgNum := DllCall("RegisterWindowMessage", Str, "SHELLHOOK")
				OnMessage(MsgNum, ObjBindMethod(WinHook.Shell, "Message"))
			}
			if !IsObject(Func)
				Func := Func(Func)
			WinHook.Shell.Hooks.Push({Func: Func, Title: wTitle, Class: wClass, Exe: wExe, Event: Event})
			WinHook.Shell.Events[Event] := true
			return
		}
		Deregister()
		{
			DllCall("DeregisterShellHookWindow", UInt, A_ScriptHwnd)
		}
		Message(Event, Hwnd)  ; Private Method
		{
			If WinHook.Shell.Events[Event] 
			{
				WinGetTitle, wTitle, ahk_id %Hwnd%
				WinGetClass, wClass, ahk_id %Hwnd%
				WinGet, wExe, ProcessName, ahk_id %Hwnd%
				for key, Hook in WinHook.Shell.Hooks
					if ((Hook.Title = wTitle or Hook.Title = "") and (Hook.Class = wClass or Hook.Class = "") and (Hook.Exe = wExe or Hook.Exe = "") and (Hook.Event = Event))
						Hook.Func.Call(Hwnd, wTitle, wClass, wExe, Event)
			}
		}
	}
	class Event
	{
		Add(eventMin, eventMax, eventProc, idProcess, WinTitle := "")
		{
			if !WinHook.Event.Hooks
			{
				WinHook.Event.Hooks := {}
				OnExit(ObjBindMethod(WinHook.Event, "UnHookAll"))
			}
			Hook := DllCall("SetWinEventHook"
				, "UInt",	eventMin								;  UINT eventMin
				, "UInt",	eventMax								;  UINT eventMax
				, "Ptr" ,	0x0											;  HMODULE hmodWinEventProc
				, "Ptr" ,	RegisterCallback(WinHook.Event.Message)	;  WINEVENTPROC lpfnWinEventProc
				, "UInt" ,	idProcess								;  DWORD idProcess
				, "UInt",	0x0											;  DWORD idThread
				, "UInt",	0x0|0x2)  								;  UINT dwflags, OutOfContext|SkipOwnProcess
			if !IsObject(eventProc)
				eventProc := Func(eventProc)
			WinHook.Event.Hooks[Hook] := {eventMin: eventMin, eventMax: eventMax, eventProc: eventProc, idProcess: idProcess, WinTitle: WinTitle}
		}
		UnHook(idProcess)
		{
			for key, Hook in WinHook.Event.Hooks
				if (Hook.idProcess = idProcess)
					DllCall("UnhookWinEvent", "Ptr",Hook.Handle)
		}
		UnHookAll()
		{
			for key, Hook in WinHook.Event.Hooks
				DllCall("UnhookWinEvent", "Ptr",Hook.Handle)
		}
		Message(event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)  ; 'Private Method 
		{
			Hook := WinHook.Event.Hooks[hWinEventHook := this] ; this' is hidden param1 because method is called as func
			WinGet, List, List, % Hook.WinTitle
			Loop % List
				if  (List%A_Index% = hwnd)
				{
					Hook.eventProc.Call(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
					return
				}
		}
	}
}
The WinHook.Shell.Add sets a shell hook to watch for a notepad window being created. When it detects one, it then calls the Created function. This function uses WinHook.Event.Add to set two event hooks to watch for that notepad window being minimized or restored and calls the designated function.

The WinHook class allows you to setup any Window Shell Hook or Window Event Hook pretty easily. Between those two types of hooks many useful actions can be detected.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to appear a msgbox when I activate specific window?

21 Nov 2018, 00:15

FanaticGuru wrote:
19 Nov 2018, 19:54
Here is a class I created called WinHook that makes working with Window Shell Hooks and Window Event Hooks much easier.

Code: Select all

WinHook.Shell.Add("Created",,, "NOTEPAD.EXE",1) ; Notepad Window Created

Created(Win_Hwnd, Win_Title, Win_Class, Win_Exe, Win_Event)
{
	MsgBox Created
	WinGet, PID, PID, ahk_id %Win_Hwnd%
	WinHook.Event.Add(0x0016, 0x0016, "Minimized", PID) 
	WinHook.Event.Add(0x0017, 0x0017, "Restored", PID) 
}

Minimized(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
{
	MsgBox Minimized
}

Restored(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
{
	MsgBox Restored
}

; [Class] WinHook
; Fanatic Guru
; 2018 10 16
;
; Class to set hooks of windows or processes
;
;{-----------------------------------------------
;	
;	Class (Nested):		WinHook.Shell
;
;		Method:
; 			Add(Func, wTitle:="", wClass:="", wExe:="", Event:=1)
;
;		Desc: Add Shell Hook
;
;   	Parameters:
;		   	1) {Func}			Function name or Function object to call on event
;   		2) {wTitle}		window Title to watch for event (default = "", all windows)
;   		3) {wClass}		window Class to watch for event (default = "", all windows)
;   		4) {wExe}			window Exe to watch for event (default = "", all windows)
;   		5) {Event}			Event (default = 1, window created)
;
;				Shell Hook Events:
;				1 = HSHELL_WINDOWCREATED
;				2 = HSHELL_WINDOWDESTROYED
;				3 = HSHELL_ACTIVATESHELLWINDOW
;				4 = HSHELL_WINDOWACTIVATED
;				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
;
;		Note: ObjBindMethod(obj, Method) can be used to create a function object to a class method
;					WinHook.Shell.Add(ObjBindMethod(TestClass.TestNestedClass, "MethodName"), wTitle, wClass, wExe, Event)
;
; ----------
;
;		Desc: Function Called on Event
;			FuncOrMethod(Win_Hwnd, Win_Title, Win_Class, Win_Exe, Win_Event)
;		
;		Parameters:
;		   	1) {Win_Hwnd}		window handle ID of window with event 
;   		2) {Win_Title}		window Title of window with event
;   		3) {Win_Class}		window Class of window with event
;   		4) {Win_Exe}			window Exe of window with event
;   		5) {Win_Event}		window Event
;
;}
;	Class (Nested):		WinHook.Event
;
;		Method:
;			Add(eventMin, eventMax, eventProc, idProcess, WinTitle := "") 			
;
;		Desc: Add Event Hook
;
;   	Parameters:
;		   	1) {eventMin}		lowest Event value handled by the hook function
;   		2) {eventMax}		highest event value handled by the hook function
;   		3) {eventProc}		event hook function, call be function name or function object
;   		4) {idProcess}		ID of the process from which the hook function receives events
;   		5) {WinTitle}			WinTitle to identify which windows to operate on, (default = "", all windows)
;
;				Event Hook Events:
;				0x8012 = EVENT_OBJECT_ACCELERATORCHANGE
;				0x8017 = EVENT_OBJECT_CLOAKED
;				0x8015 = EVENT_OBJECT_CONTENTSCROLLED
;				0x8000 = EVENT_OBJECT_CREATE
;				0x8011 = EVENT_OBJECT_DEFACTIONCHANGE
;				0x800D = EVENT_OBJECT_DESCRIPTIONCHANGE
;				0x8001 = EVENT_OBJECT_DESTROY
;				0x8021 = EVENT_OBJECT_DRAGSTART
;				0x8022 = EVENT_OBJECT_DRAGCANCEL
;				0x8023 = EVENT_OBJECT_DRAGCOMPLETE
;				0x8024 = EVENT_OBJECT_DRAGENTER
;				0x8025 = EVENT_OBJECT_DRAGLEAVE
;				0x8026 = EVENT_OBJECT_DRAGDROPPED
;				0x80FF = EVENT_OBJECT_END
;				0x8005 = EVENT_OBJECT_FOCUS
;				0x8010  = EVENT_OBJECT_HELPCHANGE
;				0x8003 = EVENT_OBJECT_HIDE
;				0x8020 = EVENT_OBJECT_HOSTEDOBJECTSINVALIDATED
;				0x8028 = EVENT_OBJECT_IME_HIDE
;				0x8027 = EVENT_OBJECT_IME_SHOW
;				0x8029 = EVENT_OBJECT_IME_CHANGE
;				0x8013 = EVENT_OBJECT_INVOKED
;				0x8019 = EVENT_OBJECT_LIVEREGIONCHANGED
;				0x800B = EVENT_OBJECT_LOCATIONCHANGE
;				0x800C = EVENT_OBJECT_NAMECHANGE
;				0x800F = EVENT_OBJECT_PARENTCHANGE
;				0x8004 = EVENT_OBJECT_REORDER
;				0x8006 = EVENT_OBJECT_SELECTION
;				0x8007 = EVENT_OBJECT_SELECTIONADD
;				0x8008 = EVENT_OBJECT_SELECTIONREMOVE
;				0x8009 = EVENT_OBJECT_SELECTIONWITHIN
;				0x8002 = EVENT_OBJECT_SHOW
;				0x800A = EVENT_OBJECT_STATECHANGE
;				0x8030 = EVENT_OBJECT_TEXTEDIT_CONVERSIONTARGETCHANGED
;				0x8014 = EVENT_OBJECT_TEXTSELECTIONCHANGED
;				0x8018 = EVENT_OBJECT_UNCLOAKED
;				0x800E = EVENT_OBJECT_VALUECHANGE
;				0x0002 = EVENT_SYSTEM_ALERT
;				0x8016 = EVENT_SYSTEM_ARRANGMENTPREVIEW
;				0x0009 = EVENT_SYSTEM_CAPTUREEND
;				0x0008 = EVENT_SYSTEM_CAPTURESTART
;				0x000D = EVENT_SYSTEM_CONTEXTHELPEND
;				0x000C = EVENT_SYSTEM_CONTEXTHELPSTART
;				0x0020 = EVENT_SYSTEM_DESKTOPSWITCH
;				0x0011 = EVENT_SYSTEM_DIALOGEND
;				0x0010 = EVENT_SYSTEM_DIALOGSTART
;				0x000F = EVENT_SYSTEM_DRAGDROPEND
;				0x000E = EVENT_SYSTEM_DRAGDROPSTART
;				0x00FF = EVENT_SYSTEM_END
;				0x0003 = EVENT_SYSTEM_FOREGROUND
;				0x0007 = EVENT_SYSTEM_MENUPOPUPEND
;				0x0006 = EVENT_SYSTEM_MENUPOPUPSTART
;				0x0005 = EVENT_SYSTEM_MENUEND
;				0x0004 = EVENT_SYSTEM_MENUSTART
;				0x0017 = EVENT_SYSTEM_MINIMIZEEND
;				0x0016 = EVENT_SYSTEM_MINIMIZESTART
;				0x000B = EVENT_SYSTEM_MOVESIZEEND
;				0x000A = EVENT_SYSTEM_MOVESIZESTART
;				0x0013 = EVENT_SYSTEM_SCROLLINGEND
;				0x0012 = EVENT_SYSTEM_SCROLLINGSTART
;				0x0001 = EVENT_SYSTEM_SOUND
;				0x0015 = EVENT_SYSTEM_SWITCHEND
;				0x0014 = EVENT_SYSTEM_SWITCHSTART
;	   
class WinHook
{
	class Shell
	{
		Add(Func, wTitle:="", wClass:="", wExe:="", Event:=1)
		{
			if !WinHook.Shell.Hooks
			{
				WinHook.Shell.Hooks := {}, WinHook.Shell.Events := {}
				DllCall("RegisterShellHookWindow", UInt, A_ScriptHwnd)
				MsgNum := DllCall("RegisterWindowMessage", Str, "SHELLHOOK")
				OnMessage(MsgNum, ObjBindMethod(WinHook.Shell, "Message"))
			}
			if !IsObject(Func)
				Func := Func(Func)
			WinHook.Shell.Hooks.Push({Func: Func, Title: wTitle, Class: wClass, Exe: wExe, Event: Event})
			WinHook.Shell.Events[Event] := true
			return
		}
		Deregister()
		{
			DllCall("DeregisterShellHookWindow", UInt, A_ScriptHwnd)
		}
		Message(Event, Hwnd)  ; Private Method
		{
			If WinHook.Shell.Events[Event] 
			{
				WinGetTitle, wTitle, ahk_id %Hwnd%
				WinGetClass, wClass, ahk_id %Hwnd%
				WinGet, wExe, ProcessName, ahk_id %Hwnd%
				for key, Hook in WinHook.Shell.Hooks
					if ((Hook.Title = wTitle or Hook.Title = "") and (Hook.Class = wClass or Hook.Class = "") and (Hook.Exe = wExe or Hook.Exe = "") and (Hook.Event = Event))
						Hook.Func.Call(Hwnd, wTitle, wClass, wExe, Event)
			}
		}
	}
	class Event
	{
		Add(eventMin, eventMax, eventProc, idProcess, WinTitle := "")
		{
			if !WinHook.Event.Hooks
			{
				WinHook.Event.Hooks := {}
				OnExit(ObjBindMethod(WinHook.Event, "UnHookAll"))
			}
			Hook := DllCall("SetWinEventHook"
				, "UInt",	eventMin								;  UINT eventMin
				, "UInt",	eventMax								;  UINT eventMax
				, "Ptr" ,	0x0											;  HMODULE hmodWinEventProc
				, "Ptr" ,	RegisterCallback(WinHook.Event.Message)	;  WINEVENTPROC lpfnWinEventProc
				, "UInt" ,	idProcess								;  DWORD idProcess
				, "UInt",	0x0											;  DWORD idThread
				, "UInt",	0x0|0x2)  								;  UINT dwflags, OutOfContext|SkipOwnProcess
			if !IsObject(eventProc)
				eventProc := Func(eventProc)
			WinHook.Event.Hooks[Hook] := {eventMin: eventMin, eventMax: eventMax, eventProc: eventProc, idProcess: idProcess, WinTitle: WinTitle}
		}
		UnHook(idProcess)
		{
			for key, Hook in WinHook.Event.Hooks
				if (Hook.idProcess = idProcess)
					DllCall("UnhookWinEvent", "Ptr",Hook.Handle)
		}
		UnHookAll()
		{
			for key, Hook in WinHook.Event.Hooks
				DllCall("UnhookWinEvent", "Ptr",Hook.Handle)
		}
		Message(event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)  ; 'Private Method 
		{
			Hook := WinHook.Event.Hooks[hWinEventHook := this] ; this' is hidden param1 because method is called as func
			WinGet, List, List, % Hook.WinTitle
			Loop % List
				if  (List%A_Index% = hwnd)
				{
					Hook.eventProc.Call(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
					return
				}
		}
	}
}
The WinHook.Shell.Add sets a shell hook to watch for a notepad window being created. When it detects one, it then calls the Created function. This function uses WinHook.Event.Add to set two event hooks to watch for that notepad window being minimized or restored and calls the designated function.

The WinHook class allows you to setup any Window Shell Hook or Window Event Hook pretty easily. Between those two types of hooks many useful actions can be detected.

FG


Thanks Dear FanaticGuru... :wave: :wave: :wave:

You are really great... :clap: :clap: :clap:


Thanks a lot once again sir.... :salute: :salute: :salute:
I don't normally code as I don't code normally.
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to appear a msgbox when I activate specific window?

25 Dec 2018, 03:23

FanaticGuru wrote:
19 Nov 2018, 19:54
Here is a class I created called WinHook that makes working with Window Shell Hooks and Window Event Hooks much easier.

Code: Select all

WinHook.Shell.Add("Created",,, "NOTEPAD.EXE",1) ; Notepad Window Created

Created(Win_Hwnd, Win_Title, Win_Class, Win_Exe, Win_Event)
{
	MsgBox Created
	WinGet, PID, PID, ahk_id %Win_Hwnd%
	WinHook.Event.Add(0x0016, 0x0016, "Minimized", PID) 
	WinHook.Event.Add(0x0017, 0x0017, "Restored", PID) 
}

Minimized(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
{
	MsgBox Minimized
}

Restored(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
{
	MsgBox Restored
}

; [Class] WinHook
; Fanatic Guru
; 2018 10 16
;
; Class to set hooks of windows or processes
;
;{-----------------------------------------------
;	
;	Class (Nested):		WinHook.Shell
;
;		Method:
; 			Add(Func, wTitle:="", wClass:="", wExe:="", Event:=1)
;
;		Desc: Add Shell Hook
;
;   	Parameters:
;		   	1) {Func}			Function name or Function object to call on event
;   		2) {wTitle}		window Title to watch for event (default = "", all windows)
;   		3) {wClass}		window Class to watch for event (default = "", all windows)
;   		4) {wExe}			window Exe to watch for event (default = "", all windows)
;   		5) {Event}			Event (default = 1, window created)
;
;				Shell Hook Events:
;				1 = HSHELL_WINDOWCREATED
;				2 = HSHELL_WINDOWDESTROYED
;				3 = HSHELL_ACTIVATESHELLWINDOW
;				4 = HSHELL_WINDOWACTIVATED
;				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
;
;		Note: ObjBindMethod(obj, Method) can be used to create a function object to a class method
;					WinHook.Shell.Add(ObjBindMethod(TestClass.TestNestedClass, "MethodName"), wTitle, wClass, wExe, Event)
;
; ----------
;
;		Desc: Function Called on Event
;			FuncOrMethod(Win_Hwnd, Win_Title, Win_Class, Win_Exe, Win_Event)
;		
;		Parameters:
;		   	1) {Win_Hwnd}		window handle ID of window with event 
;   		2) {Win_Title}		window Title of window with event
;   		3) {Win_Class}		window Class of window with event
;   		4) {Win_Exe}			window Exe of window with event
;   		5) {Win_Event}		window Event
;
;}
;	Class (Nested):		WinHook.Event
;
;		Method:
;			Add(eventMin, eventMax, eventProc, idProcess, WinTitle := "") 			
;
;		Desc: Add Event Hook
;
;   	Parameters:
;		   	1) {eventMin}		lowest Event value handled by the hook function
;   		2) {eventMax}		highest event value handled by the hook function
;   		3) {eventProc}		event hook function, call be function name or function object
;   		4) {idProcess}		ID of the process from which the hook function receives events
;   		5) {WinTitle}			WinTitle to identify which windows to operate on, (default = "", all windows)
;
;				Event Hook Events:
;				0x8012 = EVENT_OBJECT_ACCELERATORCHANGE
;				0x8017 = EVENT_OBJECT_CLOAKED
;				0x8015 = EVENT_OBJECT_CONTENTSCROLLED
;				0x8000 = EVENT_OBJECT_CREATE
;				0x8011 = EVENT_OBJECT_DEFACTIONCHANGE
;				0x800D = EVENT_OBJECT_DESCRIPTIONCHANGE
;				0x8001 = EVENT_OBJECT_DESTROY
;				0x8021 = EVENT_OBJECT_DRAGSTART
;				0x8022 = EVENT_OBJECT_DRAGCANCEL
;				0x8023 = EVENT_OBJECT_DRAGCOMPLETE
;				0x8024 = EVENT_OBJECT_DRAGENTER
;				0x8025 = EVENT_OBJECT_DRAGLEAVE
;				0x8026 = EVENT_OBJECT_DRAGDROPPED
;				0x80FF = EVENT_OBJECT_END
;				0x8005 = EVENT_OBJECT_FOCUS
;				0x8010  = EVENT_OBJECT_HELPCHANGE
;				0x8003 = EVENT_OBJECT_HIDE
;				0x8020 = EVENT_OBJECT_HOSTEDOBJECTSINVALIDATED
;				0x8028 = EVENT_OBJECT_IME_HIDE
;				0x8027 = EVENT_OBJECT_IME_SHOW
;				0x8029 = EVENT_OBJECT_IME_CHANGE
;				0x8013 = EVENT_OBJECT_INVOKED
;				0x8019 = EVENT_OBJECT_LIVEREGIONCHANGED
;				0x800B = EVENT_OBJECT_LOCATIONCHANGE
;				0x800C = EVENT_OBJECT_NAMECHANGE
;				0x800F = EVENT_OBJECT_PARENTCHANGE
;				0x8004 = EVENT_OBJECT_REORDER
;				0x8006 = EVENT_OBJECT_SELECTION
;				0x8007 = EVENT_OBJECT_SELECTIONADD
;				0x8008 = EVENT_OBJECT_SELECTIONREMOVE
;				0x8009 = EVENT_OBJECT_SELECTIONWITHIN
;				0x8002 = EVENT_OBJECT_SHOW
;				0x800A = EVENT_OBJECT_STATECHANGE
;				0x8030 = EVENT_OBJECT_TEXTEDIT_CONVERSIONTARGETCHANGED
;				0x8014 = EVENT_OBJECT_TEXTSELECTIONCHANGED
;				0x8018 = EVENT_OBJECT_UNCLOAKED
;				0x800E = EVENT_OBJECT_VALUECHANGE
;				0x0002 = EVENT_SYSTEM_ALERT
;				0x8016 = EVENT_SYSTEM_ARRANGMENTPREVIEW
;				0x0009 = EVENT_SYSTEM_CAPTUREEND
;				0x0008 = EVENT_SYSTEM_CAPTURESTART
;				0x000D = EVENT_SYSTEM_CONTEXTHELPEND
;				0x000C = EVENT_SYSTEM_CONTEXTHELPSTART
;				0x0020 = EVENT_SYSTEM_DESKTOPSWITCH
;				0x0011 = EVENT_SYSTEM_DIALOGEND
;				0x0010 = EVENT_SYSTEM_DIALOGSTART
;				0x000F = EVENT_SYSTEM_DRAGDROPEND
;				0x000E = EVENT_SYSTEM_DRAGDROPSTART
;				0x00FF = EVENT_SYSTEM_END
;				0x0003 = EVENT_SYSTEM_FOREGROUND
;				0x0007 = EVENT_SYSTEM_MENUPOPUPEND
;				0x0006 = EVENT_SYSTEM_MENUPOPUPSTART
;				0x0005 = EVENT_SYSTEM_MENUEND
;				0x0004 = EVENT_SYSTEM_MENUSTART
;				0x0017 = EVENT_SYSTEM_MINIMIZEEND
;				0x0016 = EVENT_SYSTEM_MINIMIZESTART
;				0x000B = EVENT_SYSTEM_MOVESIZEEND
;				0x000A = EVENT_SYSTEM_MOVESIZESTART
;				0x0013 = EVENT_SYSTEM_SCROLLINGEND
;				0x0012 = EVENT_SYSTEM_SCROLLINGSTART
;				0x0001 = EVENT_SYSTEM_SOUND
;				0x0015 = EVENT_SYSTEM_SWITCHEND
;				0x0014 = EVENT_SYSTEM_SWITCHSTART
;	   
class WinHook
{
	class Shell
	{
		Add(Func, wTitle:="", wClass:="", wExe:="", Event:=1)
		{
			if !WinHook.Shell.Hooks
			{
				WinHook.Shell.Hooks := {}, WinHook.Shell.Events := {}
				DllCall("RegisterShellHookWindow", UInt, A_ScriptHwnd)
				MsgNum := DllCall("RegisterWindowMessage", Str, "SHELLHOOK")
				OnMessage(MsgNum, ObjBindMethod(WinHook.Shell, "Message"))
			}
			if !IsObject(Func)
				Func := Func(Func)
			WinHook.Shell.Hooks.Push({Func: Func, Title: wTitle, Class: wClass, Exe: wExe, Event: Event})
			WinHook.Shell.Events[Event] := true
			return
		}
		Deregister()
		{
			DllCall("DeregisterShellHookWindow", UInt, A_ScriptHwnd)
		}
		Message(Event, Hwnd)  ; Private Method
		{
			If WinHook.Shell.Events[Event] 
			{
				WinGetTitle, wTitle, ahk_id %Hwnd%
				WinGetClass, wClass, ahk_id %Hwnd%
				WinGet, wExe, ProcessName, ahk_id %Hwnd%
				for key, Hook in WinHook.Shell.Hooks
					if ((Hook.Title = wTitle or Hook.Title = "") and (Hook.Class = wClass or Hook.Class = "") and (Hook.Exe = wExe or Hook.Exe = "") and (Hook.Event = Event))
						Hook.Func.Call(Hwnd, wTitle, wClass, wExe, Event)
			}
		}
	}
	class Event
	{
		Add(eventMin, eventMax, eventProc, idProcess, WinTitle := "")
		{
			if !WinHook.Event.Hooks
			{
				WinHook.Event.Hooks := {}
				OnExit(ObjBindMethod(WinHook.Event, "UnHookAll"))
			}
			Hook := DllCall("SetWinEventHook"
				, "UInt",	eventMin								;  UINT eventMin
				, "UInt",	eventMax								;  UINT eventMax
				, "Ptr" ,	0x0											;  HMODULE hmodWinEventProc
				, "Ptr" ,	RegisterCallback(WinHook.Event.Message)	;  WINEVENTPROC lpfnWinEventProc
				, "UInt" ,	idProcess								;  DWORD idProcess
				, "UInt",	0x0											;  DWORD idThread
				, "UInt",	0x0|0x2)  								;  UINT dwflags, OutOfContext|SkipOwnProcess
			if !IsObject(eventProc)
				eventProc := Func(eventProc)
			WinHook.Event.Hooks[Hook] := {eventMin: eventMin, eventMax: eventMax, eventProc: eventProc, idProcess: idProcess, WinTitle: WinTitle}
		}
		UnHook(idProcess)
		{
			for key, Hook in WinHook.Event.Hooks
				if (Hook.idProcess = idProcess)
					DllCall("UnhookWinEvent", "Ptr",Hook.Handle)
		}
		UnHookAll()
		{
			for key, Hook in WinHook.Event.Hooks
				DllCall("UnhookWinEvent", "Ptr",Hook.Handle)
		}
		Message(event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)  ; 'Private Method 
		{
			Hook := WinHook.Event.Hooks[hWinEventHook := this] ; this' is hidden param1 because method is called as func
			WinGet, List, List, % Hook.WinTitle
			Loop % List
				if  (List%A_Index% = hwnd)
				{
					Hook.eventProc.Call(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
					return
				}
		}
	}
}
The WinHook.Shell.Add sets a shell hook to watch for a notepad window being created. When it detects one, it then calls the Created function. This function uses WinHook.Event.Add to set two event hooks to watch for that notepad window being minimized or restored and calls the designated function.

The WinHook class allows you to setup any Window Shell Hook or Window Event Hook pretty easily. Between those two types of hooks many useful actions can be detected.

FG



Hello dear FanaticGuru ...

Sir, your codes are working fine when notepad window is minimized and maximized, but if I activate any other window by pressing alt+tab and then i again activate notepad window by pressing alt+tab then your codes do not work.

Suppose, currently notepad window is activated on my screen. Now i activates google chrome by pressing alt+tab or by left clicking. Now again I activates notepad window by pressing alt+tab or by left clicking then it does not show the msgbox.

Please solve my this problem.

Thanks a lot sir...
I don't normally code as I don't code normally.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: How to appear a msgbox when I activate specific window?

26 Dec 2018, 14:12

Sabestian Caine wrote:
25 Dec 2018, 03:23
Hello dear FanaticGuru ...

Sir, your codes are working fine when notepad window is minimized and maximized, but if I activate any other window by pressing alt+tab and then i again activate notepad window by pressing alt+tab then your codes do not work.

Suppose, currently notepad window is activated on my screen. Now i activates google chrome by pressing alt+tab or by left clicking. Now again I activates notepad window by pressing alt+tab or by left clicking then it does not show the msgbox.

Please solve my this problem.

Thanks a lot sir...

I posted WinHook in its own topic at the link below:
https://autohotkey.com/boards/viewtopic.php?t=59149

This is a newer version than posted in this thread so maybe it will work.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to appear a msgbox when I activate specific window?

28 Dec 2018, 12:48

FanaticGuru wrote:
26 Dec 2018, 14:12
Sabestian Caine wrote:
25 Dec 2018, 03:23
Hello dear FanaticGuru ...

Sir, your codes are working fine when notepad window is minimized and maximized, but if I activate any other window by pressing alt+tab and then i again activate notepad window by pressing alt+tab then your codes do not work.

Suppose, currently notepad window is activated on my screen. Now i activates google chrome by pressing alt+tab or by left clicking. Now again I activates notepad window by pressing alt+tab or by left clicking then it does not show the msgbox.

Please solve my this problem.

Thanks a lot sir...

I posted WinHook in its own topic at the link below:
https://autohotkey.com/boards/viewtopic.php?t=59149

This is a newer version than posted in this thread so maybe it will work.

FG
Hello dear FanaticGuru...

Sir, I tried to use these codes as found in your above link but these codes are not working-

Code: Select all

WinHook.Shell.Add(ObjBindMethod(Excel.Shell, "Button"),,, "EXCEL.EXE",1) ; Excel Window Created

;{ Excel Call Functions
class Excel
{
	class Shell
	{
		Button(Win_Hwnd, Win_Title, Win_Class, Win_Exe, Win_Event) ; Create Button
		{
			static Button
			; ----- Create WinHook.Event to handle button location with Resize method
			WinGet, PID, PID, ahk_id %Win_Hwnd%
			WinHook.Event.Add(0x800B, 0x800B, ObjBindMethod(Excel.Event, "Resize"), PID, "ahk_id " Win_Hwnd) ; LOCATIONCHANGE
			;~ WinHook.Event.Add(0x000B, 0x000B, ObjBindMethod(Excel.Event, "Resize"), PID, "ahk_id " Win_Hwnd) ; MOVESIZEEND
			; -----
			WinGetPos,,, Win_W, Win_H, ahk_id %Win_Hwnd%
			Gui_X := Win_W - 45 - 49
			Gui_Y := Win_H - 60 - 34
			; Gui Create
			Gui Excel:Default
			Gui +Resize
			Gui, Font, s12, Bold Verdana
			Gui, Margin, 0, 0
			Gui, Add, Button, xp yp Default HWNDButton gButton, Click
			Gui, +LastFound +ToolWindow +AlwaysOnTop -Caption -Border HWNDGui_Hwnd
			Excel.Gui_Hwnd := Gui_Hwnd ; Assign to Class variable to allow access from other Methods
			DllCall("SetParent", "uint", Gui_Hwnd, "uint", Win_Hwnd)
			Gui, Show, x%Gui_X% y%Gui_Y%
			return
			; Gui Actions
			Button:
				MsgBox Clicked
			return
			ExcelGuiSize:
				Excel.Gui_W := A_GuiWidth ; Assign to Class variable to allow access from other Methods
				Excel.Gui_H := A_GuiHeight ; Assign to Class variable to allow access from other Methods
				GuiControl, Move, %Button%, % "W" Excel.Gui_W " H" Excel.Gui_H
			return
		}
	}
	class Event
	{	
		Resize(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
		{
			WinGetPos,,, Win_W, Win_H, ahk_id %hwnd%
			Gui_X := Win_W - 45 - Excel.Gui_W
			Gui_Y := Win_H - 60 - Excel.Gui_H
			WinMove, % "ahk_id " Excel.Gui_Hwnd,, Gui_X, Gui_Y
			return
		}
	}
}
;}

Sir i want that when notepad window is not activated i.e when it is behind other window (which is not minimized) then it should appear msgbox saying notepad window is not activated and if notepad window is activated then it should say notepad window is activated. Is it possible? Please help. Thanks a lot sir..
I don't normally code as I don't code normally.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: How to appear a msgbox when I activate specific window?

28 Dec 2018, 14:32

Sabestian Caine wrote:
28 Dec 2018, 12:48
Sir, I tried to use these codes as found in your above link but these codes are not working-

Code: Select all

WinHook.Shell.Add(ObjBindMethod(Excel.Shell, "Button"),,, "EXCEL.EXE",1) ; Excel Window Created

;{ Excel Call Functions
class Excel
{
	class Shell
	{
		Button(Win_Hwnd, Win_Title, Win_Class, Win_Exe, Win_Event) ; Create Button
		{
			static Button
			; ----- Create WinHook.Event to handle button location with Resize method
			WinGet, PID, PID, ahk_id %Win_Hwnd%
			WinHook.Event.Add(0x800B, 0x800B, ObjBindMethod(Excel.Event, "Resize"), PID, "ahk_id " Win_Hwnd) ; LOCATIONCHANGE
			;~ WinHook.Event.Add(0x000B, 0x000B, ObjBindMethod(Excel.Event, "Resize"), PID, "ahk_id " Win_Hwnd) ; MOVESIZEEND
			; -----
			WinGetPos,,, Win_W, Win_H, ahk_id %Win_Hwnd%
			Gui_X := Win_W - 45 - 49
			Gui_Y := Win_H - 60 - 34
			; Gui Create
			Gui Excel:Default
			Gui +Resize
			Gui, Font, s12, Bold Verdana
			Gui, Margin, 0, 0
			Gui, Add, Button, xp yp Default HWNDButton gButton, Click
			Gui, +LastFound +ToolWindow +AlwaysOnTop -Caption -Border HWNDGui_Hwnd
			Excel.Gui_Hwnd := Gui_Hwnd ; Assign to Class variable to allow access from other Methods
			DllCall("SetParent", "uint", Gui_Hwnd, "uint", Win_Hwnd)
			Gui, Show, x%Gui_X% y%Gui_Y%
			return
			; Gui Actions
			Button:
				MsgBox Clicked
			return
			ExcelGuiSize:
				Excel.Gui_W := A_GuiWidth ; Assign to Class variable to allow access from other Methods
				Excel.Gui_H := A_GuiHeight ; Assign to Class variable to allow access from other Methods
				GuiControl, Move, %Button%, % "W" Excel.Gui_W " H" Excel.Gui_H
			return
		}
	}
	class Event
	{	
		Resize(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
		{
			WinGetPos,,, Win_W, Win_H, ahk_id %hwnd%
			Gui_X := Win_W - 45 - Excel.Gui_W
			Gui_Y := Win_H - 60 - Excel.Gui_H
			WinMove, % "ahk_id " Excel.Gui_Hwnd,, Gui_X, Gui_Y
			return
		}
	}
}
;}

Sir i want that when notepad window is not activated i.e when it is behind other window (which is not minimized) then it should appear msgbox saying notepad window is not activated and if notepad window is activated then it should say notepad window is activated. Is it possible? Please help. Thanks a lot sir..
The code you posted is an example for Excel, a fairly complex example that would not apply very much to your case.

My code makes monitoring some events easier but there are still many event codes. You still have to have a knowledge of the events that can be monitored and when they are triggered. That is a broad subject that generally requires some research.

All the event codes that WinHook detects are in the comments of the script so that people can look them up and see if they do what they need.

I know there is EVENT_OBJECT_FOCUS and HSHELL_WINDOWACTIVATED that will probably fire when a window gains focus. There is probably one that would help figure out when a window loses focus or changes z order and gets put behind another but I do not know exactly when every event code fires.

An approach I have used in the past for event codes is just to write a test script that fires on all events of a type and then displays the event codes in a log. Then do the actions you are trying to detect and see if a certain event code fires.

Also, if a script is trying to monitor its own windows, then you can use OnMessage with WM_WINDOWPOSCHANGED which detects z position changes. That approach would have nothing to do with the hooks used by WinHook.

WinHook is not the total solution to all event and message monitoring.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: How to appear a msgbox when I activate specific window?

28 Dec 2018, 17:32

Try also

Code: Select all

#Persistent
HookProcAdr := RegisterCallback( "HookProc", "F" )
dwFlags := ( ExcludeScriptMessages = 1 ? 0x1 : 0x0 )
hWinEventHook := SetWinEventHook( 0x1, 0x17, 0, HookProcAdr, 0, 0, 0 )

HookProc( hWinEventHook, Event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime )
{
	Event += 0
	If Event = 3	; "EVENT_SYSTEM_FOREGROUND"
	{
		WinGetClass, Class, ahk_id %hwnd%
		If (Class = "Notepad")			
			ToolTip, notepad window is activated
		else
			ToolTip
	}
}

SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags) 
{
	DllCall("CoInitialize", Uint, 0)
	return DllCall("SetWinEventHook"
	, Uint,eventMin	
	, Uint,eventMax	
	, Uint,hmodWinEventProc
	, Uint,lpfnWinEventProc
	, Uint,idProcess
	, Uint,idThread
	, Uint,dwFlags)	
}
https://autohotkey.com/board/topic/3266 ... -messages/
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: How to appear a msgbox when I activate specific window?

28 Dec 2018, 19:22

GEV wrote:
28 Dec 2018, 17:32
Try also

Code: Select all

#Persistent
HookProcAdr := RegisterCallback( "HookProc", "F" )
dwFlags := ( ExcludeScriptMessages = 1 ? 0x1 : 0x0 )
hWinEventHook := SetWinEventHook( 0x1, 0x17, 0, HookProcAdr, 0, 0, 0 )

HookProc( hWinEventHook, Event, hWnd, idObject, idChild, dwEventThread, dwmsEventTime )
{
	Event += 0
	If Event = 3	; "EVENT_SYSTEM_FOREGROUND"
	{
		WinGetClass, Class, ahk_id %hwnd%
		If (Class = "Notepad")			
			ToolTip, notepad window is activated
		else
			ToolTip
	}
}

SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags) 
{
	DllCall("CoInitialize", Uint, 0)
	return DllCall("SetWinEventHook"
	, Uint,eventMin	
	, Uint,eventMax	
	, Uint,hmodWinEventProc
	, Uint,lpfnWinEventProc
	, Uint,idProcess
	, Uint,idThread
	, Uint,dwFlags)	
}
https://autohotkey.com/board/topic/3266 ... -messages/
This is the kiind of code that WinHook makes a little easier.

Here is the same done with WinHook:

Code: Select all

; When Foreground event occurs call func ForeGroundChange for any process (basically when any window comes to the foreground the func is called) 
WinHook.Event.Add(3, 3, "ForeGroundChange") ; 3 = EVENT_SYSTEM_FOREGROUND

ForeGroundChange(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
{
	WinGetClass, Class, ahk_id %hwnd%
	If (Class = "Notepad")			; was it Notepad window
		ToolTip, Notepad Window came to Foreground
	else
		ToolTip, non-Notepad Window came to Foreground (Notepad Window not to Foreground)  
}
You don't have to understand registering callbacks and dll calls.

You still have to know the event codes and what you want to do when they fire.

On a side note this stripped down version does not manage the hooks and unhook them which is probably causing a memory leak.

Also, I am not sure why DllCall("CoInitialize", Uint, 0) is being used.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CrowexBR and 265 guests