Jump to content
Sign in to follow this  

Please help? ControlGetPos is giving me the wrong position, different from Au3Info.exe

Recommended Posts

Please help! 
I am using Autoit version to automate a task where I need to click on a control on a window of a third party program. I am having a problem with the ControlGetPos function. When I call it with the correct information about the contol and its parent window, it reports the wrong value for the Y position, though the X position is correct.
When I run AU3INFO.EXE it reports the correct position, though. Please see the attached screen shot to understand what I mean.
Here is the source code for the example code that I am running in the attached screen shot. These are the only two lines in my example program, for simplicity (it assumes the window is already up on the screen in this example):
$ControlCoordinates = ControlGetPos("Diagnostic Report", "", "[NAME:m_Details]")
MsgBox(0, "Output from my AutoIt Test Script", "Autoit code believes the control to be at this location: " & @CRLF & $ControlCoordinates[0] & "," & $ControlCoordinates[1])

(Note: I have tried putting Opt("MouseCoordMode", 2) at the top of the code and it does not change the value, regardless of whether I do 0, 1, or 2 for the mode.)


As you can see from the screen shot, AutoIt is getting "140" for the Y position. But the same control, when use AU3INFO.EXE to obtain its location, reports 164 for the Y position.

I'm sure we're talking about the same control here. When I hover over the control with AU3INFO.EXE it reports the name "[NAME:m_Details]" for that control, and that is what I put into the code. There are no other controls on the window with that identifier, it is unique to that control in the example I have shown.

The reason this is a problem is that I need to send a MouseClick to that place on the screen (ControlClick doesn't work for this program, I don't know why) and with MouseClick it's imperative that I get the position correct. It's not coming out correct this way and the mouse is clicking 24 pixels too high to register.

What's wrong with the ControlGetPos function, and more importantly, why does AU3INFO.EXE get it right when it was written by the same people? What is AU3INFO doing right that AutoIt's ControlGetPos function is not doing right? How can I fix this so my code works correctly in all cases?



Share this post

Link to post
Share on other sites

I also tried changing the code to:

$ControlCoordinates = ControlGetPos("Diagnostic Report", "", "[TEXT:Show details]")

And it does the same thing, getting the incorrect "140" instead of the correct "164" for the position.

Share this post

Link to post
Share on other sites

Also, I can tell it is not messing up based on the windows title bar size. If it were, the number would be in the 200s, not 164 or 140.

For instance, if a take the screen shot and crop it down to just the client area rectangle of the window, then the control sits pretty much exactly at 116,164 as AU3INFO says it should.

Share this post

Link to post
Share on other sites

What is returned for Width and Height for these functions: WinGetClientSize() and WinGetSize()  ?


edit: there is no need to keep on adding posts to your thread. ;)

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
Live for the present,
Dream of the future,
Learn from the past.

Share this post

Link to post
Share on other sites
What is returned for Width and Height for these functions: WinGetClientSize() and WinGetSize() ?



I could not find a WinGetSize function so I used the width and height of WinGetPos instead, I'm hoping that's what you meant. Note that in this run, I show a screen shot where the window I'm working with is a different position and size than I showed in my last example. But I get the same basic problem (and the same answers) no matter what size or position the window occupies on the screen.


Updated screen shot and code:

$ControlCoordinates = ControlGetPos("Diagnostic Report", "", "[TEXT:Show details]")
$WinGetClientSize = WinGetClientSize("Diagnostic Report")
$WinGetPos = WinGetPos("Diagnostic Report")
MsgBox(0, "Output from my AutoIt Test Script", "Autoit code believes the control to be at this location: " & @CRLF & $ControlCoordinates[0] & "," & $ControlCoordinates[1] & @CRLF & @CRLF & $WinGetClientSize[0] & "," & $WinGetClientSize[1] & "<- Client Size" & @CRLF & $WinGetPos[2] & "," & $WinGetPos[3] & "<- Get Pos")

WinActivate("Diagnostic Report")
Opt("MouseCoordMode", 0)   ; This is actually WORSE if I do window-relative (0) instead of client-area-relative (2).
MouseMove ($ControlCoordinates[0], $ControlCoordinates[1], 0)



The difference in the Y direction betwen WinGetClientSize and WinGetPos is 45. This difference is the same no matter where on the screen the window is, or what size it is, when I run it, the difference is always 45.

Not sure if that's relevant though. For example, if I add the code in (shown above now) to move the mouse to the upper left corner of that control, if I do it Window-relative (MouseCoordMode =0) instead of ClientArea-relative (MouseCoordMode =2), then it's actually WORSE that way, with the pointer being even higher up on the screen than it should be.

Interesting note!:

I just got done writing a bunch of C# code which tried to do the same thing using the API functions GetWindowRect and MapWindowPoints. Guess what? That code got the same answer as the AutoIt script got! In other words, the wrong answer of 116,140.

So what is AU3INFO doing that's special, that gets the answer more right than either AutoIt or GetWindowRect/MapWindowPoints?



Updated information: This seems to be related to the number of lines of text which appear in the box along with the "Show details" link. (The example screen I have shown can have different numbers of lines of summary text in the box before the link I'm trying to click on.)

If there are more lines of text in the box, then the error between where it should be clicking and where it is clicking, is even greater. 

Edited by tfabris

Share this post

Link to post
Share on other sites

Simplespy puts out 218;290 for the bounding rectangle of the object I'm after. However I think those were absolute screen coordinates, not window-relative coordinates, and I don't see the window's screen position in the output so I don't know where to do the math to get the window-relative coordinates in this case. Here is the full output:

Mouse position is retrieved 279-300
At least we have an element [Show details][]
Having the following values for all properties: 
Title is: <Show details> Class   := <> controltype:= <UIA_HyperlinkControlTypeId> ,<50005> , (0000C355) 
*** Parent Information top down ***
7: Title is: <Desktop> Class   := <#32769> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 
6: Title is: <Diagnostic Report> Class   := <WindowsForms10.Window.8.app.0.33c0d9d> controltype:= <UIA_WindowControlTypeId> ,<50032> , (0000C370) 
"Title:=Diagnostic Report;controltype:=UIA_WindowControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d" 
5: Title is: <> Class   := <WindowsForms10.Window.8.app.0.33c0d9d> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 
4: Title is: <> Class   := <WindowsForms10.Window.8.app.0.33c0d9d> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 
3: Title is: <> Class   := <WindowsForms10.Window.8.app.0.33c0d9d> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 
2: Title is: <> Class   := <WindowsForms10.Window.8.app.0.33c0d9d> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 
1: Title is: <Status> Class   := <WindowsForms10.Window.8.app.0.33c0d9d> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 
0: Title is: <Show details> Class   := <WindowsForms10.STATIC.app.0.33c0d9d> controltype:= <UIA_TextControlTypeId> ,<50020> , (0000C364) 
"Title:=Show details;controltype:=UIA_TextControlTypeId;class:=WindowsForms10.STATIC.app.0.33c0d9d" 
*** Standard code ***
#include "UIAWrappers.au3"
AutoItSetOption("MustDeclareVars", 1)

Local $oP6=_UIA_getObjectByFindAll($UIA_oDesktop, "Title:=Diagnostic Report;controltype:=UIA_WindowControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d", $treescope_children) 
Local $oP5=_UIA_getObjectByFindAll($oP6, "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d", $treescope_children) 
Local $oP4=_UIA_getObjectByFindAll($oP5, "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d", $treescope_children) 
Local $oP3=_UIA_getObjectByFindAll($oP4, "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d", $treescope_children) 
Local $oP2=_UIA_getObjectByFindAll($oP3, "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d", $treescope_children) 
Local $oP1=_UIA_getObjectByFindAll($oP2, "Title:=Status;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d", $treescope_children) 
Local $oP0=_UIA_getObjectByFindAll($oP1, "Title:=Show details;controltype:=UIA_TextControlTypeId;class:=WindowsForms10.STATIC.app.0.33c0d9d", $treescope_children) 
;~ First find the object in the parent before you can do something
;~$oUIElement=_UIA_getObjectByFindAll("Showdetails.mainwindow", "title:=Show details;ControlType:=UIA_HyperlinkControlTypeId", $treescope_subtree)
Local $oUIElement=_UIA_getObjectByFindAll($oP0, "title:=Show details;ControlType:=UIA_HyperlinkControlTypeId", $treescope_subtree)
*** Detailed properties of the highlighted element ***
UIA_title:= <Show details>
UIA_iaccessiblevalue:= <Show details>
UIA_iaccessiblechildId:= <0>
UIA_BoundingRectangle:= <218;290;76;16>

Share this post

Link to post
Share on other sites

This is a stand alone script that I have modified to give X,Y,HexColor,DECColor

#include <GuiConstants.au3>
#include <date.au3>

Opt("GUIOnEventMode", 1)
Opt("WinTitleMatchMode", 4) ; set title matchmode to advanced

Global $xoffset = 37
Global $yoffset = 23
Global $wposx, $wposy, $DT, $cWIN, $xcalcL, $xcalcR, $ycalc, $cpos, $pos, $tpos, $hexV, $pcolor
Global $title = "Your Title Here"

HotKeySet("{ESC}", "Terminate") ; a way out of this
;HotKeySet("!f", "_color"); check for color

$pGUI = GUICreate($title, 300, 13, -1, -1, $WS_POPUP);Make a GUI next to mouse
$startLAB = GUICtrlCreateLabel(" X: 0000, Y: 0000", 0, 0, 300, 13);Fill GUI with coords
GUICtrlSetBkColor($startLAB, 0xCCFFFF);Background color of GUI window

WinSetOnTop($pGUI, "", 1);Make GUI topmost


While 1

Func _nMGP()
    Local $_cpos = MouseGetPos()
    $pcolor = PixelGetColor($_cpos[0],$_cpos[1])
    $pos = MouseGetPos()
    GUICtrlSetData($startLAB, ' X: ' & $pos[0] & ', Y: ' & $pos[1] & ' HEX: ' & HEX($pcolor,6) & ' DEC: ' & $pcolor)
    If $DT = 1 Then
        $xcalcR = .95 * @DesktopWidth
        $xcalcL = .05 * @DesktopWidth
        $ycalc = .90 * @DesktopHeight
        If $pos[1] > $ycalc Then
            $wposy = $pos[1] - $yoffset * 2 ; If too close to bottom of screen position above mouse pointer
            $wposy = $pos[1] + $yoffset ; Position label beneath the mouse pointer
        If $pos[0] > $xcalcR Then ; If too close to right side of screen move it
            $wposx = $pos[0] - $xoffset * 3
        ElseIf $pos[0] < $xcalcL Then ; If too close to left side of screen move it
            $wposx = $pos[0] + 10
            $wposx = $pos[0] - $xoffset ; Use normal offsets for positioning label
    WinMove($title, "", $wposx, $wposy) ; Move the label appropriate to follow the mouse
EndFunc   ;==>_nMGP

;Func _checkWIN()
;    $cWIN = WinGetTitle("[ACTIVE]")
;    If $cWIN <> $title Then
;        TrayTip("", $cWIN, 30)
;        Opt("MouseCoordMode", 0)
;        $DT = 0
;    Else
;        TrayTip("", "DESKTOP", 30)
;        Opt("MouseCoordMode", 1)
;        $DT = 1
;    EndIf
;EndFunc   ;==>_checkWIN

Func _clientmouse()
    Opt("MouseCoordMode", 1)
    $tpos = MouseGetPos()
    $cpos = WinGetPos($cWIN)
    $xcalcR = .95 * $cpos[2]
    $xcalcL = .05 * $cpos[2]
    $ycalc = .90 * $cpos[3]
    If $tpos[1] > $ycalc Then
        $wposy = $tpos[1] - $yoffset * 2 ; If too close to bottom of screen position above mouse pointer
        $wposy = $tpos[1] + $yoffset ; Position label beneath the mouse pointer
    If $tpos[0] > $xcalcR Then ; If too close to right side of screen move it
        $wposx = $tpos[0] - $xoffset * 3
    ElseIf $tpos[0] < $xcalcL Then ; If too close to left side of screen move it
        $wposx = $tpos[0] + 10
        $wposx = $tpos[0] - $xoffset ; Use normal offsets for positioning label
EndFunc   ;==>_clientmouse

;Func _color()
;    Local $_cpos = MouseGetPos()
;    Local $pcolor
    ; - ;$pcolor = PixelGetColor($_cpos[0],$_cpos[1])
;    $hexV = "0x" & Hex($pcolor,6)
;    Local $fOpen = FileOpen(@ScriptDir & "\color.txt",1)
;    FileWrite($fOpen,@CRLF & _NowDate() & " " & _NowTime() & " : " & "X: " & $_cpos[0] & _
;        "," & "Y: " & $_cpos[1] & " | " & "Color: " & $hexV)
;    FileClose($fOpen)

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

Keep this. It has helped me countless times in getting x and y positions

Edited by computergroove

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

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.

  • Similar Content

    • By learner123
      Hi All,
      So I have created a small autoIT script to enter pin into a RSA token(app which generate new code every 30 second), and copy the generated code.
      I have a java application which requires this code so every time my java-code requires this RSA code, it runs the autoIT script and the copied generated code is then used in my java application. 
      I have deployed this code on a windows server and it works fine when I am logged in and the window is on focus, But as soon as I schedule task and disconnect the server (not logged out only disconnect), or even minimize the server window, the autoIT scripts fails and its not able to copy the value.
      Please find below the code for AUTOIT.
      WinActivate("rsa - RSA SecurID Token") ; activates the window that has old in the tilte bar
      WinWaitActive("rsa - RSA SecurID Token") ; waits until the window is the active window
      Send("1111") ; simulates pressing the Home key, enters password to get the code
      Send("{ENTER}") ; simulates pressing the Enter key
      Sleep(1000) ;
      Send("^c") ; simulates pressing the CTRL+c keys (copy)
      Also I saw some post regarding that WINACTIVE only works when window is active. But my below AUTO IT script to handle windows pop up  works perfectly fine when the server is disconnected. 
      Opt("WinTitleMatchMode", 1)
    • By autotin
      Hello I am a first time user of AutoIT with little coding experience and I would REALLY really appreciate your help.
      I work in a warehouse where I am responsible for data entry along with a slew of other different tasks such as auditing and managing paperwork. Most of my time is tied up entering info to close receipts of product orders (8-12+ hours for thousands of products). The quantity actually produced by the warehouse is sometimes different from the quantity listed on the reciept (in this case 20). I need to change all the build qty for sublocation "1-WIP" from 20 to the actual quantity 22 and set all other numbers as 0. The form is an iframe coded with jQuery and freezes for 1-2 seconds after every entry and refreshes. As such, it doesn't allow me to use tab and I need to click and type every incorrect field.
      I have attached an image as an example from the webapp, FinaleInventory.
      I'm thinking that I probably need a macro that has a message box entry where I can type the corrected number entry I want. Then it uses image/character recognition (Tesseract?) to find "WIP" sublocation =,   Move the mouse over to the right to "Quantity" relative to location of WIP If quantity number = entry, then move to next "WIP" image (Not quite sure how to make Tesseract "find next") Elseif quantity number ≠ entry then click quantity number of "WIP" and type corrected entry Scroll down Loop until bottom of page I would greatly appreciate any help, ideas or pointers for this project. Thank you!
      -A newbie scripter

    • By Rammanan
      Hi all, Below is my script,
      Pls advise me if any wrong on that....File exit in location but the return -1.
      FileOpen($file, 0)
         $line = FileReadLine($file)
      Local $aArray = _FileListToArrayRec($serverpath, "*|*.txt", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_NOPATH)
      If @error Then
              MsgBox($MB_SYSTEMMODAL, "Ooops!", "No files found")
          ;_ArrayDisplay($aArray, "Sorted tree")
                 Local $hSearch = FileFindFirstFile($line)
    • By JNutt
      I'm trying ot use controlClick to click on button. So I'm using the Window Info Finder tool. But I noticed that the control ID in windowInfo changes each time I restart the app.  
      Am I doing this wrong.
    • By Theodoor
      I am trying to use the ControlClick in a "save as"-window to select the path of where to save the document.
      I found out that when in press the icon in front of the path, i select the path. So I am trying to press that button (i have attached a print screen of this window).
      While trying to figure out how i had to use the ControlClick, it said somewhere that i could us Au3Info to find out what title,... i should use with the ControlClick.
      The result of the Au3Info I have attached aswel.
      The ControlClick that i tried are these: (and none of these even move the mouse)
      ControlClick("[CLASS:ToolbarWindow32; INSTANCE:4]", "", "", "Left" ,1 ,10,10)
      ControlClick("[CLASS:ToolbarWindow32; INSTANCE:4]", "", "1001", "Left" ,1 ,10,10)
      ControlClick("[CLASS:ToolbarWindow32]", "", "", "Left" ,1 ,10,10)
      ControlClick("Save project as", "", "", "Left" ,1 ,10,10)
      I don't know what I am doing wrong.
      Should I use a controlID? is my windowtitle wrong?
      Can anyone help me?
      Thanks in advance,

  • Create New...