Jump to content

ControlClick won't work second time


BabyG
 Share

Recommended Posts

It seems that the code works if the window exists and the button is loaded correctly when it is run. But if i run this code while the window is loading and the button does not exist yet, then controlclick returns a 0 as expected but every time controlclick is called from then on the button is not clicked and a 0 is returned.

This script is being made to install some software. I could easily use text to fix the problems by waiting for the window with the correct text to appear but I will be installing the same software in a different language (same dialog class ect so I will use that instead of the text). So I wish to only use button classnames and button IDs to work out whether the button is ready to be clicked. Anyways, maybe I'm wrong and I just done somthing silly!!!

Here is the code:

The function:

Func Click($title, $button, $wait)
    WinWaitActive ( $title )
    Sleep (5000)
    $seconds = 0
    While ControlClick ( $title, "", $button ) <> 1
        Sleep( 250 )
        $seconds = $seconds + .25
        If $seconds >= $wait then
            $ERROR_TEXT = "'" & $title & "' not found in " & $wait & " seconds"
            LogToFile( $LogFileName, $ERROR_TEXT )
            If MsgBox ( 1, "Error", $ERROR_TEXT ) = 2 then Exit
            $seconds = 0
        EndIf
    WEnd
EndFunc

The main code:

#Include "#functions.au3"

AutoItSetOption("WinTitleMatchMode", 4)

$DefWait = 15

Click( "classname=#32770", 1, $DefWait )

If I run this code when the dialog is fully up and waiting for user input it works!!! but if I run it while the setup stuff is loaded, controlclick returns a 0 and never work again for that button (havn't tried it for other buttons).

Thanx guys!!!

Link to comment
Share on other sites

Hi,

Perhaps u should not use the classname=#32770 while most MS windows have that classname :lmao:

Using the Window Name is better but indeed a bit more work if your'e working with more languages.

see for yourself when using the spy on Task Manager.....

Andre

What about Windows without using AutoIt ?It would be the same as driving a car without an steering Wheel!
Link to comment
Share on other sites

I have a working UDF that is similar, If your interested.

;===============================================================================
;
; Function Name:  _WinWaitClick()
; Description:  Waits for a window, then ControlClicks a button.
;                Log file used if WinWait times out or ControlClick fails.
; Return Value(s):   On Success - Returns 1.
;                   On Failure - Returns 0 if timeout occured.
;                   On Failure - Returns 2 if ControlClick failed.
;                
; Author(s):      MHz
;
;===============================================================================

Func _WinWaitClick($title, $text, $controlid, $timeout)
  ; E.g. _WinWaitClick('Title', 'Text', 1, 10)
   Local $begin = TimerInit()
   Local $log = @TempDir & '\Autoit Error Log.log'
   Local $timer = @TempDir & '\Autoit Timer Log.log'
   Local $time = @HOUR & ':' & @MIN & ':' & @SEC
   if WinWait($title, $text, $timeout) = 0 Then
      FileWriteLine($log, @CRLF & @ScriptName)
      FileWriteLine($log, $time & ' _WinWaitClick("' & $title & '", "' & $text & '", ' & $controlid & ',' & $timeout & ') timeout occurred.')
      BlockInput(0)
      Return SetError(0)
   EndIf
   If ControlClick($title, $text, $controlid) = 0 Then
      FileWriteLine($log, @CRLF & @ScriptName)
      FileWriteLine($log, $time & ' _WinWaitClick("' & $title & '", "' & $text & '", ' & $controlid & ',' & $timeout & ') ControlClick failure occurred.')
      BlockInput(0)
      Return SetError(2)
   EndIf
   SetError(1)
   Local $dif = Round(TimerDiff($begin)/1000, 2)
   FileWriteLine($timer, @CRLF & @ScriptName)
   FileWriteLine($timer, $time & ' _WinWaitClick("' & $title & '", "' & $text & '", ' & $timeout & ') Time:' & $dif)
EndFunc; Logged and Timed
Link to comment
Share on other sites

Thanks guys. I had a look around and there are alot of 32770's. But I come up with a solution that is working so far anyhows. I wrote this function that will look at the last active 32770 and see if it is the same size as the window I am looking for. It seems to work great :lmao:.

Func WaitForWindowWithSize( $title, $width, $height, $wait );Watch out, the size of the windows is usually different to what spy says
    WaitForWindow( $title, $wait )
    Dim $windowsize[2]
    $windowsize = WinGetClientSize ( $title )
    $seconds = 0
    While $windowsize = 1
        Sleep( 250 )
        $seconds = $seconds + .25
        If $seconds >= $wait then
            WindowNotFound( $title, $wait )
            $seconds = 0
        EndIf
        $windowsize = WinGetClientSize ( $title )
    WEnd
    While $windowsize[0] <> $width AND $windowsize[1] <> $height
    ;MsgBox(1, $windowsize[0], $windowsize[1]);debug
        Sleep( 250 )
        $seconds = $seconds + .25
        If $seconds >= $wait then
            WindowNotFound( $title, $wait )
            $seconds = 0
        EndIf
        $windowsize = WinGetClientSize ( $title )
        While $windowsize = 1
            Sleep( 250 )
            $seconds = $seconds + .25
            If $seconds >= $wait then
                WindowNotFound( $title, $wait )
                $seconds = 0
            EndIf
            $windowsize = WinGetClientSize ( $title )
        WEnd
    WEnd
EndFunc

Thanx MHz... It's practically the same as mine :).. except my logging is done is a seperate func.

Anyways.. I still don't understand why I can't use a controlclick and if it fails try again until it works :S... coz after a fail it will never work with that button no mater where it is in the function or how many times i active the window (manualy or with autoit). Ahh well. I guess the windowsize code could be better... maybe even a for statement could be a little better, but for now it works o:)!!! Thanx guys!!!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...