Public Declare Function CreateDC Lib "gdi32.dll" (ByVal lpszDriver As String, ByVal lpszDevice As String, ByVal lpszOutput As String, ByVal lpInitData As IntPtr) As IntPtr
Public Declare Function BitBlt Lib "gdi32.dll" (ByVal hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As UInt32) As Integer
Public Declare Function CreateCompatibleDC Lib "gdi32.dll" (ByVal hdc As IntPtr) As IntPtr
Public Declare Function CreateCompatibleBitmap Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal nWidth As Integer, ByVal nHeight As Integer) As IntPtr
Public Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal hgdiobj As IntPtr) As IntPtr
Public Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As IntPtr) As Integer
Public Declare Function PrintWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal hdcBlt As IntPtr, ByVal nFlags As UInt32) As Boolean
Public Declare Function GetWindowDC Lib "user32.dll" (ByVal hwnd As IntPtr) As IntPtr
Public Function GetWindow(ByVal hWnd As IntPtr) As Bitmap
Dim hscrdc As IntPtr = GetWindowDC(hWnd)
Dim control As system.windows.forms.Control = system.windows.forms.Control.FromHandle(hWnd)
Dim hbitmap As IntPtr = CreateCompatibleBitmap(hscrdc, control.Width, control.Height)
Dim hmemdc As IntPtr = CreateCompatibleDC(hscrdc)
SelectObject(hmemdc, hbitmap)
PrintWindow(hWnd, hmemdc, 0)
Dim bmp As Bitmap = Bitmap.FromHbitmap(hbitmap)
DeleteDC(hscrdc)
DeleteDC(hmemdc)
Return bmp
End Function
打开需要截图的窗口,必须是独立,或者模式窗口
Dim f = Forms("窗口3")
f.open
Dim bit As Bitmap = GetWindow(f.baseform.handle)
bit.save("D:\123.jpg")