Visual C++ and MFC Fundamentals Chapter 20: List-Based Controls
of images that the image list will contain. The nGrow argument represents the number of
images by which the image list can grow.
If you had designed an unmasked bitmap using the image editor and you want to
initialize it, call the second or the third versions of the Create() method. The nBitmapID
argument is the identifier of the bitmap. If you want to provide the string that contains the
identifiers of the images, pass it as the lpszBitmapID argument. The cx value represents
the width of each picture. The crMask is the color used to mask the transparency of the
picture. Each pixel of the picture that matches this color will turn to black. Here is an
example of using this Create() method:
BOOL CAnimation1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CImageList ImgList;
ImgList.Create(IDB_IMGLIST, 48, 4, RGB(255, 55, 5));
return TRUE; // return TRUE unless you set the focus to a control
}
Besides, or instead of using, the Create() method, you can call CImageList::Add() to
add a bitmap to the CImageList variable. Its syntaxes are:
int Add(CBitmap* pbmImage, CBitmap* pbmMask);
int Add(CBitmap* pbmImage, COLORREF crMask);
int Add(HICON hIcon );
The pbmImage argument represents the bitmap to be added, unless you want to add an
icon, in which case you would use the third version. The pbmMask argument is the
bitmap that will be used to mask the image list. You can use a color instead, in which
case you would pass a COLORREF value as the second argument to the second version.
If you want to remove a picture from the image list, call the CImageList::Remove()
method. Its syntax:
BOOL Remove(int nImage);
The nImage argument is the index of the picture to be removed. Instead of removing a
picture, you can just replace it with another picture. This is done using the
CImageList::Replace() method whose syntaxes are:
BOOL Replace(int nImage, CBitmap* pbmImage, CBitmap* pbmMask);
int Replace(int nImage, HICON hIcon);
Once an image list is ready, you can use it directly in an application or make it available
to a control that can use it. One way you can use an image list is to display one or more of
its pictures on a dialog box, a form, or a view. To do this, you would call the
CImageList::Draw() method. Its syntax is:
BOOL Draw(CDC* pdc, int nImage, POINT pt, UINT nStyle);