-
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
-
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.
-
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!
-
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."
-
Re: GetClientRect
Nothing wrong with a little thanks.