ImlListImage class

One picture of an ImageList: its number, its key, the picture it was made from, and the methods that draw it and return it as an icon. A program gets one from the ImlListImages collection, and from that collection’s Add method.

Dim Image As ImlListImage
Set Image = ImageList1.ListImages.Add(, "open", ColorSquare(vbRed))
Image.Tag = "Opens a file"
Debug.Print Image.Index; Image.Key
Debug.Print ImageList1.ListImages("open").Tag
Debug.Print Image.Picture.Type = vbPicTypeBitmap
Debug.Print Image.ExtractIcon.Type = vbPicTypeIcon
' Output:
'  1 open
' Opens a file
' True
' True

ColorSquare in the samples of this page is a function of the program’s own that returns a bitmap of one color, 16 pixels wide and high.

A program cannot make an ImlListImage object with New. An object kept in a variable after its picture is removed from the list, or after the list is cleared, still has its number, and its Draw and ExtractIcon then work on the picture that has that number, if there is one.

Properties

Index

The number of the picture in the list, from 1. Long, read-only. It changes when a picture is added or removed before this one.

Key

The key of the picture in the ImlListImages collection. String. Empty for a picture without a key.

An assignment gives the picture another key, and the collection no longer finds it by the old one. An empty string takes the key away. A key that another picture of the list has raises run-time error 457, and the picture keeps its key.

ImageList1.ListImages.Add , "open", ColorSquare(vbRed)
ImageList1.ListImages.Add , "save", ColorSquare(vbBlue)
ImageList1.ListImages("open").Key = "load"
Debug.Print ImageList1.ListImages.Exists("open"); ImageList1.ListImages("load").Index
On Error Resume Next
ImageList1.ListImages("load").Key = "save"
Debug.Print Err.Number; ImageList1.ListImages(1).Key
' Output:
' False 1
'  457 load

Picture

The picture object that was given to Add. The list keeps a copy of its own, of the list’s size and with the transparent parts made at the addition, and Draw and ExtractIcon work on that copy.

The property is read-only: an assignment raises run-time error 383. A program that wants another picture in this place removes the item and adds one at the same number.

Tag

A value of the program’s own, which the list does not use. Variant: unlike the Tag of a control it holds a value of any type, an object included, which is assigned with Set.

A new picture has an empty string.

Methods

Draw

Draws the picture on a device context.

Syntax: object.Draw hDC [, X ] [, Y ] [, Style ]

hDC
required A LongPtr: the handle of the device context, such as the hDC of a form or a picture box.
X, Y
optional Long: where the top left corner of the picture goes, in pixels of the device context. The default is 0 for each.
Style
optional How the picture is drawn: a member of ImlDrawConstants, or the sum of several. Without it the picture is drawn as ImlDrawNormal draws it.
ImageList1.ListImages.Add , "open", ColorSquare(vbRed)
' On the form that the image list is on, 8 pixels from its top left corner.
ImageList1.ListImages("open").Draw ImageList1.Parent.hDC, 8, 8, ImlDrawTransparent

The transparent parts of the picture leave the device context as it is, unless the list’s UseBackColor is True: then they are filled with its BackColor, except with ImlDrawTransparent. With ImlDrawNoMask and UseBackColor False, the pixels of a bitmap that had the mask color are drawn black.

ExtractIcon

Returns the picture as a new icon of the list’s size, with the picture’s transparent parts. The icon is the program’s: it does not change when the list does.

Syntax: object.ExtractIcon( ) As IPictureDisp

See Also