Sign in to follow this  
Followers 0
Miran

Advanced way to handle windows of application

1 post in this topic

#1 ·  Posted (edited)

Here is my small solution for better using AutoIt.

The example shows how to:

- find application\process windows

- handle alternative windows

- find window by title using regular expression

- find window by text using regular expression

- find window without given text using regular expression

- find window by controls

- find window which no contains given controls

- hide windows for user

- other things

Save the script in your's AutoIt directory.

It will run AutoIt code editor, type some text and save it to file.

It's recommended to close all you important projects it the editor before run this script.

#Include <WinAPI.au3>

Local $winHandle = 0
Local $winTitle = ""
Local $winText = ""
Local $winClass = ""

Local $hProcess = Run( 'SciTE\SciTE.exe' )
Local $step = 0

; main loop
While ProcessExists( $hProcess )
    ;_WinAPI_WaitForSingleObject( $hProcess )
    ;_WinAPI_WaitForInputIdle( $hProcess )
    
    Local $windows = WinList() ; collect all windows
    
    If IsArray( $windows ) Then
        Local $hProcessWindows[10][2]
        $hProcessWindows[0][0] = 0 ; reset count
        
        For $i=1 to $windows[0][0] ; collect process' windows
            If WinGetProcess($windows[$i][1]) = $hProcess AND BitAnd( WinGetState($windows[$i][1]), 2 ) Then ; check is it window of the process and is visible
                $hProcessWindows[0][0] += 1
                $hProcessWindows[$hProcessWindows[0][0]][0] = $windows[$i][0] ; store title
                $hProcessWindows[$hProcessWindows[0][0]][1] = $windows[$i][1] ; store handle
                
                ;_WinAPI_SetWindowRgn( $windows[$i][1], _WinAPI_CreateRectRgn(0, 0, 0, 0) ) ; hide windows for user
            EndIf
        Next
        
        handleWindow( $hProcessWindows )
    EndIf
WEnd

Func handleWindow( $windows )
    If IsArray( $windows ) Then
        For $i=1 to $windows[0][0]
            $winHandle = $windows[$i][1]
            $winTitle =  $windows[$i][0]
            $winText = WinGetText( $windows[$i][1] ) ; slow!
            $winClass = WinGetClassList( $windows[$i][1] )
            
        ; save changes prompt message box
            If StringRegExp( $winTitle, '(?i)SciTE4AutoIt' ) AND StringRegExp( $winText, '(?i)(?s)Save changes to' ) Then
                MsgBox( 0x40, Default, '"save changes prompt" window is handling now' )
                
                ControlClick( $winHandle, "", "[CLASS:Button; INSTANCE:2]" ) ; press no
                Return 1 ; break function
            EndIf
            
        ; file already opened in editor message box
            If StringRegExp( $winTitle, '(?i)SciTE4AutoIt' ) AND StringRegExp( $winText, '(?i)(?s)OK(.*)already open' ) Then
                MsgBox( 0x40, Default, '"file already open" window is handling now' )
                
                ControlClick( $winHandle, "", "OK" ) ; press ok
                Return 1 ; break function
            EndIf
            
        ; file already opened in editor message box
            If StringRegExp( $winTitle, '(?i)SciTE4AutoIt' ) AND StringRegExp( $winText, '(?i)(?s)OK(.*)already open' ) Then
                MsgBox( 0x40, Default, '"file already open" window is handling now' )
                
                ControlClick( $winHandle, "", "[CLASS:Button; INSTANCE:1]" ) ; press ok
                Return 1 ; break function
            EndIf
            
        ; overwrite prompt dialog (check NOT contains combo box)
            If StringRegExp( $winTitle, '(?i)Save File' ) AND NOT StringRegExp( $winClass, '(?i)(?s)ComboBox' ) AND NOT StringRegExp( $winText, '(?i)(?s)FolderView' ) Then
                MsgBox( 0x40, Default, '"file overvrite" window is handling now' )
                
                ControlClick( $winHandle, "", "[CLASS:Button; INSTANCE:1]" ) ; press yes
                $step = 3
                Return 1 ; break function
            EndIf
            
        ; browse file to save dialog (check contains combo box)
            If StringRegExp( $winTitle, '(?i)Save File' ) AND StringRegExp( $winClass, '(?i)(?s)ComboBox' ) AND StringRegExp( $winText, '(?i)(?s)FolderView' ) Then
                MsgBox( 0x40, Default, '"file browse" window is handling now' )
                ;MsgBox( 0, "Window classes:", $winClass ) ; let's see what it contains
                
                ControlSetText( $winHandle, "", "[CLASS:Edit; INSTANCE:1]", "OutputFile.txt" ) ; set filename
                ControlClick( $winHandle, "", "[CLASS:Button; INSTANCE:2]" ) ; press save
                $step = 2
                Return 1 ; break function
            EndIf
                
        ; main window
            If StringRegExp( $winTitle, '(?i)SciTE4AutoIt' ) AND StringRegExp( $winText, '(?i)(?s)Source(.*)Tab' ) Then
                MsgBox( 0x40, Default, 'Main window is handling now' )
                
                If  $step = 0 Then
                    ControlSend( $winHandle, "", "", "^n") ; file > new (ctrl+n)
                    ControlSend( $winHandle, "", "[CLASS:Scintilla]", "Current time is " & @HOUR & ":" & @MIN & ":" & @SEC ) ; type in text
                    ControlSend( $winHandle, "", "", "^s" ) ; file > save as (ctrl+shift+s)
                    
                    $step = 1
                ElseIf $step = 2 OR $step = 3 Then ; file saved
                    ControlSend( $winHandle, "", "", "!{F4}") ; press ALT+F4
                EndIf

                Return 1 ; break function
            EndIf
        Next
            
        Return 0
    EndIf
EndFunc

Do you have any ideas to make this method better?

It's bit slow because of WinGetText function.

Peace

Edited by Miran

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