Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Detecting New Child Window


  • Please log in to reply
5 replies to this topic
QTipPoker
  • Members
  • 14 posts
  • Last active: Dec 19 2012 09:10 PM
  • Joined: 21 Sep 2012
I have a program I run that uses a lot of pop up message and I want to automatically detect them and press a given button on them. I have it working using a hotkey. For example, one of the popups has the word Confirm in its title. Using the hot key, I can close it with this.

IfWinExist, Confirm
{
WinActivate
Click, 212, 136
return
}

However, I want to do this without using a hotkey; just want to detect the new window when it's opened and have it done automatically. I've read through this thread:

<!-- l --><a class="postlink-local" href="http://www.autohotkey.com/community/viewtopic.php?f=16&t=86496">viewtopic.php?f=16&t=86496</a><!-- l -->

However, it seems this doesn't work for child windows. Anyone know how to detect child windows when they pop up?

Thanks.

Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
I can't speak to using shell messages.

But what about using 'Settimer' to run the lines you listed ?
And I suggest moving the return outside the {}

Btw, please post your script within [code] [/code] tags

QTipPoker
  • Members
  • 14 posts
  • Last active: Dec 19 2012 09:10 PM
  • Joined: 21 Sep 2012
Thanks a lot. I do have that worked out, but I was concerned it might slow down the rest of my code or use a lot of processing to have it check like every 250 milliseconds. So I thought I would try to do it only when necessary...maybe I'm overestimating the stress it puts on a system.

That return thing...things are muddy for me with this return. I've read through the tutorials and see you need one when you have more than one line in a code, but then other times in a tutorial I see it used after only one line.

Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009
Return is so important, it was given its own page in the helpfile.
Here it is in the on-line docs <!-- m -->http://www.autohotke...ands/Return.htm<!-- m -->

In the case of your example, perhaps this will help explain.
IfWinExist, Confirm
{
WinActivate
Click, 212, 136 
return
}
Msgbox oops the Confirm windows didn't exist

Concerning the stress on the system, I suggest searching for the variable 'A_TickCount' in the docs and looking at the related 'elapsedtime' example. Remove the sleep, 1000
and place the remaining lines around one of your long loops, to see how long it actually takes.

Maybe this will give you a feel for the time things take to accomplish.

Another method is to try the following in your script.
settimer, counter, 250
return
counter:
count++
tooltip % count
return


QTipPoker
  • Members
  • 14 posts
  • Last active: Dec 19 2012 09:10 PM
  • Joined: 21 Sep 2012
So helpful...thanks a lot.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

Here's an example that sets a hook for the child window - requires AutoHotkey_L & the Acc Library. Press F1 to create the Child Window:

EVENT_OBJECT_CREATE := "0x8000"
Gui, New, HWNDGuiHwnd
Gui, Show, w150 h50
pCallback := RegisterCallback("WinEventProc")
Acc_SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE, pCallBack)
return

GuiClose:
	Acc_UnhookWinEvent(pCallback)
	ExitApp

F1:: Gui %GuiHwnd%: Add, Text, x10 y10 w80 h20, Hello World!


WinEventProc(hHook, event, hWnd, idObject, idChild, eventThread, eventTime) {
	global GuiHwnd
	Acc := Acc_ObjectFromEvent(_idChild_, hWnd, idObject, idChild)
	if Acc.accRole(0)=9 and DllCall("GetParent", "Ptr",hwnd)=GuiHwnd
		MsgBox, , New Child Win, % "ClassNN: " GetClassNN(hWnd,GuiHwnd)
}





GetClassNN(Chwnd, Whwnd) {
	global _GetClassNN := {}
	_GetClassNN.Hwnd := Chwnd
	Detect := A_DetectHiddenWindows
	WinGetClass, Class, ahk_id %Chwnd%
	_GetClassNN.Class := Class
	DetectHiddenWindows, On
	EnumAddress := RegisterCallback("GetClassNN_EnumChildProc")
	DllCall("EnumChildWindows", "uint",Whwnd, "uint",EnumAddress)
	DetectHiddenWindows, %Detect%
	return, _GetClassNN.ClassNN, _GetClassNN:=""
}
GetClassNN_EnumChildProc(hwnd, lparam) {
	static N
	global _GetClassNN
	WinGetClass, Class, ahk_id %hwnd%
	if _GetClassNN.Class == Class
		N++
	return _GetClassNN.Hwnd==hwnd? (0, _GetClassNN.ClassNN:=_GetClassNN.Class N, N:=0):1
}