Results 1 to 4 of 4

Thread: [RESOLVED] Determine position of UserControl on Form?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,784

    Resolved [RESOLVED] Determine position of UserControl on Form?

    Trying to figure out basically where the x,y position of a UserControl is on a form, then the width/height of the form.

    Obviously this is easy to do from the outside, but I need to do it from the UserControl code. It would also be easy if the Form was the immediate parent, but I can't presume that to be the case.

    I feel like I'm overlooking something simple... any thoughts?

  2. #2
    Hyperactive Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    321

    Re: Determine position of UserControl on Form?

    hello maybe this is what you are looking for, another way is by resorting to apis, but if it is windowless not

    use scalex and scaley if you need to do the conversion
    Code:
    Private Sub UserControl_Click()
        Debug.Print Extender.Left, Extender.Top; UserControl.ContainerHwnd
    End Sub
    leandroascierto.com Visual Basic 6 projects

  3. #3
    Hyperactive Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    321

    Re: Determine position of UserControl on Form?

    Here is an example with if it is window or is windowsless

    Code:
    Option Explicit
    Private Declare Function GetAncestor Lib "user32.dll" (ByVal hwnd As Long, ByVal gaFlags As Long) As Long
    Private Declare Function GetClientRect Lib "user32.dll" (ByVal hwnd As Long, ByRef lpRect As Rect) As Long
    Private Declare Function ClientToScreen Lib "user32.dll" (ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long
    Private Declare Function ScreenToClient Lib "user32.dll" (ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long
    
    Private Type Rect
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Private Type POINTAPI
        x As Long
        y As Long
    End Type
    
    Private Const GA_PARENT As Long = 1
    Private Const GA_ROOT As Long = 2
    Private Const GA_ROOTOWNER As Long = 3
    
    
    Private Sub UserControl_Click()
        Dim PT As POINTAPI
        Dim Rect As Rect
        Dim hParentForm As Long
        Dim ScaleC As Long
        
        hParentForm = GetAncestor(UserControl.hwnd, GA_ROOT)
        GetClientRect hParentForm, Rect
        ScreenToClient hParentForm, PT
        ClientToScreen UserControl.hwnd, PT
        Debug.Print PT.x, PT.y, Rect.Right, Rect.Bottom
        
        'If UserControl.widowless Then
    '        PT.x = 0: PT.y = 0
    '        hParentForm = GetAncestor(UserControl.ContainerHwnd, GA_ROOT)
    '        GetClientRect hParentForm, Rect
    '        ScreenToClient hParentForm, PT
    '        ClientToScreen UserControl.ContainerHwnd, PT
    '
    '        ScaleC = ScaleContainer
    '        PT.x = PT.x + ScaleX(Extender.Left, ScaleC, vbPixels)
    '        PT.y = PT.y + ScaleY(Extender.Top, ScaleC, vbPixels)
    '        Debug.Print PT.x, PT.y, Rect.Right, Rect.Bottom
        
        'End If
        
    End Sub
    
    Private Function ScaleContainer() As Long
        On Error Resume Next
        Err.Clear
        ScaleContainer = Extender.Container.ScaleMode
        If Err.Number Then
            ScaleContainer = vbTwips
        End If
    End Function
    Last edited by LeandroA; Apr 24th, 2021 at 06:46 AM.
    leandroascierto.com Visual Basic 6 projects

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,784

    Re: Determine position of UserControl on Form?

    Yes! GetAncestor is exactly what I needed and I had never seen that API; since I wouldn't be able to guarantee the host form was the immediate parent. Extender.Top returned the position on the immediate parent.

    I needed to find how much space was on top of the UC, vs how much from the top of the UC to the bottom of the form, which I did with just a slight mod to your code:

    Code:
        Dim vPT As oleexp.POINT, vpt2 As oleexp.POINT
        Dim vRect As Rect
        Dim hParentForm As Long
        Dim lTop As Long, lBtm As Long
        
        hParentForm = GetAncestor(UserControl.hwnd, GA_ROOT)
        GetClientRect hParentForm, vRect
        ClientToScreen hParentForm, vPT
        ClientToScreen UserControl.hwnd, vpt2
        lTop = vpt2.Y - vPT.Y
        lBtm = vRect.Bottom - lTop
        Debug.Print "OnTop=" & lTop & ",OnBtm=" & lBtm
    Thanks

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