Jump to content

_CtrlGetByPos()


SmOke_N
 Share

Recommended Posts

  • Moderators

I did this when someone was saying that their Control ID and ClassNameNN's were changing on every new application execution.

It's pretty self explanitory, must be Client Coords is the only thing I can think of you would need to know.

;===============================================================================
; 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)
EndFuncoÝ÷ ØLZ^jëh×6Opt('WinTitleMatchMode', 4)
Global $Control_To_Interact_With_Is = _CtrlGetByPos('classname=SciTEWindow', '', 829, 504, 3)
If IsArray($Control_To_Interact_With_Is) Then
    For $i = 1 To UBound($Control_To_Interact_With_Is) - 1
        MsgBox(0, '', $Control_To_Interact_With_Is[$i])
    Next
EndIf
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Oooh thanks Im sure I could use, will start incorperating as soon as I get test machine running again.

[quote name='DaleHohm']You have a strange habit of posting error messages that don't match your code.[/quote][quote name='SmOke_N']Forget the learning... straight to the scripting :lol: (laugh.gif)[/quote]

Link to comment
Share on other sites

  • 2 months later...
  • 1 month later...

I recently discovered that this function is no longer working in any of my scripts. Is there a way that someone can test this function with the latest version of Scite and AutoIt. Thanks.

It's working fine with latest beta 3.2.1.14

You have probably old version, see this:

; Requirement(s): External: = None.

; Internal: = AutoIt Beta 3.1.130 (or which ever release SetError(0,0,0) was introduced)

Link to comment
Share on other sites

It's working fine with latest beta 3.2.1.14

You have probably old version, see this:

I have the latest version but it seems to be working in weird ways at times.

The code above stopped working completely so I used what appears to be the old version pasted below. This version grabs controls but never the correct one.

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, '', $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 Return $sSplitClass[$iCount] & $nCount

If $sSplitClass[$iCount] <> '' And $iReturnType = 2 Then

$hCtrlWnd = ControlGetHandle($hWin, '', $sSplitClass[$iCount] & $nCount)

ControlFocus($hWin, '', $hCtrlWnd)

$aReturn = DllCall('User32.dll', 'int', 'GetDlgCtrlID', 'hwnd', $hCtrlWnd)

If @error = 0 And $aReturn[0] <> '' Then _

Return 'ControlID: ' & $aReturn[0] & @CRLF & 'ClassNameNN: ' & $sSplitClass[$iCount] & $nCount

Return 'ClassNameNN: ' & $sSplitClass[$iCount] & $nCount

ElseIf $sSplitClass[$iCount] <> '' And $iReturnType = 1 Then

$hCtrlWnd = ControlGetHandle($hWin, '', $sSplitClass[$iCount] & $nCount)

ControlFocus($hWin, '', $hCtrlWnd)

$aReturn = DllCall('User32.dll', 'int', 'GetDlgCtrlID', 'hwnd', $hCtrlWnd)

If @error = 0 And $aReturn[0] <> '' Then Return $aReturn[0]

EndIf

Return ''

EndIf

WEnd

Next

Return ''

EndFunc

Edited by spyro

www.SecurityDistro.com

Link to comment
Share on other sites

  • Moderators

I have the latest version but it seems to be working in weird ways at times. It keeps getting Control names for things around it even though the mouse chords are clearly correct.

Are you using MouseCoordMode 2 and Client Coords?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Are you using MouseCoordMode 2 and Client Coords?

I am using the data from AutoIt window

>>>>>>>>>>> Mouse Details <<<<<<<<<<<

Screen: X: 300 Y: 30

and calling it like this:

$controlname = _CtrlGetByPos('Test App','',300,30,0)

Is this correct?

www.SecurityDistro.com

Link to comment
Share on other sites

  • Moderators

I am using the data from AutoIt window

>>>>>>>>>>> Mouse Details <<<<<<<<<<<

Screen: X: 300 Y: 30

and calling it like this:

$controlname = _CtrlGetByPos('Test App','',300,30,0)

Is this correct?

No, you are using "Screen" coords.

The coordinates that a control uses are Client Coords. Change it under options to get the coords for Client.

Don't forget to use Opt("MouseCoordMode", 2) as well at the top of your script or where you need it.

Edit:

You don't need Opt("MouseCoordMode", 2) just the client coords of the control you want to get. I've lost my mind it seems this morning.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

That fixed a portion of my script since a coord was not correct because of the settings. The only other thing is that the code from the first post doesn't work but the other code I posted does.

Also does Cursor ID matter? I have a large area that gives a cursor ID of 5 but it grabs the Control Name of another area which has a control id of 2. This is when using the Client mouse setting.

Edited by spyro

www.SecurityDistro.com

Link to comment
Share on other sites

  • 1 year later...
  • Moderators

It's great work, but, do not work for me :)

Any reason to post it all over the place? http://www.autoitscript.com/forum/index.ph...&pid=495790

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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