Option ExplicitPrivate Const BI_RGB = 0& Private Const DIB_RGB_COLORS = 0 Private Const OBJ_BITMAP As Long = 7Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End TypePrivate Type BITMAPINFOHEADER biSize As Long biWidth As Long biHeight As Long biPlanes As Integer biBitCount As Integer biCompression As Long biSizeImage As Long biXPelsPerMeter As Long biYPelsPerMeter As Long biClrUsed As Long biClrImportant As Long End TypePrivate Type RGBQUAD rgbBlue As Byte rgbGreen As Byte rgbRed As Byte rgbReserved As Byte End TypePrivate Type BITMAPINFO bmiHeader As BITMAPINFOHEADER bmiColors As RGBQUAD End TypePrivate Declare Function GetCurrentObject Lib "gdi32.dll" _ (ByVal hdc As Long, ByVal uObjectType As Long) As Long Private Declare Function GetDIBits Lib "gdi32" _ (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long Private Declare Function GetWindowRect Lib "user32.dll" _ (ByVal hwnd As Long, ByRef lpRect As RECT) As Long Private Declare Function DeleteObject Lib "gdi32" _ (ByVal hObject As Long) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function PrintWindow Lib "user32" _ (ByVal hwnd As Long, ByVal hdcBlt As Long, ByVal nFlags As Long) As Long Private Declare Function SelectObject Lib "gdi32" _ (ByVal hdc As Long, ByVal hObject As Long) As Long Private Declare Function SetDIBitsToDevice Lib "gdi32" _ (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal Scan As Long, ByVal NumScans As Long, Bits As Any, BitsInfo As BITMAPINFO, ByVal wUsage As Long) As LongPrivate bi24BitInfo As BITMAPINFO Private aBytes() As BytePrivate Sub Command1_Click() Dim hWndTarget As Long Dim rc As RECT Dim iBitmap As Long Dim iOldObject As Long hWndTarget = FindWindow("Notepad", vbNullString) If hWndTarget = 0 Then Me.Print "NotePad window not found!" Exit Sub End If PrintWindow hWndTarget, Me.hdc, 0 Me.Refresh GetWindowRect hWndTarget, rc With bi24BitInfo.bmiHeader .biBitCount = 24 .biCompression = BI_RGB .biPlanes = 1 .biSize = Len(bi24BitInfo.bmiHeader) .biWidth = rc.Right - rc.Left .biHeight = rc.Bottom - rc.Top End With ReDim aBytes(1 To bi24BitInfo.bmiHeader.biWidth * bi24BitInfo.bmiHeader.biHeight * 3) As Byte iBitmap = GetCurrentObject(Me.hdc, OBJ_BITMAP) GetDIBits Me.hdc, iBitmap, 0, bi24BitInfo.bmiHeader.biHeight, aBytes(1), bi24BitInfo, DIB_RGB_COLORS DeleteObject iBitmap End SubPrivate Sub Command2_Click() Me.BackColor = vbWhite Me.Cls SetDIBitsToDevice Me.hdc, 50, 0, bi24BitInfo.bmiHeader.biWidth, bi24BitInfo.bmiHeader.biHeight, 0, 0, 0, bi24BitInfo.bmiHeader.biHeight, aBytes(1), bi24BitInfo, DIB_RGB_COLORS End SubPrivate Sub Form_Load() Me.AutoRedraw = True End Sub
Private Const DIB_RGB_COLORS = 0
Private Const OBJ_BITMAP As Long = 7Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePrivate Type BITMAPINFOHEADER
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End TypePrivate Type RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End TypePrivate Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors As RGBQUAD
End TypePrivate Declare Function GetCurrentObject Lib "gdi32.dll" _
(ByVal hdc As Long, ByVal uObjectType As Long) As Long
Private Declare Function GetDIBits Lib "gdi32" _
(ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Private Declare Function GetWindowRect Lib "user32.dll" _
(ByVal hwnd As Long, ByRef lpRect As RECT) As Long
Private Declare Function DeleteObject Lib "gdi32" _
(ByVal hObject As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PrintWindow Lib "user32" _
(ByVal hwnd As Long, ByVal hdcBlt As Long, ByVal nFlags As Long) As Long
Private Declare Function SelectObject Lib "gdi32" _
(ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function SetDIBitsToDevice Lib "gdi32" _
(ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal Scan As Long, ByVal NumScans As Long, Bits As Any, BitsInfo As BITMAPINFO, ByVal wUsage As Long) As LongPrivate bi24BitInfo As BITMAPINFO
Private aBytes() As BytePrivate Sub Command1_Click()
Dim hWndTarget As Long
Dim rc As RECT
Dim iBitmap As Long
Dim iOldObject As Long hWndTarget = FindWindow("Notepad", vbNullString)
If hWndTarget = 0 Then
Me.Print "NotePad window not found!"
Exit Sub
End If PrintWindow hWndTarget, Me.hdc, 0
Me.Refresh GetWindowRect hWndTarget, rc
With bi24BitInfo.bmiHeader
.biBitCount = 24
.biCompression = BI_RGB
.biPlanes = 1
.biSize = Len(bi24BitInfo.bmiHeader)
.biWidth = rc.Right - rc.Left
.biHeight = rc.Bottom - rc.Top
End With ReDim aBytes(1 To bi24BitInfo.bmiHeader.biWidth * bi24BitInfo.bmiHeader.biHeight * 3) As Byte
iBitmap = GetCurrentObject(Me.hdc, OBJ_BITMAP)
GetDIBits Me.hdc, iBitmap, 0, bi24BitInfo.bmiHeader.biHeight, aBytes(1), bi24BitInfo, DIB_RGB_COLORS
DeleteObject iBitmap
End SubPrivate Sub Command2_Click()
Me.BackColor = vbWhite
Me.Cls
SetDIBitsToDevice Me.hdc, 50, 0, bi24BitInfo.bmiHeader.biWidth, bi24BitInfo.bmiHeader.biHeight, 0, 0, 0, bi24BitInfo.bmiHeader.biHeight, aBytes(1), bi24BitInfo, DIB_RGB_COLORS
End SubPrivate Sub Form_Load()
Me.AutoRedraw = True
End Sub