Results 1 to 5 of 5

Thread: GetClientRect

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    GetClientRect

    I am trying to get the location of all open windows. All RECT's return 0,0,0,0.

    Code:
        Private Structure RECT
            Dim Left As Long
            Dim Top As Long
            Dim Right As Long
            Dim Bottom As Long
        End Structure
        Dim myRect As RECT
        Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, ByVal lpRect As RECT) As Long
        Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    
        Private Sub TestProcessExt()
            Dim pH As Long
            For Each p As Process In Process.GetProcesses()
                Try
                    Debug.Write(p.ProcessName & " ")
                    Debug.WriteLine(p.MainWindowTitle)
                    pH = GetParent(p.Handle.ToInt64)
                    'pH = (p.MainWindowHandle.ToInt64)
                    If p.MainWindowHandle.ToInt64 <> 0 Then
                        GetClientRect(pH, myRect)
                    End If
                Catch ex As Exception
                    Debug.WriteLine(ex.Message)
                End Try
            Next
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: GetClientRect

    are you trying to get coords in relation to the actual desktop? If so then I think you would need to be using GetWindowRect API call instead of GetClientRect.

    GetClientRect is always going to give you 0,0 for the top and left params, and only give you width and height info on the window.

    Try this:
    Code:
        Imports System.Runtime.InteropServices
    
    
    
       <DllImport("user32.dll", ExactSpelling:=True, CharSet:=CharSet.Auto)> _
        Public Shared Function GetParent(ByVal hWnd As IntPtr) As IntPtr
        End Function
    
        <DllImport("user32.dll")> _
        Public Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
        End Function
    
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure RECT
            Public Left As Integer
            Public Top As Integer
            Public Right As Integer
            Public Bottom As Integer
            Public Overrides Function ToString() As String
                Return String.Format("RECT: Left: {0}, Top: {1}, Right: {2}, Bottom {3}", _
                                     Left, Top, Right, Bottom)
            End Function
        End Structure
    
        Private Sub TestProcessExt()
            Dim myRect As RECT
            For Each p As Process In Process.GetProcesses()
                Try
                    If p.MainWindowHandle <> IntPtr.Zero Then
                        GetWindowRect(p.MainWindowHandle, myRect)
                        Debug.Write(p.ProcessName & " ")
                        Debug.Write(p.MainWindowTitle & " ")
                        Debug.WriteLine(myRect.ToString)
                    End If
                Catch ex As Exception
                    Debug.WriteLine("ERROR: " & ex.Message)
                End Try
            Next
        End Sub
    if you see weird negative values for any window, that is I think what happens when it queries windows that are minimized.

  3. #3
    New Member
    Join Date
    May 2017
    Posts
    1

    Re: GetClientRect

    Hey Kleinma, thanks a lot, this was exactly was I was looking for in google, it works perfectly! I just registered with this forum just to thank you.
    Best Wishes from Germany!

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: GetClientRect

    I was really confused to see dbasnett asking this question until I noticed the datestamp. I was like, "No way, this guy's been around long enough I swear he's /answered/ this question a dozen times."
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: GetClientRect

    Nothing wrong with a little thanks.
    My usual boring signature: Nothing

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