windows 如何在 AutoIt 中同时 Winwait 两个窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3481399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:02:18  来源:igfitidea点击:

How to Winwait for two windows simultaneously in AutoIt?

windowswaitautoit

提问by MemphiZ

I would like to know if its possible to "WinWaitActive" for "WindowWithThisTitle" and "WindowWithThatTitle" at the same time. I'm executing a command and there could be a window telling me that the connection failed or a user/pass dialog coming up.

我想知道是否可以同时为“WindowWithThisTitle”和“WindowWithThatTitle”设置“WinWaitActive”。我正在执行一个命令,可能会有一个窗口告诉我连接失败或出现用户/密码对话框。

Is there another way doing it as this?

有没有其他方法可以做到这一点?

WinWaitActive("Title1", "", 5)
If(WinExists("Title1")) Then
 MsgBox(0, "", "Do something")
Else
 If(WinExists("Title2")) Then
  MsgBox(0, "", "Do something else")
 EndIf
EndIf

Because I don't want to have the timeout which could be more than 15 seconds.

因为我不希望超时可能超过 15 秒。

Thanks in advance!

提前致谢!

采纳答案by Copas

How about something like this.

这样的事情怎么样。

$stillLooking = True
While $stillLooking
    $activeWindowTitle = WinGetTitle(WinActive(""))
    If $activeWindowTitle == "Title1" Then
        MsgBox(0, "", "Do something")
        $stillLooking = False
    ElseIf $activeWindowTitle == "Title2" Then
        MsgBox(0, "", "Do something else")
        $stillLooking = False
    EndIf
    sleep(5)
WEnd

Because I don't want to have the timeout which could be more than 15 seconds.

因为我不希望超时可能超过 15 秒。

WinWaitActive() doesn't have a timeout unless you specify one. You gave it a five second timeout but you could leave that off and it would wait forever.

WinWaitActive() 没有超时,除非您指定超时。您给了它五秒钟的超时时间,但您可以将其关闭,它会永远等待。

回答by Bibz

A simpler solution might be to use a REGEX title in your WinWaitActiveas defined here : http://www.autoitscript.com/autoit3/docs/intro/windowsadvanced.htm

一个更简单的解决方案可能是在您的WinWaitActive定义中使用 REGEX 标题:http: //www.autoitscript.com/autoit3/docs/intro/windowsadvanced.htm

You would then have something like this:

然后你会有这样的事情:

$hWnd = WinWaitActive("[REGEXPTITLE:(WindowWithThisTitle|WindowWithThatTitle)]")

If WinGetTitle($hWnd) = "WindowWithThisTitle" then
    DoSomething()
Else
    DoSomethingElse()
EndIf

回答by Ahmad Alshaikh

You can use this Functions for two windows ..

您可以将此功能用于两个窗口..

; #FUNCTION# ====================================================================================================================
; Name...........: _2WinWait
; Description ...: Wait For Tow Windows .
; Syntax.........: _2WinWait ($FirstTitle,$SecondTitle,[$FirstText = "" ,[$SecondText = ""]] )
; Parameters ....: $FirstTitle  - Title Of First  Wondow 
;                  $SecondTitle - Title Of Second Wondow 
;                  $FirstText   - Text  Of First  Wondow 
;                  $SecondText  - Text  Of Second Wondow 
; Return values .: Success - None
;                  Failure - Returns a 0 => If Your Titles Is Wrong
; Author ........: Ashalshaikh : Ahmad Alshaikh
; Remarks .......: 
; Related .......:
; Link ..........;
; Example .......; No
; ===============================================================================================================================
Func _2WinWait ($FirstTitle,$SecondTitle,$FirstText = "" ,$SecondText = "" )
    If $FirstTitle = "" Or $SecondTitle = "" Then
        Return 0 
    Else
        Do 
        Until WinExists ($FirstTitle,$FirstText) Or WinExists ($SecondTitle,$SecondText)
    EndIf
EndFunc


; #FUNCTION# ====================================================================================================================
; Name...........: _2WinWait_Any 
; Description ...: Wait For Tow Windows And Return Any Window Id Exists .
; Syntax.........: _2WinWait_Any ($FirstTitle,$SecondTitle,[$FirstText = "" ,[$SecondText = ""]] )
; Parameters ....: $FirstTitle  - Title Of First  Wondow 
;                  $SecondTitle - Title Of Second Wondow 
;                  $FirstText   - Text  Of First  Wondow 
;                  $SecondText  - Text  Of Second Wondow 
; Return values .: Success - Number Of Window ==> 1= First Window , 2= Second Window
;                  Failure - Returns a 0 => If Your Titles Is Wrong
; Author ........: Ashalshaikh : Ahmad Alshaikh
; Remarks .......: 
; Related .......:
; Link ..........;
; Example .......; No
; ===============================================================================================================================
Func _2WinWait_Any ($FirstTitle,$SecondTitle,$FirstText = "" ,$SecondText = "" )
    If $FirstTitle = "" Or $SecondTitle = "" Then
        Return 0 
    Else
        Do 
        Until WinExists ($FirstTitle,$FirstText) Or WinExists ($SecondTitle,$SecondText)
        If WinExists ($FirstTitle,$FirstTexit) Then 
            Return 1 
        Else
            Return 2 
        EndIf
    EndIf
EndFunc

for more with examples

更多示例

回答by JustinD

I'm fairly new to autoit and the programming world in general and I had this same dilemma. Luckily I figured out a straight fwd way to do it:

总的来说,我对 autoit 和编程世界还很陌生,我也遇到过同样的困境。幸运的是,我想出了一个直接的方法来做到这一点:

Do
$var1 = 0
If  WinGetState("Document Reference","")    Then
    $var1 = 1
ElseIf  WinGetState("Customer Search","")   Then
    $var1 = 1
EndIf
Until $var1 = 1

So it'll stay in the loop until it finds the window and sets $var1to 1. There's probably easier ways (I'm sure developers are gasping at this) but this is straight fwd enough for me.

所以它会一直留在循环中,直到它找到窗口并设置$var1为 1。可能有更简单的方法(我确信开发人员对此感到喘不过气来)但这对我来说已经足够了。

回答by l--''''''---------''''''''''''

You can create an infinite while loop with if statements in there:

您可以使用 if 语句创建一个无限的 while 循环:

#include <MsgBoxConstants.au3>

Example()

Func Example()
    While 1
        ; Test if the window exists and display the results.
        If WinExists("Windows Security") Then
            Local $hWnd = WinWaitActive("Windows Security", "", 2000)
            ControlSetText($hWnd, "", "[CLASS:Edit; INSTANCE:1]", "hel233")
            ControlClick("Windows Security","","[CLASS:Button; INSTANCE:2]")
            Sleep(5000)
        EndIf

        ; Test if the window exists and display the results.
        If WinExists("Spread the Word") Then
            'The line below will wait until the window is active, but we don't need that
            'Local $hWnd = WinWaitActive("Spread the Word", "", 2000)
            WinClose("Spread the Word")
            Sleep(5000)
        EndIf



    wend
EndFunc