我想将图像放入VBA UserForm图像控件,并保持其透明度(像ico文件那样的1位开关透明度具有或更好;像png一样可以具有完整的alpha通道透明度)。
也就是说,原始文件中透明的部分图像在我的用户窗体上也是透明的,所以我可以覆盖图像。
首先,我找不到关于SO的答案,所以我不得不查看其他论坛。我发现了一种支持1位开/关透明度的方法,我会将其作为答案发布,所以现在可以更容易地找到它。
但我找到的方法从剪贴板加载图片,意味着从文件路径加载我必须
ActiveSheet.Pictures.Insert("[filepath]").Cut
myUserform.someImageControl.Picture = PastePicture
这看起来很慢而且错综复杂,并且还擦除了剪贴板,所以我想知道是否有一种很好的方法可以从文件路径中将透明图片加载到用户窗体(不使用剪贴板)?
第二点是我当前的代码看起来很糟糕(结果就是,虽然代码本身有点乱)。为了获得更好的结果,我很惊讶没有一些我可以引用的框架会返回一个透明的IPictureDisp
。
wiaaut.dll
)可用于将png加载到控件 - 例如
Function loadImg(fileLocation As String) As IPictureDisp
Dim imgctrl As New WIA.ImageFile 'can handle more extensions than built in LoadPicture function
With imgctrl
.LoadFile fileLocation
Set loadImg = .fileData.Picture
End With
Set imgctrl = Nothing
End Function
然而,它消除了透明度,这是不好的。所以我问这个问题是为了汇集人们所知道的替代方法(如果有的话)以及提供最佳结果的资源?
答案 0 :(得分:0)
作为参考,如果其他地方的SO上没有副本:这里有一个函数可以代替LoadPicture将图像应用到用户窗体上的图像控件。它仅支持1位透明度(即,像素可见或不可见)
Option Explicit
Option Compare Text
''' User-Defined Types for API Calls
'Declare a UDT to store a GUID for the IPicture OLE Interface
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
'Declare a UDT to store the bitmap information
Private Type uPicDesc
Size As Long
Type As Long
hPic As Long
hPal As Long
End Type
'''Windows API Function Declarations
'Does the clipboard contain a bitmap/metafile?
Private Declare Function IsClipboardFormatAvailable Lib "user32" ( _
ByVal wFormat As Integer) As Long
'Open the clipboard to read
Private Declare Function OpenClipboard Lib "user32" ( _
ByVal hwnd As Long) As Long
'Get a pointer to the bitmap/metafile
Private Declare Function GetClipboardData Lib "user32" ( _
ByVal wFormat As Integer) As Long
'Close the clipboard
Private Declare Function CloseClipboard Lib "user32" () As Long
'Convert the handle into an OLE IPicture interface.
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" ( _
PicDesc As uPicDesc, RefIID As GUID, _
ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
'Create our own copy of the metafile, so it doesn't get _
'wiped out by subsequent clipboard updates.
Declare Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" ( _
ByVal hemfSrc As Long, ByVal lpszFile As String) As Long
'Create our own copy of the bitmap, so it doesn't get wiped out by
'subsequent
'clipboard updates.
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
'The API format types we're interested in
Const CF_BITMAP = 2
Const CF_PALETTE = 9
Const CF_ENHMETAFILE = 14
Const IMAGE_BITMAP = 0
Const LR_COPYRETURNORG = &H4
Function PastePicture(Optional lXlPicType As Long = xlPicture) As IPicture
'Some pointers
Dim h As Long, hPicAvail As Long, hPtr As Long, hPal As Long
Dim lPicType As Long, hCopy As Long
'Convert xl piture-type constant to the API constant equivalent
lPicType = IIf(lXlPicType = xlBitmap, CF_BITMAP, CF_ENHMETAFILE)
'Check if the clipboard contains the required format
hPicAvail = IsClipboardFormatAvailable(lPicType)
If hPicAvail <> 0 Then
'Get access to the clipboard
h = OpenClipboard(0&)
If h > 0 Then
'Get a handle to the image data
hPtr = GetClipboardData(lPicType)
'Create our own copy of the image on the clipboard, in the appropriate format.
If lPicType = CF_BITMAP Then
hCopy = CopyImage(hPtr, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)
Else
hCopy = CopyEnhMetaFile(hPtr, vbNullString)
End If
'Release the clipboard to other programs
h = CloseClipboard
'If we got a handle to the image, convert it into a Picture object and return it
If hPtr <> 0 Then Set PastePicture = CreatePicture(hCopy, 0, lPicType)
End If
End If
End Function
Private Function CreatePicture(ByVal hPic As Long, ByVal hPal As Long, _
ByVal lPicType) As IPicture
' IPicture requires a reference to "OLE Automation"
Dim r As Long, uPicInfo As uPicDesc, IID_IDispatch As GUID, IPic As IPicture
'OLE Picture types
Const PICTYPE_BITMAP = 1
Const PICTYPE_ENHMETAFILE = 4
' Create the Interface GUID (for the IPicture interface)
With IID_IDispatch
.Data1 = &H7BF80980
.Data2 = &HBF32
.Data3 = &H101A
.Data4(0) = &H8B
.Data4(1) = &HBB
.Data4(2) = &H0
.Data4(3) = &HAA
.Data4(4) = &H0
.Data4(5) = &H30
.Data4(6) = &HC
.Data4(7) = &HAB
End With
' Fill uPicInfo with necessary parts.
With uPicInfo
.Size = Len(uPicInfo)
.Type = IIf(lPicType = CF_BITMAP, PICTYPE_BITMAP, PICTYPE_ENHMETAFILE)
.hPic = hPic
.hPal = IIf(lPicType = CF_BITMAP, hPal, 0)
End With
' Create the Picture object.
r = OleCreatePictureIndirect(uPicInfo, IID_IDispatch, True, IPic)
' If an error occured, show the description
If r <> 0 Then
'Debug.Print "Create Picture: " & fnOLEError(r)
Debug.Print "Error, call fnOLEError(r) here"
' fnOLEError from modPastePicture not posted
End If
' Return the new Picture object.
Set CreatePicture = IPic
End Function
Original source是Stephen Bullen的pastePicture.zip
使用像:
Set myImageControl.Picture = PastePicture