Imports System.Drawing.Graphics
Public Class frm_WC
Inherits System.Windows.Forms.Form
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByRef hwnd As Long, ByRef lpRect As S_RECT) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetTopWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, ByVal lpRect As S_RECT) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As IntPtr, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Private Const SRCCOPY = &HCC0020
Private Structure S_RECT
Dim Left As Long
Dim Top As Long
Dim Right As Long
Dim Bottom As Long
End Structure
Private Structure S_BITMAP
Dim bmType As Long
Dim bmWidth As Long
Dim bmHeight As Long
Dim bmWidthBytes As Long
Dim bmPlanes As Integer
Dim bmBitsPixel As Integer
Dim bmBits As Long
End Structure
#Region " Windows Form Designer generated code "
'... omitted auto-generated code
#End Region
Private Sub cmd_Capture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_Capture.Click
Dim lpWindowHandle As Long
Dim lpWindowHandle2 As Long
Dim lpWindowHandle3 As Long
'name of window to capture
Dim lpWindowName As String = txt_Window.Text.Trim
lpWindowHandle = FindWindow(vbNullString, lpWindowName)
'display window handle
MsgBox("hwnd returns " & Hex(lpWindowHandle), MsgBoxStyle.OKOnly, "Results")
'bring it to the foreground
'SetForegroundWindow(lpWindowHandle)
GetShot(lpWindowHandle, PictureBox1)
End Sub
Private Sub GetShot(ByRef lWindowhWnd As Long, ByRef picScreen As PictureBox)
Dim nLeft As Long
Dim nTop As Long
Dim nWidth As Long
Dim nHeight As Long
Dim rRect As New S_RECT
Dim bm As New S_BITMAP
Dim lWindowhDC As Long
Dim vbSrcCopy As Long
Dim errCode As Integer
'picScreen.Image = Nothing
GetWindowRect(lWindowhWnd, rRect)
lWindowhDC = GetWindowDC(lWindowhWnd)
'// Get coordinates
nLeft = 0
nTop = 0
nWidth = rRect.Right - rRect.Left
nHeight = rRect.Bottom - rRect.Top
'// Blt to frm.picScreen
BitBlt(picScreen.Handle, 0, 0, nWidth, nHeight, lWindowhDC, nLeft, nTop, SRCCOPY)
'// Del DC
ReleaseDC(lWindowhWnd, lWindowhDC)
'// set picture
picScreen.Refresh()
End Sub
End Class