Jump to content

WinActive() Bug


BoB-
 Share

Recommended Posts

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

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

Link to comment
Share on other sites

  • Moderators

Is this not the v3 Support forum?

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

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.

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...