Sign in to follow this  
Followers 0
BoB-

WinActive() Bug

7 posts in this topic

#1 ·  Posted (edited)

AutoIt Version 3.1.1

WinActive ( "title" [, "text"] )

title - The title of the window to activate.

text - [optional] The text of the window to activate.

Success: Returns 1.

Failure: Returns 0 if window is not active.

Sample Code

Opt("WinTitleMatchMode", 4)

Run("Explorer.exe")

$CheckWindow = WinActive("classname=ExploreWClass")

WinWaitActive("classname=ExploreWClass")

WinActivate("classname=ExploreWClass")

If $CheckWindow = 1 Then
    MsgBox(0, "Success", "Found Explorer window!", 15)
Elseif $CheckWindow = 0 Then
    MsgBox(0, "Error", "Could not find Explorer window.", 15)
EndIf

When launching "Explorer.exe" it brings up 'My Documents' and displays the msg box that claims it couldn't find the window when it had to wait till it found the window to even continue on past the WinWaitActive which obviously found the window.

So in simple words on the return 0 = success, and 1 = failure rather than the other way around. This does not only happen with Explorer.exe.

Anyways just thought I'd let you guys know :P .

Edited by BoB-

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Welcome to the forums!

Run("Explorer.exe")
$CheckWindow = WinActive("classname=ExploreWClass")

This is the problem here -- you are determining whether or not an Explorer window is active immediately after running it. There is no guarantee at this point that the window has indeed appeared on-screen; consequently this is no bug. You need to do something like this:

Opt('WinTitleMatchMode', 4)
Run('Explorer')
WinWait('ClassName=ExploreWClass')
WinActivate('ClassName=ExploreWClass')
WinWaitActive('ClassName=ExploreWClass')

Edit: To make my example somewhat clearer, consider a program whose window does not appear for some time after launching it (e.g. a Microsoft Office application). Script execution continues immediately after the request to start a program is passed to Windows; AutoIt will not wait for a window to appear.

Edited by LxP

Share this post


Link to post
Share on other sites

as it is a support question I move it to the correct forum :P

Share this post


Link to post
Share on other sites

Ahhh I feel like an idiot now :P . I didn't even think about it at the time that I wasn't waiting for the window to appear before checking for it. Sorry about that :lmao: . Thanks a lot though.


Share this post


Link to post
Share on other sites

Ahhh I feel like an idiot now :P . I didn't even think about it at the time that I wasn't waiting for the window to appear before checking for it. Sorry about that :lmao: . Thanks a lot though.

Don't feel like an idiot. There are no forums here for idiots, but if there were, you wouldn't be alone ;-)

The delay waiting for any window or dialogue has bitten me many times. I now have the

If Not acitve, then activate xxx

WinWaitActive() stuff everywhere, as well as some Sleep() calls too

Share this post


Link to post
Share on other sites

as it is a support question I move it to the correct forum :P

Is this not the v3 Support forum?

Share this post


Link to post
Share on other sites

Is this not the v3 Support forum?

It was probably posted in another forum cappy, and jpm moved it here from there.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

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