Hi,

"GetForegroundWindow function returns the handle of the foreground window (the window with which the user is currently working)."

I have tried GetForegroundWindow, but it only return the handle of the parent window.
How can I get the handle of the child window, I'm currenty working with ?

Greetings,
Nordwind64
Posted on 2003-06-27 08:05:50 by Nordwind64
Use GetWindow repeatedly with the second parameter set to GW_CHILD until it returns 0. The last window handle passed is the topmost child window.
Posted on 2003-06-27 13:56:00 by Sephiroth3
Hi,

that seems not to work...

invoke GetForegroundWindow
mov nurso,eax
koopy:
invoke GetWindow,nurso,GW_CHILD
.if eax!=0
mov nurso,eax
jmp koopy
.endif

invoke GetClassName,nurso,addr unwichtig,128
invoke SetWindowText,hWin,addr unwichtig

Where's the error ?

Greetings,
Nordwind64
Posted on 2003-06-28 01:12:12 by Nordwind64
from the PSDK:
GetWindow
...
Remarks
The EnumChildWindows function is more reliable than calling GetWindow in a loop. An application that calls GetWindow to perform this task risks being caught in an infinite loop or referencing a handle to a window that has been destroyed.
Posted on 2003-06-28 06:42:50 by iblis
Erm, what does that have to do with what he's trying to do?

Anyway, it turns out that the window you're typing in won't always be the topmost window. Use GetFocus instead to find out which window the user is typing in.
Posted on 2003-06-28 08:05:13 by Sephiroth3
Hi Sephiroth3,

I could not get it working, can you help me with a few lines of (working) code...?

Greetings,
Nordwind64
Posted on 2003-06-28 16:53:55 by Nordwind64

Erm, what does that have to do with what he's trying to do?


Nothing, but it has everything to do with what you told him to do.
Posted on 2003-06-28 17:59:02 by iblis
Use the FindWindowEx function, specify NULL for the hwndChildAfter, that will give you the first child in the Z order which is the topmost child of a given parent. To enumerate the windows you should use EnumChildWindows but you can also use FindWindowEx and specify each child in the hwndChildAfter in turn.

invoke FindWindowEx, hParent, NULL, NULL, NULL
Posted on 2003-06-28 18:25:00 by donkey
But he isn't trying to enumerate windows. He only wants to find out what window the user is working with. Iblis, you've misunderstood it all. The notice in the SDK is about calling GetWindow with GW_HWNDNEXT repeatedly to enumerate all windows, and we aren't doing that.

This will retrieve the handle of the topmost child window (NASM style, sorry):
call
theloop:
mov ebx,eax
push byte 5
push ebx
call
test eax,eax
jnz theloop
; The handle is now in ebx

But when dealing with standard controls, use GetFocus since they won't be put at the top when clicked on.
Posted on 2003-06-28 19:17:23 by Sephiroth3
GetWindow does a Z-Order search. This is a long way of doing

invoke GetForegroundWindow
invoke FindWindowEx, eax, NULL, NULL, NULL

It should return the same result.
Posted on 2003-06-28 19:28:18 by donkey
I'm looking at the SDK right now and there is no note anywhere that the warning is specifically referring to GW_HWNDNEXT.

As for Norwind's question, I think he needs to be a little more specific about what he's trying to do.
Posted on 2003-06-28 19:35:22 by iblis
In any case they will only get the Z order not the focus. The way to find out which child window has the focus is this :

Invoke GetForegroundWindow

mov hTop,eax
; If it is not your window you must attach to it's thread
Invoke GetWindowThreadProcessId,hTop,ADDR pID1
Invoke GetWindowThreadProcessId,hMyWindow,ADDR pID2
Invoke AttachThreadInput,pID1,pID2,TRUE
invoke GetFocus
mov hChild,eax
Invoke AttachThreadInput,pID1,pID2,FALSE
Posted on 2003-06-28 19:38:51 by donkey
Hi,

could not get it working, donkey.
It ever returns a wrong classname, if window is not mine. Sorry.
Can you detect my error...?

  invoke GetForegroundWindow

mov nurso,eax
.if eax!=hWnd
invoke GetWindowThreadProcessId,nurso,ADDR v1
invoke GetWindowThreadProcessId,hWnd,ADDR v2
invoke AttachThreadInput,v1,v2,TRUE
invoke GetFocus
invoke AttachThreadInput,v1,v2,FALSE
.else
invoke GetFocus
.endif
.if eax!=0
mov nurso,eax
.endif

invoke GetClassName,nurso,addr unwichtig,128
invoke SetWindowText,hWnd,addr unwichtig


Greetings,
Nordwind64
Posted on 2003-06-29 03:39:16 by Nordwind64
Hi,

I think, I got it !
This is working correct under Win XP Home:

  control       dd  0

dummy dd 0
v1 dd 0
v2 dd 0
notstring db "tooltip",0

nochmal:
invoke GetForegroundWindow
mov control,eax
mov dummy,0
.if eax!=0
mov dummy,1
invoke GetWindowThreadProcessId,control,0
mov v1,eax
invoke GetWindowThreadProcessId,hWnd,0
mov v2,eax
invoke AttachThreadInput,v1,v2,TRUE
invoke GetFocus
push eax
invoke AttachThreadInput,v1,v2,FALSE
pop eax
.if eax!=0
mov control,eax
.endif
invoke GetClassName,control,addr unwichtig,128
invoke InString,1,addr unwichtig,addr notstringa ;Is control a tooltip ?
.if eax>0
mov dummy,0
.endif
.endif

.if dummy==1
;control ist the control the user is actially working with

invoke GetClassName,control,addr unwichtig,128
invoke SetWindowText,hWnd,addr unwichtig
.endif



donkey, this line had produced the error (returned 0):

invoke GetWindowThreadProcessId,hWnd,addr v2

This line works correct...

invoke GetWindowThreadProcessId,hWnd,0
mov v2,eax

Hmm...
And sometimes, the found control is a tooltip control.
If a tooltip was found the code starts again. Not proper, but it works correct.

The code isn't optimised...

To all: Thank you !!!

Greetings,
Nordwind64
Posted on 2003-06-29 07:23:45 by Nordwind64