Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Control Position ref Parent 1

Status
Not open for further replies.

HughLerwill

Programmer
Nov 22, 2004
1,818
0
0
GB
I have the following which works well but I'm sure there a couple of API's which would do the job more smoothly

Code:
Private Function GetControlPosRefForm(ByVal Ctrl As Object) As POINTAPI

    'Returns the position of a Control, possibly nested inside Containers, with reference to the coordinates of the Parent Form's client area
    'Only viable if ScaleMode on the Form and all Containers is VbTwips

    Dim x!, y!

    x = Ctrl.Left
    y = Ctrl.Top
    
    Do Until TypeOf Ctrl.Container Is Form
        Set Ctrl = Ctrl.Container
        x = x + Ctrl.Left
        y = y + Ctrl.Top
    Loop
    GetControlPosRefForm.x = x
    GetControlPosRefForm.y = y

End Function

Any ideas?
 
Well, if you like

Code:
[blue]Private Function GCPRF(ByVal Ctrl As Object) As POINTAPI
    Dim myPoint As POINTAPI
    Dim myRect As RECT
    
    GetWindowRect Ctrl.hwnd, myRect
    ClientToScreen Ctrl.Parent.hwnd, myPoint
    
    [green]' API all working in screen coordinates, i.e. pixels
    ' So, if result required in twips, must convert[/green]
    GCPRF.x = ScaleX(myRect.Left - myPoint.x, vbPixels, vbTwips)
    GCPRF.y = ScaleY(myRect.Top - myPoint.y, vbPixels, vbTwips)
End Function[/blue]

This should be more accurate, as your code fails to take borders into account
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top