INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Get Control Position ref Parent

Get Control Position ref Parent

(OP)
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?

RE: Get Control Position ref Parent

Well, if you like

CODE

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
    
    ' API all working in screen coordinates, i.e. pixels
    ' So, if result required in twips, must convert
    GCPRF.x = ScaleX(myRect.Left - myPoint.x, vbPixels, vbTwips)
    GCPRF.y = ScaleY(myRect.Top - myPoint.y, vbPixels, vbTwips)
End Function 

This should be more accurate, as your code fails to take borders into account

RE: Get Control Position ref Parent

(OP)
Like very much. Thanks for your help again.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close