'聲明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函數(shù)聲明
'剪貼板包括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
'關(guān)閉剪貼板
Private Declare Function CloseClipboard Lib "user32" () As Long
'將句柄轉(zhuǎn)換到OLE IPicture接口里.
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As uPicDesc, _
RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
'創(chuàng)建自已的metafile副本,以便不會因為隨后剪貼板的更新而擦除
Declare Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" _
(ByVal hemfSrc As Long, ByVal lpszFile As String) As Long
'創(chuàng)建自已的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
'獲取圖像數(shù)據(jù)句柄
hPtr = GetClipboardData(lPicType)
'以合適的格式創(chuàng)建自已的剪貼板中圖像的副本
If lPicType = CF_BITMAP Then
hCopy = CopyImage(hPtr, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)
Else
hCopy = CopyEnhMetaFile(hPtr, vbNullString)
End If
'對其它程序釋放剪貼板
h = CloseClipboard
'如果獲取了圖像句柄,將其轉(zhuǎn)換為Picture對象并返回
If hPtr <> 0 Then Set PastePicture = CreatePicture(hCopy, 0, lPicType)
End If
End If