Sign in to follow this  
Followers 0
timmy2

How do I wait for a child window to open when I don't know its name?

10 posts in this topic

After opening an MDI program I must wait for the user to click on a drop-down menu in that program. (He's going to select from 4 possible documents to open.)

After he makes his selection a child window will open. I won't know its name and really don't care; I just need to maximize this child child window (regardless of which document he opened). Note that there are no other windows open inside this program; just the one child window.

Share this post


Link to post
Share on other sites



You could use function WinList to get a list of windows. You need at least some text that is displayed on this window to reduce the number of returned windows.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Get the process id of the parent window, and run this function in a loop...it returns all the window handles of that process, which you can then grab the titles/text/whatever from, and conditionally do certain actions:

Func WGe_ProcessGetWindow($p_PID, $p_ReturnBestGuess = False)
; #FUNCTION# ============================================================================================================================
; Name...........: WGe_ProcessGetWindow
;
; Description ...: Returns an array of HWNDs containing all windows owned by the process $p_PID, or optionally a single "best guess."
;
; Syntax.........: WGe_ProcessGetWindow( $p_PID [, $p_ReturnBestGuess = False ])
;
; Parameters ....: $p_PID - The PID of the process you want the Window for.
;                 $p_ReturnBestGuess - If True, function will return only 1 reult on a best-guess basis.
;                                          The "Best Guess" is the VISIBLE window owned by $p_PID with the longest title.
;
; Return values .: Success    - Return $_array containing HWND info.
;                                      $_array[0] = Number of results
;                                      $_array[n] = HWND of Window n
;
;                 Failure     - Returns 0
;
;                 Error     - Returns -1 and sets @error
;                                           1 - Requires a non-zero number.
;                                           2 - Process does not exist
;                                           3 - WinList() Error
;
; Author ........: Andrew Bobulsky, contact: RulerOf <at that public email service provided by Google>.
; Remarks .......: The reverse of WinGetProcess()
; =======================================================================================================================================
Local $p_ReturnVal[1] = [0]
Local $p_WinList = WinList()
If @error Then ;Some Error handling
  SetError(3)
  Return -1
EndIf
If $p_PID = 0 Then ;Some Error handling
  SetError(1)
  Return -1
EndIf
If ProcessExists($p_PID) = 0 Then ;Some Error handling
  ConsoleWrite("WGe_ProcessGetWindow: Process " & $p_PID & " doesn't exist!" & @CRLF)
  SetError(2)
  Return -1
EndIf
For $i = 1 To $p_WinList[0][0] Step 1
  Local $w_PID = WinGetProcess($p_WinList[$i][1])
  ; ConsoleWrite("Processing Window: " & Chr(34) & $p_WinList[$i][0] & Chr(34) & @CRLF & " with HWND: " & $p_WinList[$i][1] & @CRLF & " and PID: " & $w_PID & @CRLF)
  If $w_PID = $p_PID Then
   ;ConsoleWrite("Match: HWND " & $p_WinList[$i][1] & @CRLF)
   $p_ReturnVal[0] += 1
   _ArrayAdd($p_ReturnVal, $p_WinList[$i][1])
  EndIf
Next
If $p_ReturnVal[0] > 1 Then
  If $p_ReturnBestGuess Then
   Do
    Local $i_State = WinGetState($p_ReturnVal[2])
    Local $i_StateLongest = WinGetState($p_ReturnVal[1])
    Select
     Case BitAND($i_State, 2) And BitAND($i_StateLongest, 2) ;If they're both visible
      If StringLen(WinGetTitle($p_ReturnVal[2])) > StringLen(WinGetTitle($p_ReturnVal[1])) Then ;And the new one has a longer title
       _ArrayDelete($p_ReturnVal, 1) ;Delete the "loser"
       $p_ReturnVal[0] -= 1 ;Decrement counter
      Else
       _ArrayDelete($p_ReturnVal, 2) ;Delete the failed challenger
       $p_ReturnVal[0] -= 1
      EndIf
     Case BitAND($i_State, 2) And Not BitAND($i_StateLongest, 2) ;If the new one's visible and the old one isn't
      _ArrayDelete($p_ReturnVal, 1) ;Delete the old one
      $p_ReturnVal[0] -= 1 ;Decrement counter
     Case Else ;Neither window is visible, let's just keep the first one.
      _ArrayDelete($p_ReturnVal, 2)
      $p_ReturnVal[0] -= 1
    EndSelect
   Until $p_ReturnVal[0] = 1
  EndIf
  Return $p_ReturnVal
ElseIf $p_ReturnVal[0] = 1 Then
  Return $p_ReturnVal ;Only 1 window.
Else
  Return 0 ;Window not found.
EndIf
EndFunc   ;==>WGe_ProcessGetWindow
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Or check It shows how to retrieve the child windows of a process.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Universalist, thank you for the help. When I try to Build your code two functions are undefined: ArrayAdd and ArrayDelete. Please advise.

Share this post


Link to post
Share on other sites

Universalist, thank you for the help. When I try to Build your code two functions are undefined: ArrayAdd and ArrayDelete. Please advise.

Dog ate your helpfile? ;)

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Thank you for your reply, too, water. I wanted to try Universalist's code first since it appeared ready-to-use, at least for a test. I'm new to AutoIt so I'll need to study up on WinList, and try to understand those other links. Much appreciated, though.

Share this post


Link to post
Share on other sites

Universalist: good one. No, I looked up ArrayAdd but it isn't listed. I looked up "Undefined function" but am too new to AutoIt to determine whether I can ignore such errors or must chase them down. I figured since you gave me the code you might know the error's cause.

Share this post


Link to post
Share on other sites

okay, I figured out that searching in Help requires exact spelling of the search term. And apparently array.au3 is now built in. While those were interesting discoveries that doesn't get me any closer to running Universalist's sample code. Should I assume it's not for running; just for study?

Share this post


Link to post
Share on other sites

He's jdelaney, not Universalist.

1 person likes this

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0