Option Explicit
Private Const CF_BITMAP = 2
Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, 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 DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Function TakePicture(ByVal Obj As VB.Control) As Boolean
Dim hBitmap As Long
Dim hObjDC As Long
Dim hCmpDC As Long
Dim hScreenDC As Long
Dim ObjWidth As Long
Dim ObjHeight As Long
ObjWidth = ScaleX(Obj.Width, ScaleMode, vbPixels)
ObjHeight = ScaleY(Obj.Height, ScaleMode, vbPixels)
hObjDC = GetDC(Obj.hwnd)
If (hObjDC = 0) Then
TakePicture = False
Exit Function
End If
hScreenDC = GetDC(0)
If (hScreenDC = 0) Then
TakePicture = False
GoTo f4
End If
hCmpDC = CreateCompatibleDC(hScreenDC)
If (hCmpDC = 0) Then
TakePicture = False
GoTo f3
End If
hBitmap = CreateCompatibleBitmap(hScreenDC, _
ObjWidth, ObjHeight)
If (hBitmap = 0) Then
TakePicture = False
GoTo f2
End If
Call SelectObject(hCmpDC, hBitmap)
BitBlt hCmpDC, 0, 0, ObjWidth, ObjHeight, hObjDC, 0, 0, SRCCOPY
If (OpenClipboard(Me.hwnd) = 0) Then
TakePicture = False
GoTo f1
End If
EmptyClipboard
SetClipboardData CF_BITMAP, hBitmap
CloseClipboard
TakePicture = True
f1:
Call DeleteObject(hBitmap)
f2:
Call DeleteDC(hCmpDC)
f3:
Call ReleaseDC(0, hScreenDC)
f4:
Call ReleaseDC(Obj.hwnd, hObjDC)
End Function
Private Sub Command1_Click()
Call TakePicture (Text1)
End Sub