If you're a beginner developer in MFC, you might have come across the term "CImageList". But what exactly is it, and how can you use it in your projects?
CImageList is a class in MFC that allows you to create and manage image lists. Image lists are collections of images that can be used in various UI controls, such as list views, toolbars, and menus. By using image lists, you can enhance the visual appeal and functionality of your applications.
In this guide, we'll explore what CImageList is, how to create and populate image lists, and how to use them in different UI controls.
Creating an Image List
To create an image list, you first need to create an instance of the CImageList class. You can do this by calling the Create() function of the class, which takes the following parameters:
CImageList::Create(int cx, int cy, UINT nFlags, int nInitial, int nGrow);
The first two parameters, cx and cy, specify the width and height of each image in the list. The nFlags parameter is a set of flags that specify the style and behavior of the list. The nInitial parameter is the initial number of images in the list, and the nGrow parameter is the number of images by which the list should grow when it needs to accommodate more images.
For example, the following code creates an image list with 32x32 pixel images, and an initial capacity of 10 images:
CImageList m_imageList;
m_imageList.Create(32, 32, ILC_COLOR24 | ILC_MASK, 10, 10);
Populating an Image List
Once you've created an image list, you need to populate it with images. This can be done by calling the Add() function of the CImageList class, which takes an HBITMAP handle as its parameter.
For example, the following code adds a bitmap image to the image list:
HBITMAP hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
m_imageList.Add(hBitmap, RGB(255, 0, 255));
The first parameter of the Add() function is the handle to the bitmap resource, and the second parameter is the color that should be treated as transparent in the bitmap. In this case, we're using RGB(255, 0, 255) as the transparent color.
You can add multiple images to the image list by calling the Add() function multiple times.
Using an Image List in a List View
Now that you've created and populated an image list, you can use it in a list view control. To do this, you need to set the image list of the list view control by calling its SetImageList() function.
For example, the following code sets the image list of a list view control:
m_listView.SetImageList(&m_imageList, LVSIL_SMALL);
The first parameter of the SetImageList() function is a pointer to the image list to be used in the control, and the second parameter specifies the size of the images to be used. In this case, we're using the LVSIL_SMALL value, which indicates that we're using 16x16 pixel images.
After setting the image list of the list view control, you can assign images to the items in the control by calling its SetItemImage() function.
For example, the following code assigns the first image in the image list to the first item in the list view control:
m_listView.SetItemImage(0, 0);
The first parameter of the SetItemImage() function is the index of the item in the list view control, and the second parameter is the index of the image in the image list. In this case, we're using 0 as the index for both parameters, which means we're assigning the first image in the image list to the first item in the control.
Using an Image List in a Toolbar
You can also use an image list in a toolbar control by setting its image list using the SetImageList() function.
For example, the following code sets the image list in a toolbar control:
m_toolBar.SetImageList(&m_imageList);
After setting the image list of the toolbar control, you can assign images to the buttons in the control by calling its SetButtonInfo() function.
For example, the following code assigns the first image in the image list to the first button in the toolbar control:
TBBUTTON tbButton;
tbButton.iBitmap = 0;
tbButton.idCommand = ID_BUTTON1;
tbButton.fsState = TBSTATE_ENABLED;
tbButton.fsStyle = TBSTYLE_BUTTON;
tbButton.dwData = 0;
tbButton.iString = 0;
m_toolBar.SetButtonInfo(0, ID_BUTTON1, &tbButton);
The first parameter of the SetButtonInfo() function is the index of the button in the toolbar control, and the second parameter is the ID of the command associated with the button. The third parameter is a pointer to a TBBUTTON structure that contains information about the button, including the index of the image in the image list to be used for the button. In this case, we're using 0 as the index for the button and the image, and ID_BUTTON1 as the command ID.
Conclusion
In this guide, we've explored the basics of using CImageList in MFC. We've learned how to create and populate image lists, and how to use them in list view and toolbar controls. By utilizing image lists, you can enhance the visual appeal and functionality of your applications, and provide a better user experience.