Results 1 to 3 of 3

Thread: Determining X and Y coordinates of a control

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 1999
    Location
    Californ-I- A
    Posts
    207

    Post

    Is there any way I can figure out what the X and Y coordinates of a control are without knowing that of the form it's on or it's own left and top properties? Perhaps through it's hWnd or something?

    I want to be able to establish a control's position with reference to the entire screen, but won't know the form it's on's position, nor it's position on that form.

    Any help would be much appreciated. Thanks,

    Micah Carrick

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,176

    Post

    You could use the GetWindowRect if you know the Window Handle of the Control, eg.
    Code:
    Private Type POINTAPI
            x As Long
            y As Long
    End Type
    
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
    
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    
    Private Sub Form_Load()
        Timer1.Interval = 100
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
        Dim tRECT As RECT
        Dim tPOINT As POINTAPI
        Dim lHwnd As Long
        
        Call GetCursorPos(tPOINT)
        lHwnd = WindowFromPoint(tPOINT.x, tPOINT.y)
        Call GetWindowRect(lHwnd, tRECT)
        
        Caption = tRECT.Left & ", " & tRECT.Top
    End Sub
    Point to any Window/Control to get it's Left and Top Coords Relative to the Top Left of the Screen in Pixels.

    ------------------
    Aaron Young
    Analyst Programmer
    aarony@redwingsoftware.com
    adyoung@win.bright.net

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 1999
    Location
    Californ-I- A
    Posts
    207

    Post

    Thank you very much

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