Jump to content

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


Recommended Posts

Please help! 
 
I am using Autoit version 3.3.10.2 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.
 
 
post-72839-0-22888900-1406232387_thumb.p
 
 
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?

 

 
Link to comment
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.

Link to comment
Share on other sites

  • Developers

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

Jos

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

Link to comment
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)

post-72839-0-93779900-1406239546_thumb.p

 

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?

 

EDIT: 

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
Link to comment
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) 
"Title:=Desktop;controltype:=UIA_PaneControlTypeId;class:=#32769" 
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) 
"Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d" 
4: Title is: <> Class   := <WindowsForms10.Window.8.app.0.33c0d9d> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 
"Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d" 
3: Title is: <> Class   := <WindowsForms10.Window.8.app.0.33c0d9d> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 
"Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d" 
2: Title is: <> Class   := <WindowsForms10.Window.8.app.0.33c0d9d> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 
"Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d" 
1: Title is: <Status> Class   := <WindowsForms10.Window.8.app.0.33c0d9d> controltype:= <UIA_PaneControlTypeId> ,<50033> , (0000C371) 
"Title:=Status;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d" 
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) 
_UIA_Action($oP6,"setfocus")
Local $oP5=_UIA_getObjectByFindAll($oP6, "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d", $treescope_children) 
_UIA_Action($oP5,"setfocus")
Local $oP4=_UIA_getObjectByFindAll($oP5, "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d", $treescope_children) 
_UIA_Action($oP4,"setfocus")
Local $oP3=_UIA_getObjectByFindAll($oP4, "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d", $treescope_children) 
_UIA_Action($oP3,"setfocus")
Local $oP2=_UIA_getObjectByFindAll($oP3, "Title:=;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d", $treescope_children) 
_UIA_Action($oP2,"setfocus")
Local $oP1=_UIA_getObjectByFindAll($oP2, "Title:=Status;controltype:=UIA_PaneControlTypeId;class:=WindowsForms10.Window.8.app.0.33c0d9d", $treescope_children) 
_UIA_Action($oP1,"setfocus")
Local $oP0=_UIA_getObjectByFindAll($oP1, "Title:=Show details;controltype:=UIA_TextControlTypeId;class:=WindowsForms10.STATIC.app.0.33c0d9d", $treescope_children) 
_UIA_Action($oP0,"setfocus")
;~ 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)
_UIA_action($oUIElement,"click")
*** Detailed properties of the highlighted element ***
UIA_title:= <Show details>
UIA_iaccessiblevalue:= <Show details>
UIA_iaccessiblechildId:= <0>
UIA_BoundingRectangle:= <218;290;76;16>
UIA_ProcessId:= <6756>
UIA_ControlType:= <50005>
UIA_LocalizedControlType:= <hyperlink>
UIA_Name:= <Show details>
UIA_HasKeyboardFocus:= <False>
UIA_IsKeyboardFocusable:= <True>
UIA_IsEnabled:= <True>
UIA_Culture:= <0>
UIA_IsControlElement:= <True>
UIA_IsContentElement:= <True>
UIA_IsPassword:= <False>
UIA_NativeWindowHandle:= <0>
UIA_IsOffscreen:= <False>
UIA_Orientation:= <0>
UIA_IsRequiredForForm:= <False>
UIA_IsDockPatternAvailable:= <False>
UIA_IsExpandCollapsePatternAvailable:= <False>
UIA_IsGridItemPatternAvailable:= <False>
UIA_IsGridPatternAvailable:= <False>
UIA_IsInvokePatternAvailable:= <True>
UIA_IsMultipleViewPatternAvailable:= <False>
UIA_IsRangeValuePatternAvailable:= <False>
UIA_IsScrollPatternAvailable:= <False>
UIA_IsScrollItemPatternAvailable:= <False>
UIA_IsSelectionItemPatternAvailable:= <False>
UIA_IsSelectionPatternAvailable:= <False>
UIA_IsTablePatternAvailable:= <False>
UIA_IsTableItemPatternAvailable:= <False>
UIA_IsTextPatternAvailable:= <False>
UIA_IsTogglePatternAvailable:= <False>
UIA_IsTransformPatternAvailable:= <False>
UIA_IsValuePatternAvailable:= <True>
UIA_IsWindowPatternAvailable:= <False>
UIA_ValueValue:= <Show details>
UIA_ValueIsReadOnly:= <False>
UIA_RangeValueValue:= <0>
UIA_RangeValueIsReadOnly:= <True>
UIA_RangeValueMinimum:= <0>
UIA_RangeValueMaximum:= <0>
UIA_RangeValueLargeChange:= <0>
UIA_RangeValueSmallChange:= <0>
UIA_ScrollHorizontalScrollPercent:= <0>
UIA_ScrollHorizontalViewSize:= <100>
UIA_ScrollVerticalScrollPercent:= <0>
UIA_ScrollVerticalViewSize:= <100>
UIA_ScrollHorizontallyScrollable:= <False>
UIA_ScrollVerticallyScrollable:= <False>
UIA_SelectionCanSelectMultiple:= <False>
UIA_SelectionIsSelectionRequired:= <False>
UIA_GridRowCount:= <0>
UIA_GridColumnCount:= <0>
UIA_GridItemRow:= <0>
UIA_GridItemColumn:= <0>
UIA_GridItemRowSpan:= <1>
UIA_GridItemColumnSpan:= <1>
UIA_DockDockPosition:= <5>
UIA_ExpandCollapseExpandCollapseState:= <3>
UIA_MultipleViewCurrentView:= <0>
UIA_WindowCanMaximize:= <False>
UIA_WindowCanMinimize:= <False>
UIA_WindowWindowVisualState:= <0>
UIA_WindowWindowInteractionState:= <0>
UIA_WindowIsModal:= <False>
UIA_WindowIsTopmost:= <False>
UIA_SelectionItemIsSelected:= <False>
UIA_TableRowOrColumnMajor:= <2>
UIA_ToggleToggleState:= <2>
UIA_TransformCanMove:= <False>
UIA_TransformCanResize:= <False>
UIA_TransformCanRotate:= <False>
UIA_IsLegacyIAccessiblePatternAvailable:= <True>
UIA_LegacyIAccessibleChildId:= <0>
UIA_LegacyIAccessibleName:= <Show details>
UIA_LegacyIAccessibleValue:= <Show details>
UIA_LegacyIAccessibleRole:= <30>
UIA_LegacyIAccessibleState:= <1048576>
UIA_LegacyIAccessibleDefaultAction:= <Click>
UIA_IsDataValidForForm:= <False>
UIA_ProviderDescription:= <[pid:6756,hwnd:0x0 Main(parent link):Microsoft: MSAA Proxy (unmanaged:uiautomationcore.dll)]>
UIA_IsItemContainerPatternAvailable:= <False>
UIA_IsVirtualizedItemPatternAvailable:= <False>
UIA_IsSynchronizedInputPatternAvailable:= <False>
Link to comment
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

GUISetState(@SW_SHOW)

While 1
    ;_checkWIN()
    _nMGP()
WEnd

Func _nMGP()
    Local $_cpos = MouseGetPos()
    Sleep(0010)
    $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
        Else
            $wposy = $pos[1] + $yoffset ; Position label beneath the mouse pointer
        EndIf
        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
        Else
            $wposx = $pos[0] - $xoffset ; Use normal offsets for positioning label
        EndIf
    Else
        _clientmouse()
    EndIf
    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
    Else
        $wposy = $tpos[1] + $yoffset ; Position label beneath the mouse pointer
    EndIf
    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
    Else
        $wposx = $tpos[0] - $xoffset ; Use normal offsets for positioning label
    EndIf
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)
;EndFunc

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

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

×
×
  • Create New...