Option Explicit
Public Enum FormatWhat
Shapes_Group
Big_Shape
Small_Shape
End Enum
Public Type Info
ShapeName As String
Location As String
Pixels As Long
End Type
Private Type uPicDesc
Size As Long
Type As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
hPic As LongPtr
hPal As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
hPic As Long
hPal As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
End Type
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
bmBits As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
bmBits As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
End Type
Private Type BITMAPINFOHEADER
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Private Type RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Private Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors As RGBQUAD
End Type
Private Type BLENDFUNCTION
BlendOp As Byte
BlendFlags As Byte
SourceConstantAlpha As Byte
AlphaFormat As Byte
End Type
Private Type RGB
R As Long
G As Long
B As Long
End Type
Private Type RGBA
R As Byte
G As Byte
B As Byte
A As Byte
End Type
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Private Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long
Private Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As LongPtr
Private Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As LongPtr
Private Declare PtrSafe Function FreeLibrary Lib "kernel32" (ByVal hLibModule As LongPtr) As Long
Private Declare PtrSafe Function OleCreatePictureIndirectAut Lib "oleAut32.dll" Alias "OleCreatePictureIndirect" (PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, iPic As IPictureDisp) As Long
Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hdc As LongPtr) As Long
Private Declare PtrSafe Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As LongPtr) As LongPtr
Private Declare PtrSafe Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As LongPtr, ByVal nWidth As Long, ByVal nHeight As Long) As LongPtr
Private Declare PtrSafe Function SelectObject Lib "gdi32" (ByVal hdc As LongPtr, ByVal hObject As LongPtr) As LongPtr
Private Declare PtrSafe Function DeleteObject Lib "gdi32" (ByVal hObject As LongPtr) As Long
Private Declare PtrSafe Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As LongPtr, ByVal nCount As Long, lpObject As Any) As Long
Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongPtr)
Private Declare PtrSafe Function CopyImage Lib "user32" (ByVal handle As LongPtr, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As LongPtr
Private Declare PtrSafe Function TransparentBlt Lib "msimg32.dll" (ByVal hdc As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As LongPtr, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal crTransparent As Long) As Boolean
Private Declare PtrSafe Function AlphaBlend Lib "msimg32.dll" (ByVal hdc As LongPtr, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal hdc As LongPtr, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal BLENDFUNCT As Long) As Long
Private Declare PtrSafe Function GetDIBits Lib "gdi32" (ByVal aHDC As LongPtr, ByVal hBitmap As LongPtr, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As Any, ByVal wUsage As Long) As Long
Private Declare PtrSafe Function BitBlt Lib "gdi32" (ByVal hDestDC As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As LongPtr, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare PtrSafe Function StretchBlt Lib "gdi32" (ByVal hdc As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As LongPtr, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function OleCreatePictureIndirectAut Lib "oleAut32.dll" Alias "OleCreatePictureIndirect" (PicDesc As uPicDesc, RefIID As GUID, ByVal fPictureOwnsHandle As Long, iPic As IPictureDisp) 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 CreateCompatibleDC Lib "gdi32" (ByVal hDC 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 SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function GetObjectAPI Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private 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
Private Declare Function TransparentBlt Lib "msimg32.dll" (ByVal hDC 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 nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal crTransparent As Long) As Boolean
Private Declare Function AlphaBlend Lib "msimg32.dll" (ByVal hDC As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal hDC As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal lInt As Long, ByVal BLENDFUNCT As Long) As Long
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As Any, ByVal wUsage 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 StretchBlt Lib "gdi32" (ByVal hdc 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 nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
Public Function IsShapeInside(ByVal BigShape As Shape, ByVal SmallShape As Shape, Info As Info) As Boolean
Const DIB_RGB_COLORS = 0&
Const BI_RGB = 0&
Const SRCCOPY = &HCC0020
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
Dim hdc As LongPtr, hBufferDC1 As LongPtr, hBufferDC2 As LongPtr, hBufferDC3 As LongPtr
Dim hTempDC1 As LongPtr, hTempDC2 As LongPtr, hTempDC3 As LongPtr, hTempDC4 As LongPtr, hTempBmp As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
Dim hdc As Long, hBufferDC1 As Long, hBufferDC2 As Long, hBufferDC3 As Long
Dim hTempDC1 As Long, hTempDC2 As Long, hTempDC3 As Long, hTempDC4 As Long, hTempBmp As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
Dim tBMP As BITMAP, iPic As IPictureDisp, tBMInfo As BITMAPINFO, tPixels() As RGBQUAD
Dim iNew_R As Integer, iNew_G As Integer, iNew_B As Integer
Dim lTotalPixels As Long, lOverlappingPixels As Long
Dim X As Long, Y As Long, oCurObj As Object
On Error GoTo errHandler
Application.ScreenUpdating = False
hdc = GetDC(0)
Set oCurObj = DuplicateAndFormatShapes(BigShape, SmallShape, Shapes_Group)
Set iPic = PicFromObject(oCurObj)
oCurObj.Delete
hBufferDC1 = FlipBmpInBufferDc(BigShape, SmallShape, iPic)
hTempDC1 = MakeBlackTransparent(iPic, hBufferDC1)
Set oCurObj = DuplicateAndFormatShapes(BigShape, SmallShape, Shapes_Group)
Set iPic = PicFromObject(oCurObj)
oCurObj.Delete
hBufferDC2 = FlipBmpInBufferDc(BigShape, SmallShape, iPic)
hTempDC2 = MakeBlackTransparent(iPic, hBufferDC2)
Set oCurObj = DuplicateAndFormatShapes(BigShape, SmallShape, Big_Shape)
Set iPic = PicFromObject(oCurObj)
oCurObj.Delete
hBufferDC3 = FlipBmpInBufferDc(BigShape, SmallShape, iPic)
Call BlendDCs(iPic, hTempDC2, hBufferDC3)
Call GetObjectAPI(iPic.handle, LenB(tBMP), tBMP)
hTempDC3 = CreateCompatibleDC(hdc)
hTempBmp = CreateCompatibleBitmap(hdc, tBMP.bmWidth, tBMP.bmHeight)
Call DeleteObject(SelectObject(hTempDC3, hTempBmp))
Call BitBlt(hTempDC3, 0, 0, tBMP.bmWidth, tBMP.bmHeight, hBufferDC3, 0, 0, SRCCOPY)
[COLOR=#008000] 'UNCOMMENT FOR DEBUGGING ONLY !!!
' Call BitBlt(hdc, 50, 200, tBMP.bmWidth, tBMP.bmHeight, hTempDC3, 0, 0, SRCCOPY)
' Sleep 3000[/COLOR]
tBMInfo.bmiHeader.biSize = LenB(tBMInfo.bmiHeader)
Call GetDIBits(hBufferDC3, hTempBmp, 0, 0, 0, tBMInfo, DIB_RGB_COLORS)
ReDim tPixels(tBMInfo.bmiHeader.biWidth, tBMInfo.bmiHeader.biHeight)
tBMInfo.bmiHeader.biCompression = BI_RGB
Call GetDIBits(hBufferDC3, hTempBmp, 0, tBMInfo.bmiHeader.biHeight, tPixels(1, 1), tBMInfo, DIB_RGB_COLORS)
iNew_R = CInt((255 - ColorToRGB(0).R) * (100 / 255#) + ColorToRGB(0).R)
iNew_G = CInt((255 - ColorToRGB(0).G) * (100 / 255#) + ColorToRGB(0).G)
iNew_B = CInt((255 - ColorToRGB(0).B) * (100 / 255#) + ColorToRGB(0).B)
For X = 0 To tBMInfo.bmiHeader.biWidth
For Y = 0 To tBMInfo.bmiHeader.biHeight
With tPixels(X, Y)
If .rgbRed = iNew_R And .rgbGreen = iNew_G And .rgbBlue = iNew_B Then
lOverlappingPixels = lOverlappingPixels + 1
End If
End With
Next Y
Next X
If lOverlappingPixels Then
Set oCurObj = DuplicateAndFormatShapes(BigShape, SmallShape, Small_Shape)
Set iPic = PicFromObject(oCurObj)
oCurObj.Delete
hTempDC4 = CreateCompatibleDC(hdc)
tBMInfo.bmiHeader.biSize = LenB(tBMInfo.bmiHeader)
Call GetDIBits(hTempDC4, iPic.handle, 0, 0, 0, tBMInfo, DIB_RGB_COLORS)
ReDim tPixels(tBMInfo.bmiHeader.biWidth, tBMInfo.bmiHeader.biHeight)
tBMInfo.bmiHeader.biCompression = BI_RGB
Call GetDIBits(hTempDC4, iPic.handle, 0, tBMInfo.bmiHeader.biHeight, tPixels(1, 1), tBMInfo, DIB_RGB_COLORS)
For X = 0 To tBMInfo.bmiHeader.biWidth
For Y = 0 To tBMInfo.bmiHeader.biHeight
With tPixels(X, Y)
If .rgbRed = 255 And .rgbGreen = 0 And .rgbBlue = 0 Then
lTotalPixels = lTotalPixels + 1
End If
End With
Next Y
Next X
IsShapeInside = True
If lTotalPixels = lOverlappingPixels Then
Info.Location = "Fully Inside"
Else
Info.Location = "Partly Inside"
End If
Else
Info.Location = "Outside"
End If
Info.ShapeName = SmallShape.Name: Info.Pixels = lOverlappingPixels
errHandler:
Application.ScreenUpdating = True
ReleaseDC 0, hdc: DeleteObject hBufferDC1: DeleteObject hBufferDC2
DeleteObject hBufferDC3: DeleteObject hTempDC1: DeleteObject hTempDC2
DeleteObject hTempDC3: DeleteObject hTempDC4: DeleteObject hTempBmp
If Err Then MsgBox "Error!" & vbNewLine & Err.Description, vbCritical
End Function
Private Function DuplicateAndFormatShapes(ByVal BigShape As Shape, ByVal SmallShape As Shape, Format_What As FormatWhat) As Object
Dim oObj As Object, oShp As Shape
If Format_What = Shapes_Group Then
Set oObj = BigShape.Parent.Shapes.Range(Array(BigShape.Name, SmallShape.Name))
Set oObj = oObj.Duplicate
Set oObj = oObj.Group
For Each oShp In oObj.GroupItems
If oShp.Line.Visible = msoTrue Then oShp.Line.ForeColor.RGB = IIf(oShp.Name = BigShape.Name, RGB(0, 0, 0), RGB(255, 255, 255))
oShp.TextFrame2.TextRange.Characters.Text = ""
With oShp.Fill
.ForeColor.RGB = IIf(oShp.Name = BigShape.Name, RGB(0, 0, 0), RGB(255, 255, 255))
.Solid
End With
Next oShp
ElseIf Format_What = Big_Shape Then
Set oObj = BigShape.Duplicate
With oObj
If .Line.Visible = msoTrue Then .Line.ForeColor.RGB = RGB(0, 0, 0)
.TextFrame2.TextRange.Characters.Text = ""
With .Fill
.ForeColor.RGB = RGB(0, 0, 0)
.Solid
End With
End With
Else
Set oObj = SmallShape.Duplicate
With oObj
If .Line.Visible = msoTrue Then .Line.ForeColor.RGB = RGB(255, 0, 0)
.TextFrame2.TextRange.Characters.Text = ""
With .Fill
.ForeColor.RGB = RGB(255, 0, 0)
.Solid
End With
End With
End If
oObj.Select
Set DuplicateAndFormatShapes = Selection
End Function
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
Function FlipBmpInBufferDc(ByVal BigShape As Shape, ByVal SmallShape As Shape, ByVal Pic As IPictureDisp) As LongPtr
Dim hdc As LongPtr, hMemDc As LongPtr, hBufferDC As LongPtr, hMemBmp As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
Function FlipBmpInBufferDc(ByVal BigShape As Shape, ByVal SmallShape As Shape, ByVal Pic As IPictureDisp) As Long
Dim hdc As Long, hMemDc As Long, hBufferDC As Long, hMemBmp As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
Const SRCCOPY = &HCC0020
Dim tBMP As BITMAP
hdc = GetDC(0)
Call GetObjectAPI(Pic.handle, LenB(tBMP), tBMP)
hMemDc = CreateCompatibleDC(hdc)
Call DeleteObject(SelectObject(hMemDc, Pic.handle))
hBufferDC = CreateCompatibleDC(hdc)
hMemBmp = CreateCompatibleBitmap(hdc, tBMP.bmWidth, tBMP.bmHeight)
Call DeleteObject(SelectObject(hBufferDC, hMemBmp))
Select Case True
Case SmallShape.Top <= BigShape.Top And SmallShape.Left <= BigShape.Left
StretchBlt _
hBufferDC, tBMP.bmWidth, tBMP.bmHeight, -tBMP.bmWidth, -tBMP.bmHeight, _
hMemDc, 0, 0, _
tBMP.bmWidth, tBMP.bmHeight, SRCCOPY
Case SmallShape.Left <= BigShape.Left
StretchBlt _
hBufferDC, tBMP.bmWidth, 0, -tBMP.bmWidth, tBMP.bmHeight, _
hMemDc, 0, 0, _
tBMP.bmWidth, tBMP.bmHeight, SRCCOPY
Case SmallShape.Top <= BigShape.Top
StretchBlt _
hBufferDC, 0, tBMP.bmHeight, tBMP.bmWidth, -tBMP.bmHeight, _
hMemDc, 0, 0, _
tBMP.bmWidth, tBMP.bmHeight, SRCCOPY
Case Else
hBufferDC = hMemDc
End Select
FlipBmpInBufferDc = hBufferDC
ReleaseDC 0, hdc
DeleteObject hMemBmp
End Function
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
Function MakeBlackTransparent(ByVal Pic As IPictureDisp, ByVal Buffer As LongPtr) As LongPtr
Dim hdc As LongPtr, hMemDc As LongPtr, hMemBmp As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
Function MakeBlackTransparent(ByVal Pic As IPictureDisp, ByVal Buffer As Long) As Long
Dim hdc As Long, hMemDc As Long, hMemBmp As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
Const SRCCOPY = &HCC0020
Dim tBMP As BITMAP
hdc = GetDC(0)
Call GetObjectAPI(Pic.handle, LenB(tBMP), tBMP)
hMemDc = CreateCompatibleDC(hdc)
hMemBmp = CreateCompatibleBitmap(hdc, tBMP.bmWidth, tBMP.bmHeight)
Call DeleteObject(SelectObject(hMemDc, hMemBmp))
Call TransparentBlt(hMemDc, 0, 0, tBMP.bmWidth, tBMP.bmHeight, Buffer, 0, 0, tBMP.bmWidth, tBMP.bmHeight, 0)
MakeBlackTransparent = hMemDc
ReleaseDC 0, hdc
DeleteObject hMemBmp
End Function
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
Function BlendDCs(ByVal Pic As IPictureDisp, ByVal DC1 As LongPtr, ByRef DC2 As LongPtr) As LongPtr
Dim hdc As LongPtr, hMemDc As LongPtr, hMemBmp As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
Function BlendDCs(ByVal Pic As IPictureDisp, ByVal DC1 As Long, ByRef DC2 As Long) As Long
Dim hdc As Long, hMemDc As Long, hMemBmp As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
Const AC_SRC_OVER = &H0
Dim tBF As BLENDFUNCTION, lBF As Long
Dim tBMP As BITMAP
Call GetObjectAPI(Pic.handle, LenB(tBMP), tBMP)
With tBF
.BlendOp = AC_SRC_OVER
.BlendFlags = 0
.SourceConstantAlpha = 100
.AlphaFormat = 0
End With
Call CopyMemory(lBF, tBF, LenB(lBF))
Call AlphaBlend(DC2, 0, 0, tBMP.bmWidth, tBMP.bmHeight, DC1, 0, 0, tBMP.bmWidth, tBMP.bmHeight, lBF)
BlendDCs = hMemDc
ReleaseDC 0, hdc
DeleteObject hMemDc
DeleteObject hMemBmp
End Function
Private Function PicFromObject(ByVal Obj As Object) As IPictureDisp
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL] VBA7 Then
Dim hPtr As LongPtr, hLib As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL]
Dim hPtr As Long, hLib As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL] If
Const IMAGE_BITMAP = 0
Const IMAGE_ICON = 1
Const PICTYPE_BITMAP = 1
Const PICTYPE_ICON = 3
Const LR_COPYRETURNORG = &H4
Const CF_BITMAP = 2
Const S_OK = 0
Dim IID_IDispatch As GUID, uPicinfo As uPicDesc
Dim iPic As IPictureDisp, lRet As Long, lPictype As Long
On Error GoTo errHandler
Obj.Copy
Call OpenClipboard(0)
hPtr = GetClipboardData(CF_BITMAP)
hPtr = CopyImage(hPtr, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)
Call EmptyClipboard
Call CloseClipboard
lPictype = PICTYPE_BITMAP
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
With uPicinfo
.Size = Len(uPicinfo)
.Type = lPictype
.hPic = hPtr
.hPal = 0
End With
hLib = LoadLibrary("oleAut32.dll")
If hLib Then
lRet = OleCreatePictureIndirectAut(uPicinfo, IID_IDispatch, True, iPic)
End If
Call FreeLibrary(hLib)
If lRet = S_OK Then
Set PicFromObject = iPic
End If
errHandler:
Call FreeLibrary(hLib)
Call EmptyClipboard
Call CloseClipboard
End Function
Private Function ColorToRGB(ByVal Col As Long) As RGB
ColorToRGB.R = &HFF& And Col
ColorToRGB.G = (&HFF00& And Col) \ 256
ColorToRGB.B = (&HFF0000 And Col) \ 65536
End Function