2

I am using GetWindowRect to get a screenshot, however the coordinates are substantially off.

Private Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow" () As IntPtr
Private Declare Function GetWindowRect Lib "user32" _
                        (ByVal hwnd As IntPtr,
                        ByRef lpRect As RECT) _
                        As Integer

Private Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
End Structure

Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim r As New RECT
        GetWindowRect(GetActiveWindow, r)
        MsgBox(d.Left & " " & d.Right & " " & d.Top & " " & d.Bottom)
End Sub

I suspect it has something to do with Aero desktop. However, with that post and other online resources (which seem to be sparse on this subject), I can't make heads or tails on how to implement it.

Here is the nonminimal example:

  Private Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow" () As IntPtr
 Private Declare Function GetWindowRect Lib "user32" _
       (ByVal hwnd As IntPtr,
       ByRef lpRect As RECT) _
       As Integer
 Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
 Public Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer
  Public Declare Function IsIconic Lib "user32.dll" (ByVal hwnd As Integer) As Boolean
  Public Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
    
 
 Private Structure RECT
  Public Left As Integer
  Public Top As Integer
  Public Right As Integer
  Public Bottom As Integer
 End Structure
       
 Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  Dim r As New RECT
  GetWindowRect(GetActiveWindow, r)
  MsgBox(d.Left & " " & d.Right & " " & d.Top & " " & d.Bottom)
 End Sub

 Private Function activeWindowCapture()
  Dim r As New RECT
  Dim hwnd As Integer = FocusWindow("TeamViewer", Nothing)
  GetWindowRect(hwnd, r)
  Dim img As New Bitmap(r.Right - r.Left, r.Bottom - r.Top)
  Dim gr As Graphics = Graphics.FromImage(img)
  gr.CopyFromScreen(New Point(r.Left, r.Top), Point.Empty, img.Size)
  Dim sysImg As Image = DirectCast(img, Image)
  PictureBox1.Image = sysImg
  Return sysImg
 End Function
 
 Private Function FocusWindow(ByVal strWindowCaption As String, ByVal strClassName As String)
  Dim hWnd As Integer
  hWnd = FindWindow(strClassName, strWindowCaption)
  If hWnd > 0 Then
   SetForegroundWindow(hWnd)
   If IsIconic(hWnd) Then  'Restore if minimized
    ShowWindow(hWnd, SW_RESTORE)
   Else
    ShowWindow(hWnd, SW_SHOW)
   End If
  End If
  Return hWnd
 End Function
Neil
  • 14,063
  • 3
  • 30
  • 51
  • you have only one screen, right? – Francesco B. Mar 29 '18 at 19:42
  • @FrancescoB. Yes. – Neil Mar 29 '18 at 19:43
  • Your pinvokes are all over the place. HWND should be an IntPtr, etc. Code worked otherwise. – LarsTech Mar 29 '18 at 20:10
  • @LarsTech It works correctly on some computers. However, GetWindowRect fails to get the correct coordinates on mine. – Neil Mar 29 '18 at 20:19
  • I have a double monitor and I got an image from the Calculator app I tested with on either monitor. If it works for some computers and not yours, something else is going on. That it works on some computers should have been mentioned in your post. – LarsTech Mar 29 '18 at 20:23

0 Answers0