Jump to content
Sign in to follow this  
j_stam_84

UDF: Search window by control

Recommended Posts

I use this function when I know the name (partially) of a window and multiple windows with that substring could exist.

e.g. Fetch handle for an error window with the same title as the parent window based on a given control.

Please give opinions & suggestions! :(

I hope it's some use to someone!

grtz!

; In this example, the handle for the "AutoIT Help"  window is returned
$handle = WinSearchByControl("Help", "SysListView321")
MsgBox(64, "", $handle)

Func WinSearchByControl($subTitle, $control)
    Local $found = 0, $winList[1], $subTitle, $control

; Fetch name from control
    $exp = "([A-Z_ a-z]+)[0-9]?"
    $res = StringRegExp($control, $exp, 1)
    $controlName = $res[0]
    
; Get list of available windows
    $winList = WinList()

    For $i = 1 to $winList[0][0]
    ; Check window title for substring
        $exp  = "(" & $subTitle & ")"
        $result = StringRegExp($winList[$i][0], $exp, 0)
    
        If $result == 1 Then
        ; Fetch control list from window
            $classes = WinGetClassList($winList[$i][0])

        ; Check if control-type exists
            If StringInStr($classes, $controlName, 0, 1) Then

            ; Control-type exists, send ControlCommand
            ; If the control doesn't exist, @error will be set to 1
                ControlCommand($winList[$i][1], "", $control, "IsVisible", "")

                If @error == 0 Then
                ; Control exists. Set @extended and return handle for window
                    SetExtended(1)
                    Return $winList[$i][1]
                EndIf
            EndIf
        EndIf
    Next

; If this is parsed, no match is found.
; Set extended macro to 0 (nothing found)
; Set error macro to 0 (override the error-setting from ControlCommand)
    setExtended(0)
    Return ""

EndFunc
Edited by j_stam_84

Share this post


Link to post
Share on other sites

Sorry, I don't get it.

Perhaps a more detailed example would help.


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

That's okay.. I know my explanations are sometimes a little cryptic :):(

Okay, imagine you have an application (window) called "Foobar". At a certain point, this process spawns a child window for which you need the handle. The only problem is, this window contains exactly the same title as the parent window.

If this is the case, an option is to set the second argument of WinGetHandle ("The text of the window to read") to ensure you get the handle for that specific window.

But how would you retrieve the handle for an error window (for example) when it contains exactly the same title as the parent and the message displayed is 100% dynamic (e.g. multilangual; "Error" in English would be "Erreur" in French and "Fout" in Dutch).

This problem could be overcome using this function. The only thing you need to do, is find a control in that window that doesn't exist in the other window with the same title. It doesn't matter if it's visible or not, it only needs to exist and you need to know the "ClassNameNN". (you can easily do this with the program "Window Controls Grabber & Tester")

If there's a easier way to do this, I would be very interested :(

I hope this explanation is a bit better :

p.s. A bit of extra functionality in this function; the first argument can be the full windowtitle (like normal). But you could give a substring of the windowtitle aswell since some programs tend to have dynamic titles (e.g. "Untitled - Notepad" in which "Notepad" the only static part)

Edited by j_stam_84

Share this post


Link to post
Share on other sites

So it looks for windows with a title you specify (sub-string match) and returns the handle of the first one of those that contains the control that you specify (based on ClassNameNN).


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

Yes that's correct. That was a problem I faced so I created this UDF. Thought dat more people would have this problem.. apparently not ? :(

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...