Results 1 to 10 of 10

Thread: Detect 'New' Active Window

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2000
    Posts
    71

    Question

    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 ?

  2. #2
    Lively Member Bios's Avatar
    Join Date
    Nov 1999
    Location
    Richton Park, IL, USA
    Posts
    71
    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...


    Code:
    '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

    Hope this helps,
    Bios
    Age: 17
    Languages: (Q)BASIC, Visual Basic, Dark Basic,C/C++, Visual C++, Perl, Java Script, HTML, ASP

    "DO:BEEP:LOOP"

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2000
    Posts
    71

    Question

    Hi , how can i save all the child handles to a variable ?

  4. #4
    Lively Member Bios's Avatar
    Join Date
    Nov 1999
    Location
    Richton Park, IL, USA
    Posts
    71
    You need to in your declares put
    Code:
    Dim dlist() as long
    First, at the top put in:
    Code:
    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:
    Code:
    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:
    Code:
    erase dlist
    Hope this clears everything up,
    Bios
    Age: 17
    Languages: (Q)BASIC, Visual Basic, Dark Basic,C/C++, Visual C++, Perl, Java Script, HTML, ASP

    "DO:BEEP:LOOP"

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2000
    Posts
    71

    Question

    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 .

  6. #6
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    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.
    Last edited by Lord Orwell; Feb 13th, 2001 at 04:12 AM.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2000
    Posts
    71

    Question

    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 .

  8. #8
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    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.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2000
    Posts
    71

    Question

    Hi , my program requires me to do this . This is why i have been trying to do it .

  10. #10
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    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.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width