Jump to content

Java AWT frame control


jlundqui
 Share

Recommended Posts

So, I need to run multiple AutoIt scripts, each controlling a uniquely named application, with no deliberate crosstalk or accidental cross-control. I've done a proof of concept using the Windows calculator, and it works just fine. The problem I'm having is that the application(s) I need to control are Java AWT frames.

I've tried using SmOke_N's _CtrlGetByPos, which works for the Calculator (but only with the Control Handle), and presumably for Javascript in IE, but not for an independent Java app. I've tried getting the ClassNameNN, Control ID, and Control Handle, and each return 0, but don't throw an error.

Moving the mouse to the correct location and clicking works, but there is no guarantee that another script won't make its own app active before the click, thus causing one script to interact with another script's app window. The app activity has a shortcut key, so I could try a Send. It would be faster than moving the mouse, but still has a possibility of improper interactions.

So, is there some other option? Can a control ID be assigned to a location, or is there a Send function that specifies the window to send to? Any ideas? Unfortunately, I need some resolution asap.

The Calculator example using _CtrlGetByPos is below. Unfortunately, I can't send any specific info about the Java app I'm dealing with.

Opt("WinWaitDelay",100)
Opt("WinTitleMatchMode",4)
Opt("WinDetectHiddenText",1)
Opt("MouseCoordMode",2)

Run("calc.exe")

WinWaitActive("Calculator")
WinSetTitle("Calculator","","Calc1")

Local $hndl,$ctrl1,$ctrl2 

$hndl = WinGetHandle("Calc1")
WinMove($hndl,"", 50, 50)

$ctrl1 = ControlGetHandle($hndl,"","[CLASS:Button; INSTANCE:7]")
ConsoleWrite("Control 1: " & $ctrl1 & @CRLF)
$ctrl2 = _CtrlGetByPos($hndl,"",190,188,2)
if @error Then
    ConsoleWrite("error: " &@error & @CRLF)
EndIf
ConsoleWrite("Control 2: " & $ctrl2 & @CRLF)

While WinExists($hndl)
    ControlClick($hndl,"",$ctrl1)
    Sleep(1000)
    ControlClick($hndl,"",$ctrl2)
    Sleep(1000)
WEnd


;===============================================================================
; Function Name:    _CtrlGetByPos()
; Description:    Get the Control ID or the ClassNameNN by the X and Y client coordinates
; Syntax:          _CtrlGetByPos("Window Title", [Control Text], [X Client Coord], [Y Client Coord], [Return Type])
;
; Parameter(s):  $hWin              = Window Name or Handle
;                  $sText           = Text of the Control
;                  $iXPos           = X Client Coord
;                  $iYPos           = Y Client Coord
;                  $iReturnType  = Return type (default = 0 (ClassNameNN))
;                                0 = ClassNameNN
;                                1 = Control ID
;                                2 = Control Handle
;                                3 = All 3 ClassNameNN, Control ID, Control Handle
;
;
; Requirement(s):   External:    = None.
;                  Internal:     = AutoIt Beta 3.1.130 (or which ever release SetError(0,0,0) was introduced)
;
; Return Value(s):  On Success:   = Returns Array List
;                  On Failure:   = @error 1 (Control was found but there was an error with the DLLCall)
;                  On Failure:    = @error 2 (No classes were found)
;
; Author(s):        SmOke_N
;
; Note(s):        Similar to LxP's here (This was made before I remembered he had done something similar)
;              http://www.autoitscript.com/forum/index.php?showtopic=14323&hl=
;              
;
; Example(s):
;   Opt('WinTitleMatchMode', 4)
;   _CtrlGetByPos('classname=SciTEWindow', '', 829, 504, 2)
;===============================================================================

Func _CtrlGetByPos($hWin, $sText = '', $iXPos = 0, $iYPos = 0, $iReturnType = 0)
    If IsString($hWin) Then $hWin = WinGetHandle($hWin)
    Local $sClassList = WinGetClassList($hWin), $hCtrlWnd
    Local $sSplitClass = StringSplit(StringTrimRight($sClassList, 1), @LF), $aReturn = ''
    For $iCount = UBound($sSplitClass) - 1 To 1 Step - 1
        Local $nCount = 0
        While 1
            $nCount += 1
            Local $aCPos = ControlGetPos($hWin, $sText, $sSplitClass[$iCount] & $nCount)
            If @error Then ExitLoop
            If $iXPos >= $aCPos[0] And $iXPos <= ($aCPos[0] + $aCPos[2]) _
                    And $iYPos >= $aCPos[1]  And $iYPos <= ($aCPos[1] + $aCPos[3]) Then
                If $sSplitClass[$iCount] <> '' And Not $iReturnType Then
                    Local $aClassNN[2] = [2, $sSplitClass[$iCount] & $nCount]
                    Return $aClassNN
                EndIf
                If $sSplitClass[$iCount] <> '' And $iReturnType = 3 Then
                    $hCtrlWnd = ControlGetHandle($hWin, $sText, $sSplitClass[$iCount] & $nCount)
                    ControlFocus($hWin, $sText, $hCtrlWnd)
                    $aReturn = DllCall('User32.dll', 'int', 'GetDlgCtrlID', 'hwnd', $hCtrlWnd)
                    If @error = 0 And $aReturn[0] <> '' Then
                        Local $aClassNN[4] = [4, $aReturn[0], $sSplitClass[$iCount] & $nCount, $hCtrlWnd]
                        Return $aClassNN
                    EndIf
                    Local $aClassNN[2] = [2, $sSplitClass[$iCount] & $nCount]
                    Return $aClassNN
                ElseIf $sSplitClass[$iCount] <> '' And $iReturnType = 2 Then
                    Return ControlGetHandle($hWin, $sText, $sSplitClass[$iCount] & $nCount)
                ElseIf $sSplitClass[$iCount] <> '' And $iReturnType = 1 Then
                    $hCtrlWnd = ControlGetHandle($hWin, $sText, $sSplitClass[$iCount] & $nCount)
                    ControlFocus($hWin, $sText, $hCtrlWnd)
                    $aReturn = DllCall('User32.dll', 'int', 'GetDlgCtrlID', 'hwnd', $hCtrlWnd)
                    If @error = 0 And $aReturn[0] <> '' Then
                        Local $aClassNN[2] = [2, $aReturn[0]]
                        Return $aClassNN
                    EndIf
                EndIf
                Return SetError(1, 0, 0)
            EndIf
        WEnd
    Next
    Return SetError(2, 0, 0)
EndFunc
Link to comment
Share on other sites

  • 1 year later...

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