Jump to content

WinExist and WinClose?


Recommended Posts

Have two questions involving WinExist and WinClose.

First I am trying to see if 28 windows exist, they all have the same name and only the number changes after the name (example: window1, window2, etc...). So far what I have is, when I launch the program, it seems to detect if (example: window14) does not exist, then it calls the function. But after it is done checking all 28 windows exist, then it checks to see if any windows disappeared, it does not detect missing windows again, or at least not all the time. This is the code I am using as of now, maybe it can be improved or there is a better way?

While 1
$count = 1
Sleep(100)
Do
If WinExist("window" & $count) = 0 Then
Call("function")
EndIf
$count = $count + 1
Until $count = 28
WEnd

Second question, trying to close all windows with a certain name, but I do not know how many windows are present at any given time. So right now just have it count 28 times (cause that is the max number of times this window could be open). Does not to work very well ether, cause, leave the program running, come back to it, windows with that name will still be there, so wondering if there is a better way?

While 1
$close = 1
Sleep(100)
Do
WinClose("thewindow", "")
Sleep(100)
$close = $close +1
Until $close = 28
WEnd
Edited by logcomptechs
Link to comment
Share on other sites

You might try WinList() and use the array of window handles. That would be much more reliable:

$aWinList = WinList("AutoIt")
For $n = 1 to $aWinList[0][0]
    ConsoleWrite($n & ": hWnd = " & $aWinList[$n][1] & "; Title = " & $aWinList[$n][0] & @LF)
Next

:graduated:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

No, that's a 2D array with the count in [0][0], if there are no matches then [0][0] = 0:

$aWinList = WinList("AutoIt")
If $aWinList[0][0] Then
    For $n = 1 To $aWinList[0][0]
        ConsoleWrite($n & ": hWnd = " & $aWinList[$n][1] & "; Title = " & $aWinList[$n][0] & @LF)
    Next
Else
    MsgBox(16, "Error", "No matching windows found.")
EndIf

:graduated:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Still kinda confused, cause I need to check 1 through 28, lets say window1 is missing, I need to know what number is missing, in order to call my function properly. I understand that this list all available windows, then run through the array 28 times, to see if any numbers are missing?

Link to comment
Share on other sites

You can run through the array, but you could also use the array search. I wrote a stupid little test: I opened two notepad ("Kladblok" in Dutch) windows, one with a file1.txt and one with a file3.txt. Say I would want to know in the range test1.txt to test4.txt which windows are not present! (My script should show that 2 and 4 are not present ofcourse.) A way to do that would be:

#include <Array.au3>

$testarray = WinList("test")

_ArrayDisplay($testarray)

$amountOfWindowsFound = $testarray[0][0]

$resultstring = ""

For $i = 1 To 4
    $windowThatShouldBeThere = "test" & $i & ".txt - Kladblok"
    ConsoleWrite($windowThatShouldBeThere & @CRLF)
    If _ArraySearch($testarray, $windowThatShouldBeThere) == -1 Then
        $resultstring = $resultstring & $windowThatShouldBeThere & @CRLF
    EndIf
Next

MsgBox(64, "test", "Windows not found: " & @CRLF & $resultstring)

(Instead of putting it all into a string and msgboxing it you could ofcourse do whatever you want with the information, the point is that this correctly identifies which in the range are, and which are not present.)

Is this what you are looking for?

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

#include <Array.au3>

$testarray = WinList("window")

For $i = 1 To 28
    $windowThatShouldBeThere = "window" & $i
    If _ArraySearch($testarray, $windowThatShouldBeThere) == -1 Then
        Call("function")
    EndIf
Next

Just simplified it, would that be correct? All I want it to do is if window1 or window23, etc... is missing, then call the function.

Link to comment
Share on other sites

I don't think you want to use Call (you hardly ever need it)

#include <Array.au3>

$testarray = WinList("window")

For $i = 1 To 28
    $windowThatShouldBeThere = "window" & $i
    ;Call is not needed. Just write the function name instead.
    ;By avoiding Call, you can also pass parameters to a function. Passing the name of the missing window to the function could be usefull
    If _ArraySearch($testarray, $windowThatShouldBeThere) == -1 Then function($windowThatShouldBeThere)
Next

Func function($sWinName) ;I'd think of a better name then "function" :)
    Run("path to exe") ;launch a new window
    WinWait("old title","") ;wait for the program to launch
    WinSetTitle("old title","",$sWinName) ;rename that window
    ;the more reliable way to identify the right window to rename is using winlist and compare PID's. Post again if you need it.
EndFunc
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...