Jump to content
Sign in to follow this  
ccroc

control of 2 windows with same title

Recommended Posts

ccroc

My problem is that i need to send controls (ControlSend, ControlClick...) to 2 different instances of the same application.

For instance, to begin with something simple, i run 2 notepads (same window title), and i want to write text alternatively in the 1rst and in the 2nd window. As they do have the same title, i always write in the 2nd one.

Is there a solution ? (it seems that the pid can't be used in ControlSend or ControlClick)

Share this post


Link to post
Share on other sites
exodius

Using the example from: http://www.autoitscript.com/forum/index.php?showtopic=102785

$varPID = Run ("notepad")
WinWait ("Untitled -")
$hwnd = _GetHwnd($varPID)

$varPID2 = Run ("notepad")
WinWait ("Untitled -")
$hwnd2 = _GetHwnd($varPID2)

Sleep (1000)

WinMove ($hwnd, "", 100, 100, 500, 300)
WinMove ($hwnd2, "", 100, 500, 500, 300)

Sleep (1000)

ControlSend ($hwnd, "", "Edit1", "Why hello there!" & @CR, 1)
ControlSend ($hwnd2, "", "Edit1", "Hello to you, how's it going?" & @CR, 1)
ControlSend ($hwnd, "", "Edit1", "Oh quite well, and you?" & @CR, 1)
ControlSend ($hwnd2, "", "Edit1", "No complaints here." & @CR, 1)

Func _GetHwnd($id,$txt="")   ;Retrieve Hwnd of process
    $proc = 0
    If _IsPIDOrProc($id) Then
        $proc = _PIDOrProcToHwnd($id)
    ElseIf _IsWinTitle($id,$txt) Then
        $proc = _WinTitleToHwnd($id,$txt)
    EndIf
    Return $proc
EndFunc

Func _IsPIDOrProc(ByRef $id)  ;Is running PID or Processname
    If Not ProcessExists($id) Then
        Return 0
    Else
        Return $id
    EndIf
EndFunc

Func _IsWinTitle(ByRef $id,$txt="")  ;Is running Window Title
    $win = WinGetTitle($id,$txt)
    If Not $win Then Return 0
    $id = $win
    Return 1
EndFunc

Func _IsWinVisible($handle)   ;Is Window Visible
    If BitAnd( WinGetState($handle), 2 ) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc

Func _PIDOrProcToHwnd($proc)   ;Convert PID or process to Hwnd
    If ProcessExists($proc) <> $proc Then
        $proclist = ProcessList($proc)
        $proc = $proclist[1][1]
    EndIf
    $var = WinList()
    For $i = 1 to $var[0][0]   ;Pair PID/Process with Window Title
        If $var[$i][0] <> "" AND _IsWinVisible($var[$i][1]) Then
            If WinGetProcess($var[$i][0]) = $proc Then $proc = WinGetHandle($var[$i][0])
        EndIf
    Next
    Return $proc
EndFunc

Func _WinTitleToHwnd($proc,$txt="")   ;Convert Window title to Hwnd
    $winlist = WinList($proc,$txt)
    If Not $winlist[0][0] Then Return -1
    Return $winlist[1][1]
EndFunc
Edited by exodius

Share this post


Link to post
Share on other sites
ccroc

Thanks a lot for your quick response, i tried your example and it works fine!

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  

×