1. Bind a control variable to the CListCtrl control: m_List.
2. Insert three ICONs as the icon display of the list item, the IDs are: IDI_ICON1, IDI_ICON2, IDI_ICON3. Insert two Bitmaps as the display icons of the list header icons, the IDs are: IDB_BITMAP1, IDB_BITMAP2.
3. The basic steps are: 1. Create an image list; 2. Add multiple icons to the image list; 3. Bind the CImageList object to the CListCtrl control or CHeaderCtrl control; 4. When inserting a list item, according to the icon index insert icon.
Specific code:
First variable declaration:
public: CListCtrl m_List; CImageList m_imagelist;//Define the icon list of list items CImageList m_hbmpList;//Define the icon list of the list header int m_nCol; //The current index with the icon list header BOOL m_bOrder; //Select the icon to display according to true and false afx_msg void OnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult); };
Second, the list item icon is displayed in the initialization function, and some initialization operations are performed on the list header icon:
BOOL C list item and list header icon add Dlg::OnInitDialog() { CDialogEx::OnInitDialog(); //...Omit other auto-generated code //Initialize the header of the List control m_List.InsertColumn(0,"First column",LVCFMT_CENTER,170,-1); m_List.InsertColumn(1,"Second column",LVCFMT_CENTER,170,-1); m_List.InsertColumn(2,"third column",LVCFMT_CENTER,170,-1); m_List.SetExtendedStyle(m_List.GetExtendedStyle()|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES); //The first step: Create creates a list of images m_imagelist.Create(16,16,ILC_COLOR32|ILC_MASK,3,3); //Step 2: Add multiple icons to the image list m_imagelist.Add(AfxGetApp()->LoadIcon(IDI_ICON1)); m_imagelist.Add(AfxGetApp()->LoadIcon(IDI_ICON2)); m_imagelist.Add(AfxGetApp()->LoadIcon(IDI_ICON3)); //Step 3: Bind the CImageList object to the CListCtrl control m_List.SetImageList(&m_imagelist,LVSIL_SMALL); //The fourth step, when inserting a list item, insert the icon according to the index of the icon int nCount = m_List.GetItemCount(); m_List.InsertItem(nCount,"北京",nCount); m_List.SetItemText(nCount,1,"上海"); m_List.SetItemText(nCount,2,"广州"); nCount = m_List.GetItemCount(); m_List.InsertItem(nCount,"深圳",nCount); m_List.SetItemText(nCount,1,"天津"); m_List.SetItemText(nCount,2,"武汉"); nCount = m_List.GetItemCount(); m_List.InsertItem(nCount,"杭州",nCount); m_List.SetItemText(nCount,1,"南京"); m_List.SetItemText(nCount,2,"成都"); /*====================================================*/ //The first step: Create creates a list of images m_hbmpList.Create(16,16,ILC_COLOR32|ILC_MASK,2,2); //Step 2: Load two bitmaps CBitmap b1,b2; b1.LoadBitmap(IDB_BITMAP1); b2.LoadBitmap(IDB_BITMAP2); //Step 3: Add the loaded bitmap to the image list, and specify white for the transparent color m_hbmpList.Add(&b1,RGB(255,255,255)); m_hbmpList.Add(&b2,RGB(255,255,255)); //Step 4: Bind the CImageList object to the CHeaderCtrl control CHeaderCtrl* pHeader = m_List.GetHeaderCtrl(); pHeader->SetImageList(&m_hbmpList); m_nCol = -1;//There is no column header with an icon initially m_bOrder = FALSE; return TRUE; // unless focus is set to control, return TRUE }
For the display of the list header icon, the LVN_COLUMNCLICK message needs to be added to the control:
//Click the response function of the list title void C list item and list header icon add Dlg::OnColumnclickList(NMHDR *pNMHDR, LRESULT *pResult) { LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR); NM_LISTVIEW* pList = (NM_LISTVIEW*)pNMHDR; int nCol = pList->iSubItem;//Which sub-column is clicked, the index starts from 0 int nImageIndex = 0; CHeaderCtrl* pHeader = m_List.GetHeaderCtrl(); HDITEM hDi = {HDI_FORMAT|HDI_IMAGE}; if (nCol != m_nCol) { if (m_nCol > -1)//Remove the old icon when clicking on a different column { pHeader->GetItem(m_nCol,&hDi); hDi.fmt &= ~HDF_IMAGE; pHeader->SetItem(m_nCol,&hDi); } // show new icon m_nCol = nCol; pHeader->GetItem(m_nCol,&hDi); hDi.fmt |= HDF_IMAGE; hDi.iImage = 0; pHeader->SetItem(m_nCol,&hDi); } else//Otherwise click the same column, invert the display { m_bOrder = !m_bOrder; nImageIndex = (m_bOrder == FALSE? 0: 1); pHeader->GetItem(nCol,&hDi); hDi.fmt |= HDF_IMAGE; hDi.iImage = nImageIndex; pHeader->SetItem(nCol,&hDi); } *pResult = 0; }