Using Image Lists with Header Controls

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Using Image Lists with Header Controls.

Header items have the ability to display an image within a header item. This image, stored in an associated image list, is 16 x 16 pixels and has the same characteristics as the icon images used in a list view control. In order to implement this behavior successfully, you must first create and initialize the image list, associate the list with the header control, and then modify the attributes of the header item that will display the image.

The following procedure illustrates the details, using a pointer to a header control (m_pHdrCtrl) and a pointer to an image list (m_pHdrImages).

To display an image in a header item

  1. Construct a new image list (or use an existing image list object) using the CImageList constructor, storing the resultant pointer.

  2. Initialize the new image list object by calling CImageList::Create. The following code is one example of this call.

          m_ListImageList.Create(16, 16, ILC_COLOR, 2, 2);
    
  3. Add the images for each header item. The following code adds two predefined images.

          m_ListImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
          m_ListImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON2));
    
  4. Associate the image list with the header control with a call to CHeaderCtrl::SetImageList.

  5. Modify the header item to display an image from the associated image list. The following example assigns the first image, from m_phdrImages, to the first header item, m_pHdrCtrl.

             HDITEM curItem = {0};
    
             pHeaderCtrl->SetImageList(&m_ListImageList);
    
             curItem.mask = HDI_TEXT | HDI_FORMAT | HDI_WIDTH | HDI_IMAGE;
             curItem.pszText = _T("Column 1");
             curItem.cxy = 100;
             curItem.iImage = 0;
             curItem.fmt = HDF_LEFT | HDF_STRING | HDF_IMAGE;
             pHeaderCtrl->InsertItem(0, &curItem);
    

For detailed information on the parameter values used, consult the pertinent CHeaderCtrl.

Note

It is possible to have multiple controls using the same image list. For instance, in a standard list view control, there could be an image list (of 16 x 16 pixel images) used by both the small icon view of a list view control and the header items of the list view control.

See Also

Using CHeaderCtrl