Creating an Image List

Creating an image list is easy: just call the ImageList_Create function or, if you are developing your application with MFC, use the Create member function on the CImageList object. For a nonmasked image list, ImageList_Create produces a single bitmap large enough to hold the specified number of images with the given dimensions. It then creates a screen-compatible DC and selects the bitmap into it. For a masked image list, ImageList_Create creates two bitmaps and two screen-compatible DCs. It selects the image bitmap into one DC and selects the mask bitmap into the other. The initial size of the image list is determined by the size values that you specify in your call to ImageList_Create. If you subsequently add more images, the size of the image list automatically increases to accommodate them, based on the number of images you specified as a growth limit.

The following code from the TREEVIEW sample demonstrates how to create an image list, add images to it, and then ensure that all the images have been added:

// First create the image list you will need.
hIml = ImageList_Create (BITMAP_WIDTH, // width
BITMAP_HEIGHT, // height
0, // creation flags
NUM_BITMAPS, // number of images
0); // amount this list can grow

// Load the bitmaps and add them to the image list.
hBmp = LoadBitmap (hInst, MAKEINTRESOURCE (FORSALE));
idxForSale = ImageList_Add (hIml, // handle to image list
hBmp, // handle of bitmap to add
NULL); // handle of bitmap mask

hBmp = LoadBitmap (hInst, MAKEINTRESOURCE (REDMOND));
idxRedmond = ImageList_Add (hIml, hBmp, NULL);

hBmp = LoadBitmap (hInst, MAKEINTRESOURCE (BELLEVUE));
idxBellevue = ImageList_Add (hIml, hBmp, NULL);

hBmp = LoadBitmap (hInst, MAKEINTRESOURCE (SEATTLE));
idxSeattle = ImageList_Add (hIml, hBmp, NULL);

// Be sure that all the bitmaps were added.
if (ImageList_GetImageCount (hIml) < NUM_BITMAPS)
return FALSE;