Results 1 to 8 of 8

Thread: Getting the absolute position of a control without knowing its parent

  1. #1

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Getting the absolute position of a control without knowing its parent

    Is there anyway to get the screen coords of a control that doesn't have an hWnd and if you don't know its parent control? I am trying to work on a class and I need to know the top and left of a control relative to a screen (not the window) without passing an hWnd.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Getting the absolute position of a control without knowing its parent

    Do you mean that a control will be passed as a parameter to a sub/function?

    If so, something like this should work:
    VB Code:
    1. Dim lLeft As Long
    2. Dim lTop As Long
    3. Dim oObject As Object
    4.  
    5. Set oObject = [i]<parameter name>[/i]
    6. lLeft = oObject.Left
    7. lTop = oObject.Top
    8.  
    9. Do While Not (TypeOf oObject Is Form)
    10.   Set oObject = oObject.Container
    11.   lLeft = lLeft + oObject.Left
    12.   lTop = lTop + oObject.Top
    13. Loop
    14.  
    15. MsgBox lLeft & vbCr & lTop

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Getting the absolute position of a control without knowing its parent

    Don't all controls have a Parent property, which is the Form it is sited upon.

  4. #4

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Getting the absolute position of a control without knowing its parent

    Well I am trying to make a class that works with any control that is alignable on an MDI form. Some custom controls might not have a .Parent property so I was wondering if there was an API way to do it without knowing the form or the .Parent.

    I guess I could have the user pass the form as an argument when intializing the control, but I was trying to avoid that. If there is no API way to do it then I will just have them pass the form (or the form's hWnd I guess) as an argument.

    Thanks guys.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Getting the absolute position of a control without knowing its parent

    Quote Originally Posted by brucevde
    Don't all controls have a Parent property, which is the Form it is sited upon.
    I think so, not 100% sure tho. The downside of using Parent is that it does not take into account any frames or other container controls, hence the use of Container in my code (which may or may not be valid for all controls).

    I guess I could have the user pass the form as an argument when intializing the control, but I was trying to avoid that. If there is no API way to do it then I will just have them pass the form (or the form's hWnd I guess) as an argument.
    You would also need to specify any container controls such as frames that the control is on, as these alter the actual position of the control. Bear in mind that you can have frames within frames, and pictureboxes on those frames that contain controls.

  6. #6

    Thread Starter
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Getting the absolute position of a control without knowing its parent

    Thanks SI.

    Since the control that my class is targeted for will be alginable on an MDI form, there isn't really much of a problem of multiple frames, etc (as far as I can see) because it will only function the way it is suppose to if the control is placed directly on the MDI form.

    If I have the user pass the parent form as an agurment then I can use the .Top and .Left of the control to know if the control is layered with other alignable controls AND I can use the GetWindowRect to get position of the form on the screen.

    The reason I need the absolute value of the window is because I am extending a class JA made that draws ontop of all windows. Here is the the thread.

    Thanks again SI.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  7. #7
    New Member
    Join Date
    Apr 2020
    Posts
    2

    Re: Getting the absolute position of a control without knowing its parent

    Quote Originally Posted by si_the_geek View Post
    Do you mean that a control will be passed as a parameter to a sub/function?

    If so, something like this should work:
    VB Code:
    1. Dim lLeft As Long
    2. Dim lTop As Long
    3. Dim oObject As Object
    4.  
    5. Set oObject = [i]<parameter name>[/i]
    6. lLeft = oObject.Left
    7. lTop = oObject.Top
    8.  
    9. Do While Not (TypeOf oObject Is Form)
    10.   Set oObject = oObject.Container
    11.   lLeft = lLeft + oObject.Left
    12.   lTop = lTop + oObject.Top
    13. Loop
    14.  
    15. MsgBox lLeft & vbCr & lTop
    I had to make a couple of mods to make it work:
    Dim tLeft As Long
    Dim tTop As Long
    Dim tObject As Object
    tObject = uDialogue.Parent
    tLeft = tObject.Left
    tTop = tObject.Top
    Do While Not (TypeOf tObject Is Form)
    Try
    ' tObject = tObject.Container
    tObject = tObject.parent
    Catch ex As Exception
    Exit Do
    End Try
    If tObject IsNot Nothing Then
    tLeft = tLeft + tObject.Left
    tTop = tTop + tObject.Top
    End If
    Loop

  8. #8
    PowerPoster
    Join Date
    Jan 2020
    Posts
    5,541

    Re: Getting the absolute position of a control without knowing its parent

    getwindowrect api for parent control with hwnd.
    like :
    picture1> frame1 > label1
    Last edited by xiaoyao; Jul 6th, 2022 at 07:20 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width