Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () 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 CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Const CF_BITMAP = 2
Public Function CopyScreenToClipboard() As Long
Dim lngScreenDC As Long
Dim lngScreenH As Long, lngScreenW As Long
Dim lngDC As Long
Dim lngBMP As Long, lngOrigBMP As Long
' get a handle to the screen
lngScreenDC = GetDC(GetDesktopWindow)
' get the dimensions of the screen in pixels
lngScreenH = Screen.Height \ Screen.TwipsPerPixelY
lngScreenW = Screen.Width \ Screen.TwipsPerPixelX
' create a dc to temporarily hold the screen bmp
lngDC = CreateCompatibleDC(lngScreenDC)
' create the bitmap in memory to hold picture of the screen
lngBMP = CreateCompatibleBitmap(lngScreenDC, lngScreenW, lngScreenH)
' put the BMP created into the DC created
lngOrigBMP = SelectObject(lngDC, lngBMP)
' blt the current state of the screen to the DC
BitBlt lngDC, 0, 0, lngScreenW, lngScreenH, lngScreenDC, 0, 0, vbSrcCopy
'open the clipboard
If OpenClipboard(Me.hwnd) Then
' clear the clipboard of current data
If EmptyClipboard Then
' put the bitmap in clipboard
SetClipboardData CF_BITMAP, lngBMP
' don't need to be thw owner of the clipboard anymore so let it go
CloseClipboard
Else
MsgBox "Error saving data to clipboard!"
End If
Else
MsgBox "Error saving data to clipboard!"
End If
' free memory
SelectObject lngDC, lngOrigBMP
DeleteDC lngDC
End Function
Private Sub Command1_Click()
CopyScreenToClipboard
End Sub