The Overlay method


You can use the Overlay method to combine any two ListImage objects in an ImageList. This works fine on a solid background, but, as Figure 11-13 illustrates, icons don’t come out right. The top image is placed transparently on the bottom image, but the bottom image is placed opaquely on the background. In other words, the Overlay method converts an icon to a bitmap. You can get around this bug (still not fixed from version 4) by inserting the overlaid image back into the ImageList and then using ExtractIcon to remove it as an icon. Here’s the code to use Overlay normally and the code to fix the problem:

With imlstIcons
If chkOverlay.Value <> vbChecked Then
' Overlay without bug fix
imgIconOverlay.Picture = .Overlay(iIconsLast, iIcons)
Else
' Save old background and mask color
Dim clrBack As Long, clrMask As Long
clrBack = .BackColor: clrMask = .MaskColor
' Set color that does not occur in image
.BackColor = vbMagenta: .MaskColor = vbMagenta
' Insert overlay, extract as icon, remove, and restore color
.ListImages.Add 1, , .Overlay(iIconsLast, iIcons)
imgIconOverlay.Picture = .ListImages(1).ExtractIcon
.ListImages.Remove 1
.BackColor = clrBack: .MaskColor = clrMask
End If
End With

This technique works in the sample because I found a temporary background and mask color, vbMagenta, that didn’t occur in any of the sample icons. But what if you were loading the icons at run time? I suppose you could check every pixel in the icon with the Point method, but it’s easier to simply draw the ­images on top of each other using the techniques shown in the next section.