PDA

Click to See Complete Forum and Search --> : Detect 'New' Active Window


eng70640
Feb 7th, 2001, 09:27 AM
Hi , i need to create a procedure which checks whether the active window (window in focus) has changed using GetForegroundWindow api & when this occur, use the FindWindowEx api to obtain all the child handles of the 'new' active window . Can anyone help me ? :)

Bios
Feb 8th, 2001, 06:33 AM
ok here ya go I hope this will work...I didn't test it, but if it doesn't work it's close, and you should be able to figure it out from this...



'public declares in your module:

Public Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Long

Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long


'public const's in your module:

Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDNEXT = 2


'actual code:
dfgw& = GetForegroundWindow
subwin&=FindWindowEx(dfgw&, 0&, vbnullstring, vbNullString)
subwin&=getwindow(subwin&, GW_HWNDFIRST)
do:doevents
list1.additem str(subwin&)
subwin&=getwindow(subwin&,GW_HWNDNEXT)
loop until subwin&=0



This code will get the foreground window, and then add all of it's children to a listbox...do whatever you want with the data...just besure to put the line of code that does whatever it is you want to do with the information where the list1.additem... code is

:D Hope this helps,

eng70640
Feb 8th, 2001, 06:47 AM
Hi , how can i save all the child handles to a variable ?

Bios
Feb 8th, 2001, 11:07 PM
You need to in your declares put

Dim dlist() as long


First, at the top put in:

redim dlist(0)

This will create an array that you can redim to the number of windowsm and to store the hwnd in.

Then,where the "List1.additem" is replace it with:

Redim preserve dlist(ubound(dlist)+1)
dlist(ubound(dlist))=subwin&

This will set the hWnd's to the dlist array.

When your program ends you should make sure to free up memory by deleting the array from memory like this:

erase dlist


Hope this clears everything up,

eng70640
Feb 12th, 2001, 12:50 AM
Hi , i have problems assigning the last element of the array dlist to a variable & then reducing the array size by 1 so that i can repeat the same assigning to another variable. For eg , if dlist has elements of 1 to 10 , i assign the element '10' to variable x then i want to reduce the size to 9 so that i can assign the element '9' to variable y .

Lord Orwell
Feb 13th, 2001, 03:08 AM
Not sure if this is what you meant, but you don't need to resize an array to read a number besides the last one.

dim handle_array(10) as long 'handles are long
'place code here to store handles in array
x = handle_array(10)
'no array resize required for next step
y = handle_array(9)
'etc.

eng70640
Feb 13th, 2001, 03:20 AM
Hi the element 1 to 10 was just an example to illusrate my enquiry . My real problem is that achieving that in Bios code . That is in his code , there is an array variable dlist whereby i need to save each & every element of this array variable into individual variable . Sorry if i did not make it clear . :)

Lord Orwell
Feb 13th, 2001, 03:31 AM
Since you don't know how many entries into the array you will have, you can't know in advance how many variables to use. So you will have to use an indexed array, like in his code. Why do you want to use separate variables? If i knew this, maybe i could help.

eng70640
Feb 13th, 2001, 03:35 AM
Hi , my program requires me to do this . This is why i have been trying to do it . :)

Lord Orwell
Feb 13th, 2001, 04:13 AM
You should probably rewrite the part that requires it. But if for some unforseen to me reason you HAVE to have it, extremely lengthy code in the following form would work:
dim var(50)'if you encounter a window with more children than 50(not likely) then simply increase the number

if var(1) > 0 then x = var(1)
if var(2) > 0 then y = var(2)
if var(3) > 0 then zztop = var(3)
if var(4) > 0 then farfegnugen = var(4)
if var(5) > 0 then fluffyteddybear = var(5)
if var(6) > 0 then dropthatbass = var(6)
etc. 50 times
Takes advantage of the fact that numerical arrays are initialized with a zero value, and searching for a handle and not finding one returns a zero value.
If you could post the code sample as an attachment, maybe i could have better insight in what you are trying to do.