Sign in to follow this  
Followers 0
dvdljns

getting active window in win 7

5 posts in this topic

The problem is unless you have a real simple program like notepad the window you are working in is a child window. can someone tell me how to id that.

Share this post


Link to post
Share on other sites



Ok heres what I came up with.

$var = WinList()
For $i = 1 to $var[0][0]
 ; Only display visble windows that have a title
  If $var[$i][0] <> "" AND IsVisible($var[$i][1]) Then
 MsgBox(0, "Details", "Title=" & $var[$i][0] & @LF & "Handle=" & $var[$i][1])
 MsgBox(64, "ProcessId: " , WinGetProcess($var[$i][0]))
  EndIf
Next
Func IsVisible($handle)
  If BitAnd( WinGetState($handle), 8) Then
 Return 1
  Else
 Return 0
 
  EndIf
EndFunc

seems to work but regardles of how much I search I can not find how to save that output. I need to save the output to use in the second part of the script. The second part of the the script needs to pick up the saved output and use it with send so I cand send a predefined set of keystrokes to the window.

Share this post


Link to post
Share on other sites

get the processid, and throw it into:

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

returns array of all windows created under that process...loop through those for the active one.


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

get the processid, and throw it into:

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

returns array of all windows created under that process...loop through those for the active one.

Interesting and miles ahead of what I can do but the problem is I get nothing. Not even a sign the script is running. If it saves the text I can not find it. I need something saved in a text file.

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