'声明UDT来为IPicture OLE接口储存GUID
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
'声明UDT储存bitmap信息
Private Type uPicDesc
Size As Long
Type As Long
hPic As Long
hPal As Long
End Type
'Windows API函数声明
'剪贴板包括bitmap/metafile吗?
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Long
'打开剪贴板读取
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
'获取bitmap/metafile指针
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As Long
'关闭剪贴板
Private Declare Function CloseClipboard Lib "user32" () As Long
'将句柄转换到OLE IPicture接口里.
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As uPicDesc, _
RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
'创建自已的metafile副本,以便不会因为随后剪贴板的更新而擦除
Declare Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" _
(ByVal hemfSrc As Long, ByVal lpszFile As String) As Long
'创建自已的bitmap副本,以便不会因为随后剪贴板的更新而擦除
Declare Function CopyImage Lib "user32" (ByVal handle As Long, ByVal un1 As Long, _
ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
If hPicAvail <> 0 Then
'获取对剪贴板的访问
h = OpenClipboard(0&)
If h > 0 Then
'获取图像数据句柄
hPtr = GetClipboardData(lPicType)
'以合适的格式创建自已的剪贴板中图像的副本
If lPicType = CF_BITMAP Then
hCopy = CopyImage(hPtr, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)
Else
hCopy = CopyEnhMetaFile(hPtr, vbNullString)
End If
'对其它程序释放剪贴板
h = CloseClipboard
'如果获取了图像句柄,将其转换为Picture对象并返回
If hPtr <> 0 Then Set PastePicture = CreatePicture(hCopy, 0, lPicType)
End If
End If