MFC - Image Lists



An Image List is a collection of same-sized images, each of which can be referred to by its zero-based index. Image lists are used to efficiently manage large sets of icons or bitmaps. Image lists are represented by CImageList class.

Let us create a new MFC Application MFCImageListDemo with the following settings.

MFCImageListDemo

Step 1 − Add bmp file as a resource in your application.

Step 2 − In header file of CMFCImageListDemoView class, add the following two variables.

CImageList ImageList;
int nImage;

Step 3 − Add the following code in constructor of CMFCImageListDemoView.

CMFCImageListDemoView::CMFCImageListDemoView() {
   // TODO: add construction code here
   ImageList.Create(800, 800, ILC_COLOR, 4, 1);
   
   CBitmap bmp;
   bmp.LoadBitmap(IDB_BITMAP1);
   ImageList.Add(&bmp, RGB(0, 0, 0));

}

Step 4 − Call the CImageList::Draw() method as follows.

void CMFCImageListDemoView::OnDraw(CDC* pDC) {
   CMFCImageListDemoDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);

   nImage = 0;
   ImageList.Draw(pDC , nImage, CPoint(0,0), ILD_NORMAL);
   Invalidate();

   if (!pDoc)
      return;

   // TODO: add draw code for native data here
}

Step 5 − When you run this application, you will see the following output.

Image Lists
mfc_windows_controls.htm
Advertisements