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