Jump to content

ControlGetPos relative to screen?


Recommended Posts

Is there any way to get the screen coordinates of a control? It looks like ControlGetPos can only return the coords relative to the containing client area of the window.

The reason I ask is I need to grab the color of a pixel within a control. PixelGetColor can take coords relative to a window, but only if it's active, which I can't guarantee. So I need to give PixelGetColor the screen coords of the control instead. I've been looking around for a while, but didn't find anything. I hope someone can help!

Link to comment
Share on other sites

Wow, thank you for the instant reply! I was thinking along those lines. The problem is really the height of the titlebar can vary with different themes, which would throw off the calculation. But I just found the WinGetClientSize function which I think is the key I need...

I played around and came up with this which works! What's nice is the window doesn't need to be active and can even be behind another window...

$winPos = WinGetPos( $aWindowHandle )
        $clientSize = WinGetClientSize( $aWindowHandle )
        $winWidth = $winPos[2]
        $winHeight = $winPos[3]
        $clientWidth = $clientSize[0]
        $clientHeight = $clientSize[1]
        $winBorder = ( $winWidth - $clientWidth ) / 2  ;~ assume the border is even along the sides and bottom!
        $titleBarHeight = $winHeight - $clientHeight - $winBorder
        $controlPos = ControlGetPos( $aWindowHandle , "", "[CLASS:List; INSTANCE:2]" )
        $controlPosAbsX = $winPos[0] + $winBorder + $controlPos[0]
        $controlPosAbsY = $winPos[1] + $titleBarHeight + $controlPos[1]
        MsgBox( 0, "", "color: " & Hex( PixelGetColor( $controlPosAbsX + 8, $controlPosAbsY + 8 ), 6 ) )
        MouseMove( $controlPosAbsX + 4, $controlPosAbsY + 8 )
Edited by joncl
Link to comment
Share on other sites


I use ClientToScreen api call save calculating, example...

#include <GUIConstants.au3>

Global $state, $CGP, $CTS, $Gui, $listview, $button

$Gui = GUICreate("listview resize", 220, 200)
$listview = GUICtrlCreateListView ("col1  |col2|col3  ", 10, 10, 200, 150)
$button = GUICtrlCreateButton ("Resize",75,170,70,20)

While 1
    $msg = GUIGetMsg ()
        Case $msg = $GUI_EVENT_CLOSE
        Case $msg = $button
            $CGP = ControlGetPos($Gui, "", $listview)
            $CTS = ClientToScreen($Gui, $CGP[0], $CGP[1])
            MsgBox(0, "Before Resize", "Ctrl Pos X: " & $CGP[0] & @LF & "Ctrl Pos Y: " & $CGP[1] & @LF & _
                                            "Screen Pos X: " & $CTS[0] & @LF & "Screen Pos Y: " & $CTS[1])
            If Not $state Then
                $state = 1
                ControlMove($Gui, "", $listview, 40, 40, 140, 100)
                $state = 0
                ControlMove($Gui, "", $listview, 10, 10, 200, 150)
            $CGP = ControlGetPos($Gui, "", $listview)
            $CTS = ClientToScreen($Gui, $CGP[0], $CGP[1])
            MsgBox(0, "After Resize", "Ctrl Pos X: " & $CGP[0] & @LF & "Ctrl Pos Y: " & $CGP[1] & @LF & _
                                            "Screen Pos X: " & $CTS[0] & @LF & "Screen Pos Y: " & $CTS[1])

Func ClientToScreen($hWnd, $cX, $cY)
    Local $Point, $aRes[2]
    $Point = DllStructCreate("int;int")
    DllStructSetData($Point, 1, $cX)
    DllStructSetData($Point, 2, $cY)
    DllCall("User32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($Point))
    $aRes[0] = DllStructGetData($Point, 1)
    $aRes[1] = DllStructGetData($Point, 2)
    Return $aRes

But whatever works for you is best :)

Edited by smashly
Link to comment
Share on other sites

I'm looking all over for the "A3lib window api udf". Is this part of the "User Definied Fuctions" section in AutoIt Help? Does the api get installed with AutoIt? Well, at least I know what/where user32.dll is. :)

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...